You are here

public function ClassCreateInstanceSniff::process in Coder 8.3

Same name and namespace in other branches
  1. 8.2 coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php \Drupal\Sniffs\Classes\ClassCreateInstanceSniff::process()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php \Drupal\Sniffs\Classes\ClassCreateInstanceSniff::process()

Processes this test, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $stackPtr The position of the current token in the: stack passed in $tokens.

Return value

void

File

coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php, line 50

Class

ClassCreateInstanceSniff
Class create instance Test.

Namespace

Drupal\Sniffs\Classes

Code

public function process(File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();
  $commaOrColon = $phpcsFile
    ->findNext([
    T_SEMICOLON,
    T_COLON,
    T_COMMA,
  ], $stackPtr + 1);
  if ($commaOrColon === false) {

    // Syntax error, nothing we can do.
    return;
  }

  // Search for an opening parenthesis in the current statement until the
  // next semicolon or comma.
  $nextParenthesis = $phpcsFile
    ->findNext(T_OPEN_PARENTHESIS, $stackPtr + 1, $commaOrColon);
  if ($nextParenthesis === false) {
    $error = 'Calling class constructors must always include parentheses';
    $constructor = $phpcsFile
      ->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);

    // We can invoke the fixer if we know this is a static constructor
    // function call or constructor calls with namespaces, example
    // "new \DOMDocument;" or constructor with class names in variables
    // "new $controller;".
    if ($tokens[$constructor]['code'] === T_STRING || $tokens[$constructor]['code'] === T_NS_SEPARATOR || $tokens[$constructor]['code'] === T_VARIABLE && $tokens[$constructor + 1]['code'] === T_SEMICOLON) {

      // Scan to the end of possible string\namespace parts.
      $nextConstructorPart = $constructor;
      while (true) {
        $nextConstructorPart = $phpcsFile
          ->findNext(Tokens::$emptyTokens, $nextConstructorPart + 1, null, true, null, true);
        if ($nextConstructorPart === false || $tokens[$nextConstructorPart]['code'] !== T_STRING && $tokens[$nextConstructorPart]['code'] !== T_NS_SEPARATOR) {
          break;
        }
        $constructor = $nextConstructorPart;
      }
      $fix = $phpcsFile
        ->addFixableError($error, $constructor, 'ParenthesisMissing');
      if ($fix === true) {
        $phpcsFile->fixer
          ->addContent($constructor, '()');
      }

      // We can invoke the fixer if we know this is a
      // constructor call with class names in an array
      // example "new $controller[$i];".
    }
    else {
      if ($tokens[$constructor]['code'] === T_VARIABLE && $tokens[$constructor + 1]['code'] === T_OPEN_SQUARE_BRACKET) {

        // Scan to the end of possible multilevel arrays.
        $nextConstructorPart = $constructor;
        do {
          $nextConstructorPart = $tokens[$nextConstructorPart + 1]['bracket_closer'];
        } while ($tokens[$nextConstructorPart + 1]['code'] === T_OPEN_SQUARE_BRACKET);
        $fix = $phpcsFile
          ->addFixableError($error, $nextConstructorPart, 'ParenthesisMissing');
        if ($fix === true) {
          $phpcsFile->fixer
            ->addContent($nextConstructorPart, '()');
        }
      }
      else {
        $phpcsFile
          ->addError($error, $stackPtr, 'ParenthesisMissing');
      }
    }

    //end if
  }

  //end if
}