Commit aaa69893 authored by Klaus Purer's avatar Klaus Purer
Browse files

fix(GlobalFunction): declaring a t() method in a class is flagged incorrectly...

fix(GlobalFunction): declaring a t() method in a class is flagged incorrectly (#3059712 by klausi, pwolanin)
parent 38328fc7
Loading
Loading
Loading
Loading
+7 −26
Original line number Diff line number Diff line
@@ -10,9 +10,9 @@
namespace DrupalPractice\Sniffs\Objects;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use DrupalPractice\Sniffs\Objects\GlobalDrupalSniff;
use DrupalPractice\Project;
use Drupal\Sniffs\Semantics\FunctionCall;

/**
 * Checks that global functions like t() are not used in forms or controllers.
@@ -21,7 +21,7 @@ use DrupalPractice\Project;
 * @package  PHP_CodeSniffer
 * @link     http://pear.php.net/package/PHP_CodeSniffer
 */
class GlobalFunctionSniff implements Sniff
class GlobalFunctionSniff extends FunctionCall
{

    /**
@@ -56,18 +56,6 @@ class GlobalFunctionSniff implements Sniff
    protected $traitFunctions = ['t' => '\Drupal\Core\StringTranslation\StringTranslationTrait'];


    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array
     */
    public function register()
    {
        return [T_STRING];

    }//end register()


    /**
     * Processes this test, when one of its tokens is encountered.
     *
@@ -86,23 +74,16 @@ class GlobalFunctionSniff implements Sniff
            return;
        }

        // We are only interested in function calls, which are not in the global
        // scope.
        if (isset($this->functions[$tokens[$stackPtr]['content']]) === false
            || isset($tokens[($stackPtr + 1)]) === false
            || $tokens[($stackPtr + 1)]['code'] !== T_OPEN_PARENTHESIS
        // We just want to listen on function calls, nothing else.
        if ($this->isFunctionCall($phpcsFile, $stackPtr) === false
            // Ignore function calls in the global scope.
            || empty($tokens[$stackPtr]['conditions']) === true
            // Only our list of function names.
            || isset($this->functions[$tokens[$stackPtr]['content']]) === false
        ) {
            return;
        }

        // If there is an object operator before the call then this is a method
        // invocation, not a function call.
        $previous = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
        if ($tokens[$previous]['code'] === T_OBJECT_OPERATOR) {
            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) {
+1 −0
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ class GlobalFunctionUnitTest extends CoderSniffUnitTest
    protected function getTestFiles($testFileBase)
    {
        return [
            __DIR__.'/src/DeclareT.php',
            __DIR__.'/src/example.module',
            __DIR__.'/src/ExampleClass.php',
            __DIR__.'/src/ExampleClassWithDependencyInjection.php',
+12 −0
Original line number Diff line number Diff line
<?php

class DeclareT {

  /**
   * Just declaring a t() method here, should not throw errors.
   */
  function t() {
    return '';
  }

}