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 ?string $name = null;
public bool $hasName {
get => $this->name !== null;
/** @phpstan-assert-if-true !null $this->name */
set {
$this->name = $value ? 'yes' : null;
}
}
}
Why is it reported? #
The @phpstan-assert, @phpstan-assert-if-true, and @phpstan-assert-if-false PHPDoc tags are not supported above a set property hook. A set hook runs when a value is assigned to the property and returns void. There is no result that could be checked in a condition, so the tags have no effect.
Assert tags on property hooks narrow types when the property is read, so they are only supported above the get hook.
How to fix it #
Move the tag above the get hook:
public bool $hasName {
+ /** @phpstan-assert-if-true !null $this->name */
get => $this->name !== null;
- /** @phpstan-assert-if-true !null $this->name */
set {
$this->name = $value ? 'yes' : null;
}
}
If the tag was meant to describe the effect of the assignment, remove it. PHPStan does not narrow types based on what a set hook does.
How to ignore this error #
You can use the identifier assert.setHook to ignore this error using a comment:
// @phpstan-ignore assert.setHook
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: assert.setHook
Rules that report this error #
- PHPStan\Rules\PhpDoc\PropertyHookAssertRule [1]