djoos / Symfony-coding-standard

Development repository for the Symfony coding standard
MIT License
401 stars 102 forks source link

Yoda comparation not complaining for this case #113

Closed tatitati closed 4 years ago

tatitati commented 6 years ago

I know that YODA comparation tries to avoid a bad assignation to a variable. But also is an standard saying that the expected value should be on the left (first) when comparing.

This said, in my case I can do this without simple variables and phpcs doesn't complain:

if ($e->getErrorCode() == 2006) {
       ....
}

I think in this case also should complain. This makes the code more standard having all the expected values on the left.

straticJeff commented 6 years ago

I think the answer is in your post - Yoda style helps avoid unintended dangerous variable assignments. But your example is a comparison against a function call, which you cannot accidentally assign in PHP. The Yoda style rule is there to help devs proactively defend against typos that are difficult to spot and debug, but as your example can't be dangerous, I don't think it should apply in this case.

djoos commented 4 years ago

Closing this one off for now - happy to revisit the discussion if needed in the future!

outcomer commented 1 year ago

@djoos Hey! What about this? if ($args['type'] == 1) {....}

YODA rule also would not trigger in this case due to shiff logic if (T_VARIABLE === $tokens[$comparison - 2]['code'] && T_VARIABLE !== $tokens[$comparison + 2]['code'] ) {

For this if ($args == 1) {....} YODA rule WILL works.