3
* Squiz_Sniffs_PHP_DisallowBooleanStatementSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12
* @link http://pear.php.net/package/PHP_CodeSniffer
16
* Squiz_Sniffs_PHP_DisallowBooleanStatementSniff.
18
* Ensures that boolean operators are only used inside control structure conditions.
21
* @package PHP_CodeSniffer
22
* @author Greg Sherwood <gsherwood@squiz.net>
23
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
24
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25
* @version Release: 1.5.3
26
* @link http://pear.php.net/package/PHP_CodeSniffer
28
class Squiz_Sniffs_PHP_DisallowBooleanStatementSniff implements PHP_CodeSniffer_Sniff
33
* Returns an array of tokens this test wants to listen for.
37
public function register()
39
return PHP_CodeSniffer_Tokens::$booleanOperators;
45
* Processes this test, when one of its tokens is encountered.
47
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
48
* @param int $stackPtr The position of the current token
49
* in the stack passed in $tokens.
53
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
55
$tokens = $phpcsFile->getTokens();
56
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
58
foreach ($tokens[$stackPtr]['nested_parenthesis'] as $open => $close) {
59
if (isset($tokens[$open]['parenthesis_owner']) === true) {
60
// Any owner means we are not just a simple statement.
66
$error = 'Boolean operators are not allowed outside of control structure conditions';
67
$phpcsFile->addError($error, $stackPtr, 'Found');