Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function register()

/**
* Processes this test, when one of its tokens is encountered.
* Tokens can get autoreplaced if statements don't contain an operator with a priority between and/or and &&/||
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
* @param int $stackPtr The position of the current token in the
Expand All @@ -75,7 +76,54 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$operator,
$replacements[$operator],
);
$phpcsFile->addError($error, $stackPtr, 'NotAllowed', $data);

$blackList = array(
T_EQUAL,
T_PLUS_EQUAL,
T_MINUS_EQUAL,
T_MUL_EQUAL,
T_POW_EQUAL,
T_DIV_EQUAL,
T_CONCAT_EQUAL,
T_MOD_EQUAL,
T_AND_EQUAL,
T_OR_EQUAL,
T_XOR_EQUAL,
T_SL_EQUAL,
T_SR_EQUAL,
T_LOGICAL_XOR,
T_COALESCE,
T_COALESCE_EQUAL,
T_INLINE_THEN,
T_INLINE_ELSE,
);

// Extend blacklist depending on which operator is being processed.
if ($tokens[$stackPtr]['code'] === T_LOGICAL_OR) {
$blackList[] = T_LOGICAL_XOR;
} else if ($tokens[$stackPtr]['code'] === T_LOGICAL_AND) {
$blackList[] = T_BOOLEAN_OR;
}

$start = $phpcsFile->findStartOfStatement($stackPtr);
$end = $phpcsFile->findEndOfStatement($stackPtr);

for ($index = $start; $index <= $end; ++$index) {
// Skip checking contents of grouped statements.
if ($tokens[$index]['code'] === T_OPEN_PARENTHESIS) {
$index = ($phpcsFile->findEndOfStatement($index + 1) + 1);
}

if (in_array($tokens[$index]['code'], $blackList, true)) {
$phpcsFile->addError($error, $stackPtr, 'NotAllowed', $data);
return;
}
}

$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotAllowed', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr, $replacements[$operator]);
}

}//end process()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,32 @@ if ($a xor $b) {

if ($a XOR $b) {
}

$c = $a and $b;
$c += $a and $b;
$c -= $a and $b;
$c *= $a and $b;
$c **= $a and $b;
$c /= $a and $b;
$c .= $a and $b;
$c %= $a and $b;
$c &= $a and $b;
$c |= $a and $b;
$c ^= $a and $b;
$c ?? $a and $b;
$c ??= $a and $b;
$c <<= $a and $b;
$c >>= $a and $b;

$c = ($a and $b);
$c = ($a and $c ?? ($c or $d));
$c = ($a and ($b ?? $c));
$foo = ($a and ($b ?? $c) ?? $d);
$foo = ($a and ($b ?? $c) or $d);
$foo = ($a and ($b ?? $c) or $d ?? $e);

$foo = ($a and $b || $c);
$foo = ($a or $b xor $c);
$foo = ($a and $b or $c);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ public function getErrorList()
5 => 1,
11 => 1,
17 => 2,
29 => 1,
30 => 1,
31 => 1,
32 => 1,
33 => 1,
34 => 1,
35 => 1,
36 => 1,
37 => 1,
38 => 1,
39 => 1,
40 => 1,
41 => 1,
42 => 1,
43 => 1,
45 => 1,
46 => 2,
47 => 1,
48 => 1,
49 => 2,
50 => 2,
52 => 1,
53 => 1,
54 => 2,
);

}//end getErrorList()
Expand Down
Loading