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

Breadcrumb

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

class MethodDeclarationSniff

Same name in this branch
  1. 11.1.x vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Methods/MethodDeclarationSniff.php \Drupal\Sniffs\Methods\MethodDeclarationSniff

Hierarchy

  • class \PHP_CodeSniffer\Sniffs\AbstractScopeSniff implements \PHP_CodeSniffer\Sniffs\Sniff
    • class \PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods\MethodDeclarationSniff extends \PHP_CodeSniffer\Sniffs\AbstractScopeSniff

Expanded class hierarchy of MethodDeclarationSniff

1 file declares its use of MethodDeclarationSniff
MethodDeclarationSniff.php in vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Methods/MethodDeclarationSniff.php

File

vendor/squizlabs/php_codesniffer/src/Standards/PSR2/Sniffs/Methods/MethodDeclarationSniff.php, line 16

Namespace

PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods
View source
class MethodDeclarationSniff extends AbstractScopeSniff {
    
    /**
     * Constructs a Squiz_Sniffs_Scope_MethodScopeSniff.
     */
    public function __construct() {
        parent::__construct(Tokens::$ooScopeTokens, [
            T_FUNCTION,
        ]);
    }
    
    //end __construct()
    
    /**
     * Processes the function tokens within the class.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
     * @param int                         $stackPtr  The position where the token was found.
     * @param int                         $currScope The current scope opener token.
     *
     * @return void
     */
    protected 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;
        }
        $methodName = $phpcsFile->getDeclarationName($stackPtr);
        if ($methodName === null) {
            // Ignore closures.
            return;
        }
        if ($methodName[0] === '_' && isset($methodName[1]) === true && $methodName[1] !== '_') {
            $error = 'Method name "%s" should not be prefixed with an underscore to indicate visibility';
            $data = [
                $methodName,
            ];
            $phpcsFile->addWarning($error, $stackPtr, 'Underscore', $data);
        }
        $visibility = 0;
        $static = 0;
        $abstract = 0;
        $final = 0;
        $find = Tokens::$methodPrefixes + Tokens::$emptyTokens;
        $prev = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true);
        $prefix = $stackPtr;
        while (($prefix = $phpcsFile->findPrevious(Tokens::$methodPrefixes, $prefix - 1, $prev)) !== false) {
            switch ($tokens[$prefix]['code']) {
                case T_STATIC:
                    $static = $prefix;
                    break;
                case T_ABSTRACT:
                    $abstract = $prefix;
                    break;
                case T_FINAL:
                    $final = $prefix;
                    break;
                default:
                    $visibility = $prefix;
                    break;
            }
        }
        $fixes = [];
        if ($visibility !== 0 && $final > $visibility) {
            $error = 'The final declaration must precede the visibility declaration';
            $fix = $phpcsFile->addFixableError($error, $final, 'FinalAfterVisibility');
            if ($fix === true) {
                $fixes[$final] = '';
                $fixes[$final + 1] = '';
                if (isset($fixes[$visibility]) === true) {
                    $fixes[$visibility] = 'final ' . $fixes[$visibility];
                }
                else {
                    $fixes[$visibility] = 'final ' . $tokens[$visibility]['content'];
                }
            }
        }
        if ($visibility !== 0 && $abstract > $visibility) {
            $error = 'The abstract declaration must precede the visibility declaration';
            $fix = $phpcsFile->addFixableError($error, $abstract, 'AbstractAfterVisibility');
            if ($fix === true) {
                $fixes[$abstract] = '';
                $fixes[$abstract + 1] = '';
                if (isset($fixes[$visibility]) === true) {
                    $fixes[$visibility] = 'abstract ' . $fixes[$visibility];
                }
                else {
                    $fixes[$visibility] = 'abstract ' . $tokens[$visibility]['content'];
                }
            }
        }
        if ($static !== 0 && $static < $visibility) {
            $error = 'The static declaration must come after the visibility declaration';
            $fix = $phpcsFile->addFixableError($error, $static, 'StaticBeforeVisibility');
            if ($fix === true) {
                $fixes[$static] = '';
                $fixes[$static + 1] = '';
                if (isset($fixes[$visibility]) === true) {
                    $fixes[$visibility] .= ' static';
                }
                else {
                    $fixes[$visibility] = $tokens[$visibility]['content'] . ' static';
                }
            }
        }
        // Batch all the fixes together to reduce the possibility of conflicts.
        if (empty($fixes) === false) {
            $phpcsFile->fixer
                ->beginChangeset();
            foreach ($fixes as $stackPtr => $content) {
                $phpcsFile->fixer
                    ->replaceToken($stackPtr, $content);
            }
            $phpcsFile->fixer
                ->endChangeset();
        }
    }
    
    //end processTokenWithinScope()
    
    /**
     * 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 Overrides
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
MethodDeclarationSniff::processTokenOutsideScope protected function Processes a token that is found within the scope that this test is
listening to.
Overrides AbstractScopeSniff::processTokenOutsideScope
MethodDeclarationSniff::processTokenWithinScope protected function Processes the function tokens within the class. Overrides AbstractScopeSniff::processTokenWithinScope
MethodDeclarationSniff::__construct public function Constructs a Squiz_Sniffs_Scope_MethodScopeSniff. Overrides AbstractScopeSniff::__construct 1
RSS feed
Powered by Drupal