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 Sponsor
{
public ?string $sponsorName = null;
public ?string $sponsorUrl = null;
public bool $sponsored {
/** @phpstan-assert-if-true !null $this->sponsorName */
get => $this->sponsorName !== null;
}
}
class OtherTags extends Sponsor
{
public bool $sponsored {
/** @phpstan-assert-if-true !null $this->sponsorUrl */
get => $this->sponsorUrl !== null;
}
}
Why is it reported? #
Assert tags above a get property hook narrow types when the property is read. Code that reads $sponsor->sponsored relies on the tags of Sponsor::$sponsored. When true is read, $sponsor->sponsorName is treated as string. When the object is actually an OtherTags, the overriding property must keep that promise. Otherwise the narrowed type is wrong and code that depends on it may crash.
The get hook of an overriding property has to narrow each expression at least as much as the overridden hook does in every situation:
- after reading the property, for
@phpstan-assert - when it is
true, for@phpstan-assert-if-true - when it is
false, for@phpstan-assert-if-false
Conditional tags also narrow the opposite branch to the negated type. In the example above, $this->sponsorName is narrowed to null when the parent property is false. An overriding hook cannot change the type of a conditional tag in either direction, unless it is never true or never false.
Assert tags are inherited only when the overriding get hook has none of its own. In the example above, OtherTags adds a tag about $this->sponsorUrl, so the parent’s tag about $this->sponsorName is no longer inherited:
Property OtherTags::$sponsored does not narrow $this->sponsorName when it is true, but overridden property Sponsor::$sponsored narrows it to mixed~null.
Adding assertions about other expressions is allowed.
How to fix it #
Repeat the parent’s tags in the overriding hook:
class OtherTags extends Sponsor
{
public bool $sponsored {
- /** @phpstan-assert-if-true !null $this->sponsorUrl */
- get => $this->sponsorUrl !== null;
+ /**
+ * @phpstan-assert-if-true !null $this->sponsorName
+ * @phpstan-assert-if-true !null $this->sponsorUrl
+ */
+ get => $this->sponsorName !== null && $this->sponsorUrl !== null;
}
}
If the overriding hook should keep the parent’s behaviour, remove its tags so the parent’s tags are inherited:
class OtherTags extends Sponsor
{
public bool $sponsored {
- /** @phpstan-assert-if-true !null $this->sponsorUrl */
- get => $this->sponsorUrl !== null;
+ get => $this->sponsorName !== null;
}
}
How to ignore this error #
You can use the identifier property.childAssertType to ignore this error using a comment:
// @phpstan-ignore property.childAssertType
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: property.childAssertType