function FileHeaderSniff::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 stack.
Return value
int|void
Overrides Sniff::process
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Standards/ PSR12/ Sniffs/ Files/ FileHeaderSniff.php, line 41
Class
Namespace
PHP_CodeSniffer\Standards\PSR12\Sniffs\FilesCode
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$possibleHeaders = [];
$searchFor = Tokens::$ooScopeTokens;
$searchFor[T_OPEN_TAG] = T_OPEN_TAG;
$openTag = $stackPtr;
do {
$headerLines = $this->getHeaderLines($phpcsFile, $openTag);
if (empty($headerLines) === true && $openTag === $stackPtr) {
// No content in the file.
return;
}
$possibleHeaders[$openTag] = $headerLines;
if (count($headerLines) > 1) {
break;
}
$next = $phpcsFile->findNext($searchFor, $openTag + 1);
if (isset(Tokens::$ooScopeTokens[$tokens[$next]['code']]) === true) {
// Once we find an OO token, the file content has
// definitely started.
break;
}
$openTag = $next;
} while ($openTag !== false);
if ($openTag === false) {
// We never found a proper file header.
// If the file has multiple PHP open tags, we know
// that it must be a mix of PHP and HTML (or similar)
// so the header rules do not apply.
if (count($possibleHeaders) > 1) {
return $phpcsFile->numTokens;
}
// There is only one possible header.
// If it is the first content in the file, it technically
// serves as the file header, and the open tag needs to
// have a newline after it. Otherwise, ignore it.
if ($stackPtr > 0) {
return $phpcsFile->numTokens;
}
$openTag = $stackPtr;
}
else {
if (count($possibleHeaders) > 1) {
// There are other PHP blocks before the file header.
$error = 'The file header must be the first content in the file';
$phpcsFile->addError($error, $openTag, 'HeaderPosition');
}
else {
// The first possible header was the file header block,
// so make sure it is the first content in the file.
if ($openTag !== 0) {
// Allow for hashbang lines.
$hashbang = false;
if ($tokens[$openTag - 1]['code'] === T_INLINE_HTML) {
$content = trim($tokens[$openTag - 1]['content']);
if (substr($content, 0, 2) === '#!') {
$hashbang = true;
}
}
if ($hashbang === false) {
$error = 'The file header must be the first content in the file';
$phpcsFile->addError($error, $openTag, 'HeaderPosition');
}
}
}
}
//end if
$this->processHeaderLines($phpcsFile, $possibleHeaders[$openTag]);
return $phpcsFile->numTokens;
}