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 Bar {}
class Foo
{
public Bar $bar;
public function __construct()
{
$this->bar = new Bar();
}
}
function test(): void
{
$foo = new Foo();
$foo->bar = new Bar();
if (empty($foo->bar)) {
echo 'empty';
}
}
Why is it reported? #
The property used inside empty() has a type that PHPStan can fully evaluate for emptiness. When the property type is always falsy or never falsy, the empty() check is either always true or always false, indicating a logic error or a check that should be written more explicitly.
In the example above, $foo->bar is typed as Bar, which is an object and can never be falsy. The empty() call on it always returns false.
How to fix it #
Remove the redundant empty() check since an object value is never falsy:
function test(): void
{
$foo = new Foo();
$foo->bar = new Bar();
- if (empty($foo->bar)) {
- echo 'empty';
- }
+ echo $foo->bar::class;
}
If the property can legitimately be nullable, adjust the type to reflect that:
class Foo
{
- public Bar $bar;
+ public ?Bar $bar;
public function __construct()
{
- $this->bar = new Bar();
+ $this->bar = null;
}
}
How to ignore this error #
You can use the identifier empty.property to ignore this error using a comment:
// @phpstan-ignore empty.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: empty.property