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

Breadcrumb

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

function UnnecessaryFinalModifierSniff::process

Processes this test, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $stackPtr The position of the current token: in the stack passed in $tokens.

Return value

void

Overrides Sniff::process

File

vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/CodeAnalysis/UnnecessaryFinalModifierSniff.php, line 53

Class

UnnecessaryFinalModifierSniff

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $token = $tokens[$stackPtr];
    // Skip for statements without body.
    if (isset($token['scope_opener']) === false) {
        return;
    }
    if ($phpcsFile->getClassProperties($stackPtr)['is_final'] === false) {
        // This class is not final so we don't need to check it.
        return;
    }
    $next = ++$token['scope_opener'];
    $end = --$token['scope_closer'];
    for (; $next <= $end; ++$next) {
        if ($tokens[$next]['code'] === T_FINAL) {
            $error = 'Unnecessary FINAL modifier in FINAL class';
            $phpcsFile->addWarning($error, $next, 'Found');
        }
        // Skip over the contents of functions as those can't contain the `final` keyword anyway.
        if ($tokens[$next]['code'] === T_FUNCTION && isset($tokens[$next]['scope_closer']) === true) {
            $next = $tokens[$next]['scope_closer'];
        }
    }
}
RSS feed
Powered by Drupal