Error Identifier: return.noParent
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);
namespace App;
/**
* @return parent
*/
function returnParent()
{
// ...
}
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 function (not a method), or 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:
<?php declare(strict_types = 1);
namespace App;
-/**
- * @return parent
- */
-function returnParent()
+function returnParent(): SomeClass
{
// ...
}
If the code is in a class that should have a parent, add the extends clause:
<?php declare(strict_types = 1);
namespace App;
-class MyClass
+class MyClass 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]