On Jan 27, 2013, at 5:18 PM, Sherbrow wrote:
> I came across an if statement with some complex checking - quite long too - and the Control Structure section of PSR-2 doesn't give specific information on this case.
>
> My question would be, where does the PHP-FIG stand on this matter ? Or on what do they disagree ? I can think of several alternatives :
>
> if ($var1 && $var2 || ($varX % $varY == 1)) {
> // ...
>
> if ($var1 &&
> $var2 ||
> ($varX % $varY == 1)) {
> // ...
>
> if (
> $var1
> && $var2
> || ($varX % $varY == 1)
> ) {
> // ...
>
> These are the combinations of the nicest and ugliest things I could think of right now. The last one seems the nicest to me, if the first one isn't possible.
>
> Thank you for your involvement.
There is no existing PSR that expresses a recommendation on this matter.
It my personal opinion, as I have stated earlier elsewhere, that this is a classic candidate for "extract to explaining variable." Instead of leaving the entire condition set inside the parenthesis, extract it to a variable and then use the variable as the condition.
For example:
$cond = $var1
&& $var2 || ($varX % $varY == 1);
if ($cond) {
// ...
}
Hope that helps.
-- pmj