Skip to content

CS: add the RequireExplicitBooleanOperatorPrecedence sniff to PHPCS native ruleset #326

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

Merged
merged 1 commit into from
Feb 4, 2024
Merged
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
3 changes: 3 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
<!-- Do not allow unreachable code. -->
<rule ref="Squiz.PHP.NonExecutableCode"/>

<!-- Do not allow ambiguous conditions. -->
<rule ref="Generic.CodeAnalysis.RequireExplicitBooleanOperatorPrecedence"/>

<!-- The testing bootstrap file uses string concats to stop IDEs seeing the class aliases -->
<rule ref="Generic.Strings.UnnecessaryStringConcat">
<exclude-pattern>tests/bootstrap\.php</exclude-pattern>
Expand Down
8 changes: 4 additions & 4 deletions src/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public function register()
public function process(File $phpcsFile, $stackPtr)
{
$scopeModifier = $phpcsFile->getMethodProperties($stackPtr)['scope'];
if ($scopeModifier === 'protected'
&& $this->minimumVisibility === 'public'
|| $scopeModifier === 'private'
&& ($this->minimumVisibility === 'public' || $this->minimumVisibility === 'protected')
if (($scopeModifier === 'protected'
&& $this->minimumVisibility === 'public')
|| ($scopeModifier === 'private'
&& ($this->minimumVisibility === 'public' || $this->minimumVisibility === 'protected'))
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)

// Remove the newline after the docblock, and any entirely
// empty lines before the member var.
if ($tokens[$i]['code'] === T_WHITESPACE
&& $tokens[$i]['line'] === $tokens[$prev]['line']
if (($tokens[$i]['code'] === T_WHITESPACE
&& $tokens[$i]['line'] === $tokens[$prev]['line'])
|| ($tokens[$i]['column'] === 1
&& $tokens[$i]['line'] !== $tokens[($i + 1)]['line'])
) {
Expand Down
2 changes: 1 addition & 1 deletion src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@ protected function tokenize($string)
|| (stripos($newContent, '0b') === 0 && bindec(str_replace('_', '', $newContent)) > PHP_INT_MAX)
|| (stripos($newContent, '0o') === 0 && octdec(str_replace('_', '', $newContent)) > PHP_INT_MAX)
|| (stripos($newContent, '0x') !== 0
&& stripos($newContent, 'e') !== false || strpos($newContent, '.') !== false)
&& (stripos($newContent, 'e') !== false || strpos($newContent, '.') !== false))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless it's too early in the morning and I misread this, it even caught a bug in here 🙌

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so too when I made the change, but there is a test for this code and it passed before and after 🤷🏻‍♀️

|| (strpos($newContent, '0') === 0 && stripos($newContent, '0x') !== 0
&& stripos($newContent, '0b') !== 0 && octdec(str_replace('_', '', $newContent)) > PHP_INT_MAX)
|| (strpos($newContent, '0') !== 0 && str_replace('_', '', $newContent) > PHP_INT_MAX))
Expand Down