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

Breadcrumb

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

function ClassFilesSniff::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

int

Overrides Sniff::process

File

vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/InfoFiles/ClassFilesSniff.php, line 48

Class

ClassFilesSniff
Checks files[] entries in info files. Only files containing classes/interfaces should be listed.

Namespace

Drupal\Sniffs\InfoFiles

Code

public function process(File $phpcsFile, $stackPtr) {
    // Only run this sniff once per info file.
    $fileExtension = strtolower(substr($phpcsFile->getFilename(), -4));
    if ($fileExtension !== 'info') {
        return $phpcsFile->numTokens + 1;
    }
    $contents = file_get_contents($phpcsFile->getFilename());
    $info = self::drupalParseInfoFormat($contents);
    if (isset($info['files']) === true && is_array($info['files']) === true) {
        foreach ($info['files'] as $file) {
            $fileName = dirname($phpcsFile->getFilename()) . '/' . $file;
            if (file_exists($fileName) === false) {
                // We need to find the position of the offending line in the
                // info file.
                $ptr = self::getPtr('files[]', $file, $phpcsFile);
                $error = 'Declared file was not found';
                $phpcsFile->addError($error, $ptr, 'DeclaredFileNotFound');
                continue;
            }
            // Read the file, parse its tokens and check if it actually contains
            // a class or interface definition.
            $searchTokens = token_get_all(file_get_contents($fileName));
            foreach ($searchTokens as $token) {
                if (is_array($token) === true && in_array($token[0], [
                    T_CLASS,
                    T_INTERFACE,
                    T_TRAIT,
                    T_ENUM,
                ]) === true) {
                    continue 2;
                }
            }
            $ptr = self::getPtr('files[]', $file, $phpcsFile);
            $error = "It's only necessary to declare files[] if they declare a class or interface.";
            // cspell:ignore UnecessaryFileDeclaration
            $phpcsFile->addError($error, $ptr, 'UnecessaryFileDeclaration');
        }
        
        //end foreach
    }
    
    //end if
    return $phpcsFile->numTokens + 1;
}
RSS feed
Powered by Drupal