Error Identifier: nullableType.never
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 function bar(string $a): ?never // error: Type never cannot be part of a nullable type declaration.
{
throw new \RuntimeException($a);
}
}
Why is it reported? #
The never return type indicates that a function never returns normally – it either always throws an exception or terminates execution. A nullable never type (?never) is a contradiction: if a function could return null, it does return, which conflicts with the meaning of never. PHP does not allow never to be part of a union or nullable type declaration.
How to fix it #
If the function always throws or exits, use never without the nullable modifier.
class Foo
{
- public function bar(string $a): ?never
+ public function bar(string $a): never
{
throw new \RuntimeException($a);
}
}
If the function can sometimes return null, use void instead since it indicates the function returns no meaningful value.
class Foo
{
- public function bar(string $a): ?never
+ public function bar(string $a): void
{
if ($a === '') {
throw new \RuntimeException('Empty string');
}
}
}
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\Types\InvalidTypesInUnionRule [1]