Error Identifier: phpstan.unknownExpectation
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);
use function PHPStan\Testing\assertType;
$type = rand(0, 1) ? 'int' : 'string';
assertType($type, $value);
Why is it reported? #
PHPStan’s test assertion functions (assertType, assertNativeType, assertSuperType, assertVariableCertainty) require the expected type to be a literal string so that the expectation is unambiguous. When the first argument is not a constant literal string, PHPStan cannot determine what type was expected.
This also applies to assertVariableCertainty, where the first argument must be a static call to TrinaryLogic::createYes(), TrinaryLogic::createMaybe(), or TrinaryLogic::createNo().
How to fix it #
Pass a literal string as the first argument:
use function PHPStan\Testing\assertType;
-$type = rand(0, 1) ? 'int' : 'string';
-assertType($type, $value);
+assertType('int', $value);
For assertVariableCertainty, use a direct TrinaryLogic static call:
use function PHPStan\Testing\assertVariableCertainty;
use PHPStan\TrinaryLogic;
-assertVariableCertainty($certainty, $variable);
+assertVariableCertainty(TrinaryLogic::createYes(), $variable);
Non-ignorable error #
This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.