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

Breadcrumb

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

class StaticThisUsageSniff

Hierarchy

  • class \PHP_CodeSniffer\Sniffs\AbstractScopeSniff implements \PHP_CodeSniffer\Sniffs\Sniff
    • class \PHP_CodeSniffer\Standards\Squiz\Sniffs\Scope\StaticThisUsageSniff extends \PHP_CodeSniffer\Sniffs\AbstractScopeSniff

Expanded class hierarchy of StaticThisUsageSniff

File

vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Scope/StaticThisUsageSniff.php, line 16

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\Scope
View source
class StaticThisUsageSniff extends AbstractScopeSniff {
    
    /**
     * Constructs the test with the tokens it wishes to listen for.
     */
    public function __construct() {
        parent::__construct([
            T_CLASS,
            T_TRAIT,
            T_ENUM,
            T_ANON_CLASS,
        ], [
            T_FUNCTION,
        ]);
    }
    
    //end __construct()
    
    /**
     * Processes this test, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned.
     * @param int                         $stackPtr  The position of the current token in the
     *                                               stack passed in $tokens.
     * @param int                         $currScope A pointer to the start of the scope.
     *
     * @return void
     */
    public function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) {
        $tokens = $phpcsFile->getTokens();
        // Determine if this is a function which needs to be examined.
        $conditions = $tokens[$stackPtr]['conditions'];
        end($conditions);
        $deepestScope = key($conditions);
        if ($deepestScope !== $currScope) {
            return;
        }
        // Ignore abstract functions.
        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
            return;
        }
        $next = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true);
        if ($next === false || $tokens[$next]['code'] !== T_STRING) {
            // Not a function declaration, or incomplete.
            return;
        }
        $methodProps = $phpcsFile->getMethodProperties($stackPtr);
        if ($methodProps['is_static'] === false) {
            return;
        }
        $next = $stackPtr;
        $end = $tokens[$stackPtr]['scope_closer'];
        $this->checkThisUsage($phpcsFile, $next, $end);
    }
    
    //end processTokenWithinScope()
    
    /**
     * Check for $this variable usage between $next and $end tokens.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned.
     * @param int                         $next      The position of the next token to check.
     * @param int                         $end       The position of the last token to check.
     *
     * @return void
     */
    private function checkThisUsage(File $phpcsFile, $next, $end) {
        $tokens = $phpcsFile->getTokens();
        do {
            $next = $phpcsFile->findNext([
                T_VARIABLE,
                T_ANON_CLASS,
            ], $next + 1, $end);
            if ($next === false) {
                continue;
            }
            if ($tokens[$next]['code'] === T_ANON_CLASS) {
                $this->checkThisUsage($phpcsFile, $next, $tokens[$next]['scope_opener']);
                $next = $tokens[$next]['scope_closer'];
                continue;
            }
            if ($tokens[$next]['content'] !== '$this') {
                continue;
            }
            $error = 'Usage of "$this" in static methods will cause runtime errors';
            $phpcsFile->addError($error, $next, 'Found');
        } while ($next !== false);
    }
    
    //end checkThisUsage()
    
    /**
     * Processes a token that is found within the scope that this test is
     * listening to.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
     * @param int                         $stackPtr  The position in the stack where this
     *                                               token was found.
     *
     * @return void
     */
    protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) {
    }
    
    //end processTokenOutsideScope()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AbstractScopeSniff::$listenOutside private property True if this test should fire on tokens outside of the scope.
AbstractScopeSniff::$scopeTokens private property The type of scope opener tokens that this test wishes to listen to.
AbstractScopeSniff::$tokens private property The token types that this test wishes to listen to within the scope.
AbstractScopeSniff::process final public function Processes the tokens that this test is listening for. Overrides Sniff::process
AbstractScopeSniff::register final public function The method that is called to register the tokens this test wishes to
listen to.
Overrides Sniff::register
StaticThisUsageSniff::checkThisUsage private function Check for $this variable usage between $next and $end tokens.
StaticThisUsageSniff::processTokenOutsideScope protected function Processes a token that is found within the scope that this test is
listening to.
Overrides AbstractScopeSniff::processTokenOutsideScope
StaticThisUsageSniff::processTokenWithinScope public function Processes this test, when one of its tokens is encountered. Overrides AbstractScopeSniff::processTokenWithinScope
StaticThisUsageSniff::__construct public function Constructs the test with the tokens it wishes to listen for. Overrides AbstractScopeSniff::__construct
RSS feed
Powered by Drupal