-
-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add autofix support for Squiz.Operators.ValidLogicalOperatorsSniff #613
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,59 @@ public function process(File $phpcsFile, $stackPtr) | |
$operator, | ||
$replacements[$operator], | ||
]; | ||
$phpcsFile->addError($error, $stackPtr, 'NotAllowed', $data); | ||
|
||
// Based on https://www.php.net/manual/en/language.operators.precedence.php | ||
// It contains the tokens that can result in different code behaviour when replacing "AND / OR" with "&& / ||". | ||
// So we need to check for them to decide whether it's a fixable or non-fixable error. | ||
$blockList = [ | ||
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_COALESCE, | ||
T_COALESCE_EQUAL, | ||
T_INLINE_THEN, | ||
T_INLINE_ELSE, | ||
T_YIELD, | ||
T_YIELD_FROM, | ||
T_PRINT, | ||
]; | ||
|
||
// Extend blocklist depending on which operator is being processed. | ||
if ($tokens[$stackPtr]['code'] === T_LOGICAL_OR) { | ||
$blockList[] = T_LOGICAL_XOR; | ||
} else if ($tokens[$stackPtr]['code'] === T_LOGICAL_AND) { | ||
$blockList[] = 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); | ||
} | ||
Comment on lines
+101
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any particular reason why |
||
|
||
if (in_array($tokens[$index]['code'], $blockList, true) === true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance nitpick: please set up the blocklist to allow for using |
||
$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() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,33 @@ 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); | ||
print $a and $b; | ||
yield $a and $b; | ||
?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this is a fixer, which could potentially lead to a change in behaviour in an application, I'd like to see a lot more variation in the tests to verify that the determination whether something is fixable is done correctly. The fast majority of the above tests are quite simple statements, while real life code gets a lot more complex and varied. Some examples of syntaxes I'm missing in the tests:
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
if ($value === TRUE || $other === FALSE) { | ||
} | ||
|
||
if ($value === TRUE || $other === FALSE) { | ||
} | ||
|
||
if ($value === TRUE && $other === FALSE) { | ||
} | ||
|
||
if ($value === TRUE && $other === FALSE) { | ||
} | ||
|
||
if ($one === TRUE && ($two === TRUE || $three === TRUE)) { | ||
} | ||
|
||
if ($one === TRUE && ($two === TRUE || $three === TRUE)) { | ||
} | ||
|
||
if ($a ^ $b) { | ||
} | ||
|
||
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 && $b); | ||
$c = ($a and $c ?? ($c || $d)); | ||
$c = ($a && ($b ?? $c)); | ||
$foo = ($a and ($b ?? $c) ?? $d); | ||
$foo = ($a && ($b ?? $c) || $d); | ||
$foo = ($a and ($b ?? $c) or $d ?? $e); | ||
|
||
$foo = ($a and $b || $c); | ||
$foo = ($a or $b xor $c); | ||
$foo = ($a && $b || $c); | ||
print $a and $b; | ||
yield $a and $b; | ||
?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason not to use the
Tokens::$assignmentTokens
token array when building this list ?