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 create(): parent
{
}
}
Why is it reported? #
The parent keyword in PHP refers to the parent class of the current class. It can only be used inside a class that extends another class. When parent is used as a return type in a class that does not extend any other class, it has no meaning because there is no parent class to refer to.
How to fix it #
Replace parent with the actual class name that was intended:
class Foo
{
- public function create(): parent
+ public function create(): self
{
}
}
If the class should have a parent, add the extends clause:
-class Foo
+class Foo extends BaseClass
{
public function create(): parent
{
+ return new BaseClass();
}
}
How to ignore this error #
You can use the identifier return.noParent to ignore this error using a comment:
// @phpstan-ignore return.noParent
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: return.noParent
Rules that report this error #
- PHPStan\Rules\Functions\ExistingClassesInArrowFunctionTypehintsRule [1]
- PHPStan\Rules\Functions\ExistingClassesInClosureTypehintsRule [1]
- PHPStan\Rules\Functions\ExistingClassesInTypehintsRule [1]
- PHPStan\Rules\Methods\ExistingClassesInTypehintsRule [1]
- PHPStan\Rules\Properties\ExistingClassesInPropertyHookTypehintsRule [1]