Error Identifier: assert.unknownExpr
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
{
/**
* @phpstan-assert string $this->barProp
*/
public function doBar(mixed $a): bool
{
return is_string($a);
}
}
Why is it reported? #
The @phpstan-assert PHPDoc tag references a property or expression that does not exist on the class. PHPStan cannot verify the assertion because the target expression cannot be resolved. In the example above, $this->barProp does not exist on the class Foo.
How to fix it #
Reference an existing property or parameter in the assert tag:
<?php declare(strict_types = 1);
class Foo
{
+ public mixed $barProp;
+
/**
* @phpstan-assert string $this->barProp
*/
public function doBar(mixed $a): bool
{
return is_string($a);
}
}
Or fix the assert tag to reference the correct expression:
<?php declare(strict_types = 1);
class Foo
{
/**
- * @phpstan-assert string $this->barProp
+ * @phpstan-assert string $a
*/
public function doBar(mixed $a): bool
{
return is_string($a);
}
}
How to ignore this error #
You can use the identifier assert.unknownExpr to ignore this error using a comment:
// @phpstan-ignore assert.unknownExpr
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.unknownExpr