Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. UselessOverridingMethodSniff.php

class UselessOverridingMethodSniff

Hierarchy

  • class \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\UselessOverridingMethodSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of UselessOverridingMethodSniff

File

vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php, line 28

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis
View source
class UselessOverridingMethodSniff implements Sniff {
    
    /**
     * Object-Oriented scopes in which a call to parent::method() can exist.
     *
     * @var array<int|string, bool> Keys are the token constants, value is irrelevant.
     */
    private $validOOScopes = [
        T_CLASS => true,
        T_ANON_CLASS => true,
        T_TRAIT => true,
    ];
    
    /**
     * Registers the tokens that this sniff wants to listen for.
     *
     * @return array<int|string>
     */
    public function register() {
        return [
            T_FUNCTION,
        ];
    }
    
    //end register()
    
    /**
     * Processes this test, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
     * @param int                         $stackPtr  The position of the current token
     *                                               in the stack passed in $tokens.
     *
     * @return void
     */
    public function process(File $phpcsFile, $stackPtr) {
        $tokens = $phpcsFile->getTokens();
        $token = $tokens[$stackPtr];
        // Skip function without body.
        if (isset($token['scope_opener'], $token['scope_closer']) === false) {
            return;
        }
        $conditions = $token['conditions'];
        $lastCondition = end($conditions);
        // Skip functions that are not a method part of a class, anon class or trait.
        if (isset($this->validOOScopes[$lastCondition]) === false) {
            return;
        }
        // Get function name.
        $methodName = $phpcsFile->getDeclarationName($stackPtr);
        // Get all parameters from method signature.
        $signature = [];
        foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
            $signature[] = $param['name'];
        }
        $next = ++$token['scope_opener'];
        $end = --$token['scope_closer'];
        for (; $next <= $end; ++$next) {
            $code = $tokens[$next]['code'];
            if (isset(Tokens::$emptyTokens[$code]) === true) {
                continue;
            }
            else {
                if ($code === T_RETURN) {
                    continue;
                }
            }
            break;
        }
        // Any token except 'parent' indicates correct code.
        if ($tokens[$next]['code'] !== T_PARENT) {
            return;
        }
        // Find next non empty token index, should be double colon.
        $next = $phpcsFile->findNext(Tokens::$emptyTokens, $next + 1, null, true);
        // Skip for invalid code.
        if ($tokens[$next]['code'] !== T_DOUBLE_COLON) {
            return;
        }
        // Find next non empty token index, should be the name of the method being called.
        $next = $phpcsFile->findNext(Tokens::$emptyTokens, $next + 1, null, true);
        // Skip for invalid code or other method.
        if (strcasecmp($tokens[$next]['content'], $methodName) !== 0) {
            return;
        }
        // Find next non empty token index, should be the open parenthesis.
        $next = $phpcsFile->findNext(Tokens::$emptyTokens, $next + 1, null, true);
        // Skip for invalid code.
        if ($tokens[$next]['code'] !== T_OPEN_PARENTHESIS || isset($tokens[$next]['parenthesis_closer']) === false) {
            return;
        }
        $parameters = [
            '',
        ];
        $parenthesisCount = 1;
        for (++$next; $next < $phpcsFile->numTokens; ++$next) {
            $code = $tokens[$next]['code'];
            if ($code === T_OPEN_PARENTHESIS) {
                ++$parenthesisCount;
            }
            else {
                if ($code === T_CLOSE_PARENTHESIS) {
                    --$parenthesisCount;
                }
                else {
                    if ($parenthesisCount === 1 && $code === T_COMMA) {
                        $parameters[] = '';
                    }
                    else {
                        if (isset(Tokens::$emptyTokens[$code]) === false) {
                            $parameters[count($parameters) - 1] .= $tokens[$next]['content'];
                        }
                    }
                }
            }
            if ($parenthesisCount === 0) {
                break;
            }
        }
        
        //end for
        $next = $phpcsFile->findNext(Tokens::$emptyTokens, $next + 1, null, true);
        if ($tokens[$next]['code'] !== T_SEMICOLON && $tokens[$next]['code'] !== T_CLOSE_TAG) {
            return;
        }
        // This list deliberately does not include the `T_OPEN_TAG_WITH_ECHO` as that token implicitly is an echo statement, i.e. content.
        $nonContent = Tokens::$emptyTokens;
        $nonContent[T_OPEN_TAG] = T_OPEN_TAG;
        $nonContent[T_CLOSE_TAG] = T_CLOSE_TAG;
        // Check rest of the scope.
        for (++$next; $next <= $end; ++$next) {
            $code = $tokens[$next]['code'];
            // Skip for any other content.
            if (isset($nonContent[$code]) === false) {
                return;
            }
        }
        $parameters = array_map('trim', $parameters);
        $parameters = array_filter($parameters);
        if (count($parameters) === count($signature) && $parameters === $signature) {
            $phpcsFile->addWarning('Possible useless method overriding detected', $stackPtr, 'Found');
        }
    }
    
    //end process()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
UselessOverridingMethodSniff::$validOOScopes private property Object-Oriented scopes in which a call to parent::method() can exist.
UselessOverridingMethodSniff::process public function Processes this test, when one of its tokens is encountered. Overrides Sniff::process
UselessOverridingMethodSniff::register public function Registers the tokens that this sniff wants to listen for. Overrides Sniff::register
RSS feed
Powered by Drupal