Are parentheses required in PSR-2 PHP syntax?

Question: Do parentheses in PSR-2 need PHP ternary syntax?

Looking for what (if any of) Ternary syntax syntax is compatible with PSR-2 - I also need to provide documentation or a link to some permissions:

$error = ($error_status) ? 'Error' : 'No Error';

OR

$error = $error_status ? 'Error' : 'No Error';


Note: php.net it shows the parenthesis syntax, but I could not find it in any of the official PSR-2 docs.


Conclusion

If this is not the PSR-2 standard, how is this the most common agreement?

+6
source share
4 answers

The PSR-2 standard specifically omits any opinion on operators:

There are many elements of style and practice deliberately omitted by this guide. These include, but are not limited to: ... Operators and purpose

Since brackets are used to group expressions, your example does not make much sense:

 $error = ($error_status) ? 'Error' : 'No Error'; 

It makes no sense to surround one variable in parentheses. A more complex condition may benefit from parentheses, but in most cases they will be read-only.

A more general template would be to always surround the entire three-dimensional expression:

 $error = ($error_status ? 'Error' : 'No Error'); 

The main motivation for this is that the ternary operator in PHP has a rather inconvenient associativity and priority, so using it in complex expressions often gives unexpected / useless results.

The usual case is string concatenation, for example:

 $error = 'Status: ' . $error_status ? 'Error' : 'No Error'; 

Here, the concatenation operator ( . ) Is actually evaluated before the ternary operator, so the condition is always a non-empty string (the beginning of 'Status: ' ), and you will always get the string Error' as the result.

To prevent this, parentheses are required:

 $error = 'Status: ' . ($error_status ? 'Error' : 'No Error'); 

A similar situation exists when the “triple expressions” for forming the equivalent of the if-elseif chain, like an error at the beginning of the PHP history, mean that several ternary operators are evaluated sequentially from left to right, and do not reduce the entire false branch when the condition is true.

An example from the PHP manual explains this more clearly:

 // on first glance, the following appears to output 'true' echo (true?'true':false?'t':'f'); // however, the actual output of the above is 't' // this is because ternary expressions are evaluated from left to right // the following is a more obvious version of the same code as above echo ((true ? 'true' : false) ? 't' : 'f'); // here, you can see that the first expression is evaluated to 'true', which // in turn evaluates to (bool)true, thus returning the true branch of the // second ternary expression. 
+12
source

The general agreement is always simplified. The PSR standard is such that

 $error = $error_status ? 'Error' : 'No Error'; 

Seems cleaner than parentheses.

If you need clearer readability, the PSR-2 standard goes into:

 if ($error_status) { $error = 'Error'; else { $error = 'No Error'; } 

Everything. PSR is the standard to better understand our code, when you write code as you provide, you go deeper in simplification, and there is no restriction on your imagination, just don't exceed the PSR rules.

Use PHP Sniffer to test your code for PSR1 and PSR2 rules.

Code sniffer

+2
source

Unless explicitly stated, there is no standard. Or it works.

+1
source

It is important to remember that the PSR-2 claims that strings MUST be longer than 80 characters .
Triple syntax can be quite long sometimes, so I think we have a missing recommendation for a very common kind of code. What I'm doing now is indentation, like this:

 $stuff = $count > MyLongNamespace\MyLongClassName->get('count') ? 'yikes this seems to be some large stuff' : 'erm this is rather small stuff'; 
+1
source

Source: https://habr.com/ru/post/976793/


All Articles