Commit 6f6b654c authored by Klaus Purer's avatar Klaus Purer
Browse files

fix(GlobalFunction): Detect use of global functions in classes that have...

fix(GlobalFunction): Detect use of global functions in classes that have access to the container. (#3056538)
parent efe6ac6d
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -90,9 +90,14 @@ class GlobalClassSniff implements Sniff
        $classPtr    = key($tokens[$stackPtr]['conditions']);
        $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;
        }
+5 −0
Original line number Diff line number Diff line
@@ -94,8 +94,13 @@ class GlobalDrupalSniff implements Sniff
        $classPtr    = key($tokens[$stackPtr]['conditions']);
        $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, static::$baseClasses) === false)
            && Project::isServiceClass($phpcsFile, $classPtr) === false
            && $canAccessContainer === false
        ) {
            return;
        }
+41 −12
Original line number Diff line number Diff line
@@ -45,6 +45,16 @@ class GlobalFunctionSniff implements Sniff
        'user_role_load'           => 'the "entity_type.manager" service',
    ];

    /**
     * List of global functions that are covered by traits.
     *
     * This is a subset of the global functions list. These functions can be
     * replaced by methods that are provided by the listed trait.
     *
     * @var string[]
     */
    protected $traitFunctions = ['t' => '\Drupal\Core\StringTranslation\StringTranslationTrait'];


    /**
     * Returns an array of tokens this test wants to listen for.
@@ -98,11 +108,21 @@ class GlobalFunctionSniff implements Sniff
        // 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;
            }
@@ -112,6 +132,15 @@ class GlobalFunctionSniff implements Sniff
                $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);

    }//end process()
+5 −0
Original line number Diff line number Diff line
@@ -38,6 +38,8 @@ class GlobalClassUnitTest extends CoderSniffUnitTest
        switch ($testFile) {
        case 'GlobalClassUnitTest.inc':
            return [8 => 1];
        case 'ExampleClassWithDependencyInjection.php':
            return [24 => 1];
        case 'ExampleService.php':
            return [23 => 1];
        default:
@@ -58,6 +60,9 @@ class GlobalClassUnitTest extends CoderSniffUnitTest
    {
        return [
            __DIR__.'/GlobalClassUnitTest.inc',
            __DIR__.'/src/example.module',
            __DIR__.'/src/ExampleClass.php',
            __DIR__.'/src/ExampleClassWithDependencyInjection.php',
            __DIR__.'/src/ExampleService.php',
        ];

+5 −0
Original line number Diff line number Diff line
@@ -38,6 +38,8 @@ class GlobalDrupalUnitTest extends CoderSniffUnitTest
        switch ($testFile) {
        case 'GlobalDrupalUnitTest.inc':
            return [6 => 1];
        case 'ExampleClassWithDependencyInjection.php':
            return [17 => 1];
        case 'ExampleService.php':
            return [16 => 1];
        default:
@@ -58,6 +60,9 @@ class GlobalDrupalUnitTest extends CoderSniffUnitTest
    {
        return [
            __DIR__.'/GlobalDrupalUnitTest.inc',
            __DIR__.'/src/example.module',
            __DIR__.'/src/ExampleClass.php',
            __DIR__.'/src/ExampleClassWithDependencyInjection.php',
            __DIR__.'/src/ExampleService.php',
        ];

Loading