You are here

public function GlobalFunctionSniff::process in Coder 8.3

Same name and namespace in other branches
  1. 8.2 coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalFunctionSniff.php \DrupalPractice\Sniffs\Objects\GlobalFunctionSniff::process()
  2. 8.3.x coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalFunctionSniff.php \DrupalPractice\Sniffs\Objects\GlobalFunctionSniff::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|int

Overrides FunctionCall::process

File

coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalFunctionSniff.php, line 67

Class

GlobalFunctionSniff
Checks that global functions like t() are not used in forms or controllers.

Namespace

DrupalPractice\Sniffs\Objects

Code

public function process(File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();

  // Only run this sniff on Drupal 8+.
  if (Project::getCoreVersion($phpcsFile) < 8) {

    // No need to check this file again, mark it as done.
    return $phpcsFile->numTokens + 1;
  }

  // We just want to listen on function calls, nothing else.
  if ($this
    ->isFunctionCall($phpcsFile, $stackPtr) === false || empty($tokens[$stackPtr]['conditions']) === true || isset($this->functions[$tokens[$stackPtr]['content']]) === false) {
    return;
  }

  // Check that this statement is not in a static function.
  foreach ($tokens[$stackPtr]['conditions'] as $conditionPtr => $conditionCode) {
    if ($conditionCode === T_FUNCTION && $phpcsFile
      ->getMethodProperties($conditionPtr)['is_static'] === true) {
      return;
    }
  }

  // Check if the class extends another class and get the name of the class
  // that is extended.
  $classPtr = key($tokens[$stackPtr]['conditions']);
  if ($tokens[$classPtr]['code'] !== T_CLASS) {
    return;
  }
  if (isset($this->traitFunctions[$tokens[$stackPtr]['content']]) === false) {
    $extendsName = $phpcsFile
      ->findExtendedClassName($classPtr);

    // Check if the class implements ContainerInjectionInterface.
    $implementedInterfaceNames = $phpcsFile
      ->findImplementedInterfaceNames($classPtr);
    $canAccessContainer = !empty($implementedInterfaceNames) && in_array('ContainerInjectionInterface', $implementedInterfaceNames);
    if (($extendsName === false || in_array($extendsName, GlobalDrupalSniff::$baseClasses) === false) && Project::isServiceClass($phpcsFile, $classPtr) === false && $canAccessContainer === false) {
      return;
    }
    $warning = '%s() calls should be avoided in classes, use dependency injection and %s instead';
    $data = [
      $tokens[$stackPtr]['content'],
      $this->functions[$tokens[$stackPtr]['content']],
    ];
  }
  else {
    $warning = '%s() calls should be avoided in classes, use %s and %s instead';
    $data = [
      $tokens[$stackPtr]['content'],
      $this->traitFunctions[$tokens[$stackPtr]['content']],
      $this->functions[$tokens[$stackPtr]['content']],
    ];
  }

  //end if
  $phpcsFile
    ->addWarning($warning, $stackPtr, 'GlobalFunction', $data);
}