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

[S1488] False Positives when Assigned Value Depends on the Variable Itself

    XMLWordPrintable

    Details

    • Type: False-Positive
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.17
    • Component/s: None
    • Labels:
      None

      Description

      We should not raise S1488 when the assigned value depends on the variable itself.

      An example of this is when shorthand assignment operators are used:

      function foo() {
          $a = "a";
          $a .= "b"; // Currently a false positive.
          return $a;
      }
      

      Why this happens?
      In the implementation of rule S1488 we do consider expressions of type AssignmentExpressionTree. However, assignments with shorthand operators (i.e., .=, +=, -=, ...) are also represented as AssignmentExpressionTree. We should differentiate between those and only consider assignments with the normal assignment operator in the rule implementation. This also aligns with how the Java and JS analyzers behave in this rule.

      However, this can also happen in cases like these:

      function foo() {
          $a = "a";
          $a = str_replace( 'a', 'b', $a ); // FP
          return $a;
      }
      
      function bar() {
          $a = "a";
          $a = "b" . $a; // FP
          return $a;
      }
      

      Reported on community.

        Attachments

          Activity

            People

            Assignee:
            karim.ouerghemmi Karim El Ouerghemmi
            Reporter:
            karim.ouerghemmi Karim El Ouerghemmi
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Due:
              Created:
              Updated:
              Resolved: