Error Identifier: method.visibility
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
{
public function doFoo(): void
{
}
}
class Bar extends Foo
{
private function doFoo(): void
{
}
}
Why is it reported? #
An overriding method has a more restrictive visibility than the parent method. PHP enforces that overriding methods must not reduce visibility: a public method cannot be overridden with protected or private, and a protected method cannot be overridden with private.
This is a requirement of the Liskov Substitution Principle and is enforced by PHP at runtime with a fatal error.
How to fix it #
Match or widen the visibility of the overriding method:
<?php declare(strict_types = 1);
class Bar extends Foo
{
- private function doFoo(): void
+ public function doFoo(): void
{
}
}
Non-ignorable error #
This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.