Add rule to check for: if ($var == 'Literal'...)

I do not see this being flagged in PHP.
if ($querytype == “C”) {…

I think a rule should be added to flag (as at least an Info or bad style) this construct:
if ($var == ‘literal’ …)
and suggest
if (‘literal’ == $var… )

(and the double quote variant as well).

As a rule, the literal should be on the left hand side of “==” in most (all?) languages. Enforcing this rule helps to develop a style that avoid these errors:
if (‘literal’ = $foobar) //wont compile or run, but gets written
if ($foobar = ‘literal’) //dreadful bug to track
both the above should be flagged, and the developer writing that style should be flogged.

Hello @graeme_sonarqb,

Welcome back to our community!

We will discuss your suggestion in the team, and come back to you with an answer.

In the while, we thank you for reaching us, and wish you a good week-end!


Hello @graeme_sonarqb,

Based on your suggestion, we will create and implement a new rule to ensure the usage of the literal at the left hand side of the equality operators.

I have created the ticket SONARPHP-1119 so you can follow the progress.

Thank you again for being an active member of our community.


Thank you, I think this rule will be a benefit for all languages that support this: if (foo == ‘bar’) and its != variant.