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

FP on anonymous function for "$this should not be used in a static context"

    XMLWordPrintable

    Details

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

      Description

      The implementation of RSPEC-2014 ("$this" should not be used in a static context) should not raise issues inside anonymous functions. for example:

      class A {
          public $field;
          public function __construct(string $name) {
              $this->field = $name;
          }
          public static function calling_anonymous_function() {
              $fn = function() { return $this->field; }; // FP
              return $fn->call(new A('Hello'));
          }
      }
      
      print(A::calling_anonymous_function());
      

      See also a very good description of the problem.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              karim.ouerghemmi Karim El Ouerghemmi
              Reporter:
              pierre-yves.nicolas Pierre-Yves Nicolas
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Due:
                Created:
                Updated:
                Resolved: