website/errors/cast.bool.md
<?php declare(strict_types = 1);
class Foo
{
}
$foo = new Foo();
$value = (bool) $foo;
The expression being cast to bool has a type that cannot be meaningfully cast to a boolean. Not all types support casting to bool in a well-defined way.
In the example above, an object of class Foo is cast to bool. While PHP allows casting most values to bool, PHPStan reports this when the source type makes the cast problematic or undefined.
Instead of casting the value directly, use an explicit comparison or add a method that returns a boolean:
<?php declare(strict_types = 1);
class Foo
{
+ public function isValid(): bool
+ {
+ return true;
+ }
}
$foo = new Foo();
-$value = (bool) $foo;
+$value = $foo->isValid();