slevomat / coding-standard

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs
MIT License
1.39k stars 171 forks source link

SlevomatCodingStandard.PHP.UselessParentheses.UselessParentheses - false positive with modulo operator #1678

Open rvock opened 4 months ago

rvock commented 4 months ago
<?php
declare(strict_types = 1);

var_dump(!(12 % 12));
var_dump(!12 % 12);

Output:

bool(true)
int(0)

Here I get an error, that the first parentheses are useless. But that's not the case, because the output is different, if I remove them.

The ! operator has a higher precedence than the module % operator: https://www.php.net/manual/en/language.operators.precedence.php

bkdotcom commented 4 months ago

See also #1674 (bit-shift operator) & #1672 (string concat & ternary operator)

This sniff doesn't seem to take context into account?
3 * (2 + 1) doesn't trigger UselessParentheses though ¯\_(ツ)_/¯