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

Breadcrumb

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

function SideEffectsSniff::process

Processes this sniff, 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 token stack.

Return value

int

Overrides Sniff::process

File

vendor/squizlabs/php_codesniffer/src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php, line 41

Class

SideEffectsSniff

Namespace

PHP_CodeSniffer\Standards\PSR1\Sniffs\Files

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $result = $this->searchForConflict($phpcsFile, 0, $phpcsFile->numTokens - 1, $tokens);
    if ($result['symbol'] !== null && $result['effect'] !== null) {
        $error = 'A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line %s and the first side effect is on line %s.';
        $data = [
            $tokens[$result['symbol']]['line'],
            $tokens[$result['effect']]['line'],
        ];
        $phpcsFile->addWarning($error, 0, 'FoundWithSymbols', $data);
        $phpcsFile->recordMetric($stackPtr, 'Declarations and side effects mixed', 'yes');
    }
    else {
        $phpcsFile->recordMetric($stackPtr, 'Declarations and side effects mixed', 'no');
    }
    // Ignore the rest of the file.
    return $phpcsFile->numTokens;
}
RSS feed
Powered by Drupal