class RequireExplicitBooleanOperatorPrecedenceSniff
Hierarchy
- class \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\RequireExplicitBooleanOperatorPrecedenceSniff implements \PHP_CodeSniffer\Sniffs\Sniff
Expanded class hierarchy of RequireExplicitBooleanOperatorPrecedenceSniff
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Standards/ Generic/ Sniffs/ CodeAnalysis/ RequireExplicitBooleanOperatorPrecedenceSniff.php, line 36
Namespace
PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysisView source
class RequireExplicitBooleanOperatorPrecedenceSniff implements Sniff {
/**
* Array of tokens this test searches for to find either a boolean
* operator or the start of the current (sub-)expression. Used for
* performance optimization purposes.
*
* @var array<int|string>
*/
private $searchTargets = [];
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register() {
$this->searchTargets = Tokens::$booleanOperators;
$this->searchTargets[T_INLINE_THEN] = T_INLINE_THEN;
$this->searchTargets[T_INLINE_ELSE] = T_INLINE_ELSE;
return Tokens::$booleanOperators;
}
//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$start = $phpcsFile->findStartOfStatement($stackPtr);
$previous = $phpcsFile->findPrevious($this->searchTargets, $stackPtr - 1, $start, false, null, true);
if ($previous === false) {
// No token found.
return;
}
if ($tokens[$previous]['code'] === $tokens[$stackPtr]['code']) {
// Identical operator found.
return;
}
if (in_array($tokens[$previous]['code'], [
T_INLINE_THEN,
T_INLINE_ELSE,
], true) === true) {
// Beginning of the expression found for the ternary conditional operator.
return;
}
// We found a mismatching operator, thus we must report the error.
$error = 'Mixing different binary boolean operators within an expression';
$error .= ' without using parentheses to clarify precedence is not allowed.';
$phpcsFile->addError($error, $stackPtr, 'MissingParentheses');
}
//end process()
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
RequireExplicitBooleanOperatorPrecedenceSniff::$searchTargets | private | property | Array of tokens this test searches for to find either a boolean operator or the start of the current (sub-)expression. Used for performance optimization purposes. |
|
RequireExplicitBooleanOperatorPrecedenceSniff::process | public | function | Processes this test, when one of its tokens is encountered. | Overrides Sniff::process |
RequireExplicitBooleanOperatorPrecedenceSniff::register | public | function | Returns an array of tokens this test wants to listen for. | Overrides Sniff::register |