Error Identifier: callable.inaccessibleMethod
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
class Foo
{
private function secretMethod(): void
{
}
}
function doFoo(): void
{
$callable = [new Foo(), 'secretMethod'];
$callable();
}
Why is it reported? #
A callable references a class method that is not accessible from the current scope. The method is either private or protected, and the code attempting to call it is outside the allowed visibility. Invoking such a callable will result in a runtime error.
In the example above, secretMethod() is private in Foo, but the callable is created and invoked from the doFoo() function which is outside the class and has no access to the private method.
How to fix it #
Change the method visibility to public if it is intended to be called externally:
<?php declare(strict_types = 1);
class Foo
{
- private function secretMethod(): void
+ public function secretMethod(): void
{
}
}
function doFoo(): void
{
$callable = [new Foo(), 'secretMethod'];
$callable();
}
Or provide a public method that delegates to the private one:
<?php declare(strict_types = 1);
class Foo
{
private function secretMethod(): void
{
}
+ public function run(): void
+ {
+ $this->secretMethod();
+ }
}
function doFoo(): void
{
- $callable = [new Foo(), 'secretMethod'];
- $callable();
+ (new Foo())->run();
}
How to ignore this error #
You can use the identifier callable.inaccessibleMethod to ignore this error using a comment:
// @phpstan-ignore callable.inaccessibleMethod
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: callable.inaccessibleMethod
Rules that report this error #
- PHPStan\Rules\Functions\CallCallablesRule [1]