Skip to content

Commit

Permalink
Fixed IntersectionType::toBoolean()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 4, 2020
1 parent 2a6901a commit 2b0013f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,14 @@ public function isCloneable(): TrinaryLogic

public function toBoolean(): BooleanType
{
/** @var BooleanType $type */
$type = $this->intersectTypes(static function (Type $type): BooleanType {
return $type->toBoolean();
});

if (!$type instanceof BooleanType) {
return new BooleanType();
}

return $type;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Type/IntersectionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasPropertyType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
Expand Down Expand Up @@ -420,4 +421,10 @@ public function testIsSubTypeOfInversed(IntersectionType $type, Type $otherType,
);
}

public function testToBooleanCrash(): void
{
$type = new IntersectionType([new NeverType(), new NonEmptyArrayType()]);
$this->assertSame('bool', $type->toBoolean()->describe(VerbosityLevel::precise()));
}

}

0 comments on commit 2b0013f

Please sign in to comment.