Skip to content

DynamicInvocationViaScopeResolution ​

warning on by default quick-fix

Group: Code style · PhpStorm name: DynamicInvocationViaScopeResolutionInspection

Calling an instance (non-static) method through :: (self::run(), static::run(), $obj::run()) hides that the call is really an instance call and breaks in static contexts. Such calls should use ->.

Example ​

php
<?php

class Engine
{
    public function start() {}
}

class Car extends Engine
{
    public function start() {}
    public function honk() {}
    public static function build() {}

    public static function factory()
    {
        self::honk();
        self::build();
    }

    public function drive($speed)
    {
        static::honk();
        Car::start();
        self::honk();
        parent::start();
        $cb = function () { self::honk(); };
    }
}

class SportsCar extends Car
{
    public function start()
    {
        Car::start();
        Engine::start();
    }
}

$car = new Car();
$car::honk();
$car::build();
php
<?php

class Engine
{
    public function start() {}
}

class Car extends Engine
{
    public function start() {}
    public function honk() {}
    public static function build() {}

    public static function factory()
    {
        self::honk();
        self::build();
    }

    public function drive($speed)
    {
        $this->honk();
        $this->start();
        $this->honk();
        parent::start();
        $cb = function () { self::honk(); };
    }
}

class SportsCar extends Car
{
    public function start()
    {
        Car::start();
        Engine::start();
    }
}

$car = new Car();
$car->honk();
$car::build();

Reported:

  • line 16: Call 'honk' on an instance with '->' instead of '::'.
  • line 22: Call 'honk' with '$this->' instead of '::'.
  • line 23: Call 'start' with '$this->' instead of '::'.
  • line 24: Call 'honk' with '$this->' instead of '::'.
  • line 40: Call 'honk' on an instance with '->' instead of '::'.

Options ​

OptionTypeDefaultEffect

Configure ​

In custos.json:

json
{
  "rules": {
    "DynamicInvocationViaScopeResolution": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore DynamicInvocationViaScopeResolution

/**
 * @noinspection DynamicInvocationViaScopeResolutionInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.