Many methods in PHPStan do not return a two-state boolean, but a three-state
The object can be created by calling one of the static methods:
PHPStan\TrinaryLogic::createFromBoolean(bool $value): self
You can ask what value the object represents by calling one of these instance methods:
PHPStan\TrinaryLogic::no(self $other): bool
The object is immutable. You can combine multiple instances either with AND or OR logical operations. These methods return new instances:
PHPStan\TrinaryLogic::and(self ...$operands): self
PHPStan\TrinaryLogic::or(self ...$operands): self
Usage in practice #
For example, let’s say we have a
new ObjectType(\Exception::class) and we ask about
hasMethod('getMessage'). This method will return TrinaryLogic’s
yes because the method always exists.
If we have
maybe, because the method is available only on one of the subtypes.
new ObjectType(\Exception::class) whether it
no, because that method does not exist on
Trinary logic is at the core of PHPStan’s rule levels. Up until level 6, most rules ask whether the result is
!no(), meaning that
maybe are valid answers and no error is reported in that case. On level 7 and up,
yes() is required, meaning that only
yes is a valid answer.