-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/Standards/Generic/Sniffs/CodeAnalysis/MixedBooleanOperatorSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
/** | ||
* Detects mixed '&&' and '||' within a single expression, without making | ||
* precedence explicit using parentheses. | ||
* | ||
* <code> | ||
* $var = true && true || true; | ||
* </code> | ||
* | ||
* @author Tim Duesterhus <duesterhus@woltlab.com> | ||
* @copyright 2021 WoltLab GmbH. | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
class MixedBooleanOperatorSniff implements Sniff | ||
{ | ||
|
||
|
||
/** | ||
* Registers the tokens that this sniff wants to listen for. | ||
* | ||
* @return int[] | ||
*/ | ||
public function register() | ||
{ | ||
return [ | ||
T_BOOLEAN_OR, | ||
T_BOOLEAN_AND, | ||
]; | ||
|
||
}//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(); | ||
$token = $tokens[$stackPtr]; | ||
|
||
$start = $phpcsFile->findStartOfStatement($stackPtr); | ||
|
||
if ($token['code'] === T_BOOLEAN_AND) { | ||
$search = T_BOOLEAN_OR; | ||
} else if ($token['code'] === T_BOOLEAN_OR) { | ||
$search = T_BOOLEAN_AND; | ||
} else { | ||
throw new \LogicException('Unreachable'); | ||
} | ||
|
||
while (true) { | ||
$previous = $phpcsFile->findPrevious( | ||
[ | ||
$search, | ||
T_OPEN_PARENTHESIS, | ||
T_OPEN_SQUARE_BRACKET, | ||
T_CLOSE_PARENTHESIS, | ||
T_CLOSE_SQUARE_BRACKET, | ||
], | ||
$stackPtr, | ||
$start | ||
); | ||
|
||
if ($previous === false) { | ||
break; | ||
} | ||
|
||
if ($tokens[$previous]['code'] === T_OPEN_PARENTHESIS | ||
|| $tokens[$previous]['code'] === T_OPEN_SQUARE_BRACKET | ||
) { | ||
// We halt if we reach the opening parens / bracket of the boolean operator. | ||
return; | ||
} else if ($tokens[$previous]['code'] === T_CLOSE_PARENTHESIS) { | ||
// We skip the content of nested parens. | ||
$stackPtr = ($tokens[$previous]['parenthesis_opener'] - 1); | ||
} else if ($tokens[$previous]['code'] === T_CLOSE_SQUARE_BRACKET) { | ||
// We skip the content of nested brackets. | ||
$stackPtr = ($tokens[$previous]['bracket_opener'] - 1); | ||
} else if ($tokens[$previous]['code'] === $search) { | ||
// We reached a mismatching operator, thus we must report the error. | ||
$error = "Mixed '&&' and '||' within an expression without using parentheses."; | ||
$phpcsFile->addError($error, $stackPtr, 'MissingParentheses', []); | ||
return; | ||
} else { | ||
throw new \LogicException('Unreachable'); | ||
} | ||
}//end while | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
41 changes: 41 additions & 0 deletions
41
src/Standards/Generic/Tests/CodeAnalysis/MixedBooleanOperatorUnitTest.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
if (true && true || true); | ||
if ((true && true) || true); | ||
if (true && (true || true)); | ||
|
||
$var = true && true || true; | ||
$var = (true && true) || true; | ||
$var = true && (true || true); | ||
|
||
$complex = true && (true || true) && true; | ||
$complex = true && (true || true) || true; | ||
|
||
if ( | ||
true | ||
&& true | ||
|| true | ||
); | ||
|
||
if ( | ||
true | ||
&& ( | ||
true | ||
|| true | ||
) | ||
); | ||
|
||
if (true && foo(true || true)); | ||
if (true && foo(true && true || true)); | ||
if (true && $foo[true || true]); | ||
if (true && $foo[true && true || true]); | ||
|
||
if (true && foo(true) || true); | ||
if (true && $foo[true] || true); | ||
if (true && foo($foo[true]) || true); | ||
|
||
$foo[] = true && true || false; | ||
|
||
foo([true && true || false]); | ||
|
||
if (true && true || true && true); |
61 changes: 61 additions & 0 deletions
61
src/Standards/Generic/Tests/CodeAnalysis/MixedBooleanOperatorUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Unit test class for the MixedBooleanOperator sniff. | ||
* | ||
* @author Tim Duesterhus <duesterhus@woltlab.com> | ||
* @copyright 2021 WoltLab GmbH. | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\Generic\Tests\CodeAnalysis; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class MixedBooleanOperatorUnitTest extends AbstractSniffUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of errors that should occur on that line. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getErrorList() | ||
{ | ||
return [ | ||
3 => 1, | ||
7 => 1, | ||
12 => 1, | ||
17 => 1, | ||
29 => 1, | ||
31 => 1, | ||
33 => 1, | ||
34 => 1, | ||
35 => 1, | ||
37 => 1, | ||
39 => 1, | ||
41 => 2, | ||
]; | ||
|
||
}//end getErrorList() | ||
|
||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of warnings that should occur on that line. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getWarningList() | ||
{ | ||
return []; | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class |