public function ClassFilesSniff::process in Coder 8.3.x
Same name and namespace in other branches
- 8.3 coder_sniffer/Drupal/Sniffs/InfoFiles/ClassFilesSniff.php \Drupal\Sniffs\InfoFiles\ClassFilesSniff::process()
- 8.2 coder_sniffer/Drupal/Sniffs/InfoFiles/ClassFilesSniff.php \Drupal\Sniffs\InfoFiles\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
File
- 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\InfoFilesCode
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,
]) === true) {
continue 2;
}
}
$ptr = self::getPtr('files[]', $file, $phpcsFile);
$error = "It's only necessary to declare files[] if they declare a class or interface.";
$phpcsFile
->addError($error, $ptr, 'UnecessaryFileDeclaration');
}
//end foreach
}
//end if
return $phpcsFile->numTokens + 1;
}