Error Identifier: phpstan.superType
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\assertSuperType;
function doFoo(int $a): void
{
assertSuperType('string', $a); // ERROR: Expected subtype of string, actual: int
}
Why is it reported? #
The PHPStan\Testing\assertSuperType() function is a testing and debugging tool built into PHPStan. It asserts that the type of the second argument is a subtype of the type specified in the first argument (a type string). When the assertion fails – meaning the actual type is not a subtype of the expected super type – this error is reported.
This function is primarily used in PHPStan’s own test suite and in extension development to verify that PHPStan correctly infers types. It ensures that the inferred type is at least as specific as (or more specific than) the expected type.
This error is not ignorable because it represents a failed type assertion that should be addressed by fixing either the code or the expectation.
How to fix it #
Either fix the code so that the type matches the assertion:
<?php declare(strict_types = 1);
use function PHPStan\Testing\assertSuperType;
-function doFoo(int $a): void
+function doFoo(string $a): void
{
assertSuperType('string', $a);
}
Or update the assertion to match the actual type:
<?php declare(strict_types = 1);
use function PHPStan\Testing\assertSuperType;
function doFoo(int $a): void
{
- assertSuperType('string', $a);
+ assertSuperType('int', $a);
}
Or remove the assertion once debugging is complete:
<?php declare(strict_types = 1);
-use function PHPStan\Testing\assertSuperType;
-
function doFoo(int $a): void
{
- assertSuperType('string', $a);
+ // actual code here
}
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.
Rules that report this error #
- PHPStan\Rules\Debug\FileAssertRule [1]