Uploaded image for project: 'SonarPHP'
  1. SonarPHP
  2. SONARPHP-1119

Create new rule for Yoda conditions

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Rules
    • Labels:

      Description

      About Yoda conditions

      Yoda conditions is a programming style that places the constant portion of the expression on the left side of a conditional statement. See example below.

      Why

      Using Yoda conditions is promoted as best-practice by popular frameworks as Symfony and Wordpress to prevent developers from accidental assignments inside the condition statements.

      if ($foo = 'bar') {} // It could be an accidental assignment
      if ('bar' = $foo) {} // Yoda conditions - It will fail, preventing the accidental assignment
      

      What

      We should have a rule to raise an issue when the developer does not apply this programming style.
      This rule should not belong to the default quality profile.
      The Yoda conditions programming style applies only to the following operators: ==, !=, ===, !==

      Example

      if ($foo == 'bar') {} // Noncompliant
      
      if ('bar' == $foo) {} // Compliant
      
      if ($foo <= 'bar') {} // Compliant
      

       

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              roberto.orlandi Roberto Orlandi
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: