function EchoedStringsSniff::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
void
Overrides Sniff::process
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Standards/ Squiz/ Sniffs/ Strings/ EchoedStringsSniff.php, line 41
Class
Namespace
PHP_CodeSniffer\Standards\Squiz\Sniffs\StringsCode
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$firstContent = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
// If the first non-whitespace token is not an opening parenthesis, then we are not concerned.
if ($tokens[$firstContent]['code'] !== T_OPEN_PARENTHESIS) {
$phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no');
return;
}
$end = $phpcsFile->findNext([
T_SEMICOLON,
T_CLOSE_TAG,
], $stackPtr, null, false);
// If the token before the semicolon is not a closing parenthesis, then we are not concerned.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $end - 1, null, true);
if ($tokens[$prev]['code'] !== T_CLOSE_PARENTHESIS) {
$phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no');
return;
}
// If the parenthesis don't match, then we are not concerned.
if ($tokens[$firstContent]['parenthesis_closer'] !== $prev) {
$phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no');
return;
}
$phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'yes');
if ($phpcsFile->findNext(Tokens::$operators, $stackPtr, $end, false) === false) {
// There are no arithmetic operators in this.
$error = 'Echoed strings should not be bracketed';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'HasBracket');
if ($fix === true) {
$phpcsFile->fixer
->beginChangeset();
$phpcsFile->fixer
->replaceToken($firstContent, '');
if ($tokens[$firstContent - 1]['code'] !== T_WHITESPACE) {
$phpcsFile->fixer
->addContent($firstContent - 1, ' ');
}
$phpcsFile->fixer
->replaceToken($prev, '');
$phpcsFile->fixer
->endChangeset();
}
}
}