public function Drupal_Sniffs_InfoFiles_ClassFilesSniff::process in Coder 7.2
Processes this test, when one of its tokens is encountered.
Parameters
PHP_CodeSniffer_File $phpcsFile The file being scanned.:
int $stackPtr The position of the current token in the: stack passed in $tokens.
Return value
void
File
- coder_sniffer/
Drupal/ Sniffs/ InfoFiles/ ClassFilesSniff.php, line 45
Class
- Drupal_Sniffs_InfoFiles_ClassFilesSniff
- Checks files[] entries in info files. Only files containing classes/interfaces should be listed.
Code
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
$fileExtension = strtolower(substr($phpcsFile
->getFilename(), -4));
if ($fileExtension !== 'info') {
return;
}
$tokens = $phpcsFile
->getTokens();
// Only run this sniff once per info file.
if ($tokens[$stackPtr]['line'] !== 1) {
return;
}
$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 && ($token[0] === T_CLASS || $token[0] === T_INTERFACE)) {
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
}