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;
/** @phpstan-assert-if-true !null $this->name */
public bool $hasName {
get => $this->name !== null;
}
}
Why is it reported? #
The @phpstan-assert, @phpstan-assert-if-true, and @phpstan-assert-if-false PHPDoc tags are not supported above a property declaration. They are not applied there, so they have no effect on type narrowing.
Assert tags describe what reading a property tells about other expressions. On PHP 8.4+, that logic lives in the get property hook, so the tags belong above the hook. A property without a get hook is plain storage, and reading it cannot narrow anything else.
When the property has a get hook, the error includes the tip “Put it above the get hook instead.”
How to fix it #
Move the tag above the get hook:
class Foo
{
public ?string $name = null;
- /** @phpstan-assert-if-true !null $this->name */
public bool $hasName {
+ /** @phpstan-assert-if-true !null $this->name */
get => $this->name !== null;
}
}
If the property has no hook, add a get hook that computes the value, or replace the property with a method that carries the tag:
class Foo
{
public ?string $name = null;
- /** @phpstan-assert-if-true !null $this->name */
- public bool $hasName = false;
+ /** @phpstan-assert-if-true !null $this->name */
+ public function hasName(): bool
+ {
+ return $this->name !== null;
+ }
}
How to ignore this error #
You can use the identifier assert.property to ignore this error using a comment:
// @phpstan-ignore assert.property
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.property
Rules that report this error #
- PHPStan\Rules\PhpDoc\PropertyAssertRule [1]