Skip to content

Commit 9f13233

Browse files
authored
More precise MixedType::toBoolean() with subtracted type
1 parent b76fecc commit 9f13233

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/Type/MixedType.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,10 @@ function () use ($level): string {
478478

479479
public function toBoolean(): BooleanType
480480
{
481-
if ($this->subtractedType !== null && StaticTypeFactory::falsey()->equals($this->subtractedType)) {
482-
return new ConstantBooleanType(true);
481+
if ($this->subtractedType !== null) {
482+
if ($this->subtractedType->isSuperTypeOf(StaticTypeFactory::falsey())->yes()) {
483+
return new ConstantBooleanType(true);
484+
}
483485
}
484486

485487
return new BooleanType();

tests/PHPStan/Analyser/nsrt/mixed-subtract.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ function subtract(mixed $m, $moreThenFalsy) {
5353
assertType('mixed', $m);
5454
assertType('bool', (bool) $m); // could be true
5555
}
56+
57+
if ($m != 0 && !is_array($m) && $m != null && !is_object($m)) { // subtract more types then falsy
58+
assertType("mixed~(0|0.0|''|'0'|array|object|false|null)", $m);
59+
assertType('true', (bool) $m);
60+
}
5661
}

0 commit comments

Comments
 (0)