Skip to content

Commit 2b0013f

Browse files
committed
Fixed IntersectionType::toBoolean()
1 parent 2a6901a commit 2b0013f

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/Type/IntersectionType.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,14 @@ public function isCloneable(): TrinaryLogic
326326

327327
public function toBoolean(): BooleanType
328328
{
329-
/** @var BooleanType $type */
330329
$type = $this->intersectTypes(static function (Type $type): BooleanType {
331330
return $type->toBoolean();
332331
});
333332

333+
if (!$type instanceof BooleanType) {
334+
return new BooleanType();
335+
}
336+
334337
return $type;
335338
}
336339

tests/PHPStan/Type/IntersectionTypeTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\TrinaryLogic;
66
use PHPStan\Type\Accessory\HasOffsetType;
77
use PHPStan\Type\Accessory\HasPropertyType;
8+
use PHPStan\Type\Accessory\NonEmptyArrayType;
89
use PHPStan\Type\Constant\ConstantArrayType;
910
use PHPStan\Type\Constant\ConstantIntegerType;
1011
use PHPStan\Type\Constant\ConstantStringType;
@@ -420,4 +421,10 @@ public function testIsSubTypeOfInversed(IntersectionType $type, Type $otherType,
420421
);
421422
}
422423

424+
public function testToBooleanCrash(): void
425+
{
426+
$type = new IntersectionType([new NeverType(), new NonEmptyArrayType()]);
427+
$this->assertSame('bool', $type->toBoolean()->describe(VerbosityLevel::precise()));
428+
}
429+
423430
}

0 commit comments

Comments
 (0)