Coding-standard: SlevomatCodingStandard.PHP.UselessParentheses removes also usefull parentheses

Created on 28 Aug 2018  路  8Comments  路  Source: slevomat/coding-standard

UselessParentheses currently removes parentheses around really simple expressions, but also for all conditions in ternary expressions. Since these conditions can be more complex (multiple lines, function calls, multiple epressions joined with operators etc.) it can be much less readable without the parentheses (although they are technically still not needed).

So I think there should be an option to evaluate these as useful leaving only single "simple" expressions as useless and probably also considering these expressions with unary operators (such as !) useless as well.

Fo example:

($foo) // still useless
(!$foo) // still useless
($foo && $bar) // useful
($foo !== $bar) // useful

(of course inplace of $foo and $bar would be more complicated expressions generally)

Enhancement

Most helpful comment

One could argue that if your ternary expressions are so complicated that you need parentheses to make them readable, you should probably refactor them into something more readable (e.g. simple method calls that actually tell you what the check is about).

All 8 comments

One could argue that if your ternary expressions are so complicated that you need parentheses to make them readable, you should probably refactor them into something more readable (e.g. simple method calls that actually tell you what the check is about).

It does not need to be necessary complex as in difficult to understand, but also "visually" complex, which can be even a function call with multiple parameters. Extracting it into a variable or method etc. is always possible, but sometimes not really practical and from readability point of view can make things even worse.

Other case that parentheses might be useful:

($foo['bar'] ?? false) ? true : false

I consider usefull parenthesis in instanceof case.

$abc !== null && !($abc instanceof SomeClass)

Parentheses with exclamation mark are not mark as useless.

I consider usefull parenthesis in instanceof case.

I disagree, those are useless.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ilyes512 picture Ilyes512  路  5Comments

grogy picture grogy  路  5Comments

kukulich picture kukulich  路  7Comments

carusogabriel picture carusogabriel  路  5Comments

Stadly picture Stadly  路  6Comments