Error Identifier: conditionalType.subjectNotFound
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
{
/**
* @return ($value is string ? int : bool)
*/
public function doFoo(mixed $value): int|bool
{
// ...
}
}
Why is it reported? #
A conditional return type uses a subject type that does not reference any @template tag. In a class method without @template tags, the subject of a conditional type must refer to a parameter using the $param is Type syntax rather than a bare type name.
How to fix it #
If the condition depends on a parameter, use the $param is Type syntax:
<?php declare(strict_types = 1);
class Foo
{
/**
- * @return ($value is string ? int : bool)
+ * @param string|int $value
+ * @return ($value is string ? int : bool)
*/
- public function doFoo(mixed $value): int|bool
+ public function doFoo(string|int $value): int|bool
{
// ...
}
}
If you intend to use a template type as the subject, declare it with @template:
<?php declare(strict_types = 1);
class Foo
{
/**
+ * @template T
- * @return (int is string ? int : bool)
+ * @return (T is string ? int : bool)
*/
- public function doFoo(mixed $value): int|bool
+ public function doFoo(mixed $value): int|bool
{
// ...
}
}
How to ignore this error #
You can use the identifier conditionalType.subjectNotFound to ignore this error using a comment:
// @phpstan-ignore conditionalType.subjectNotFound
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: conditionalType.subjectNotFound