forked from phpstan/phpstan-src
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make IntegerRangeType->isSubTypeOf analyse Unions
Partially resolves phpstan/phpstan#3339
- Loading branch information
Showing
5 changed files
with
142 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type; | ||
|
||
use PHPStan\TrinaryLogic; | ||
|
||
/** | ||
* Indictates that isSubTypeOf may be called with a UnionType without risking | ||
* infinite recursion. | ||
*/ | ||
interface ImplementsSubTypeOfUnion | ||
{ | ||
|
||
public function isSubTypeOf(Type $type): TrinaryLogic; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type; | ||
|
||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\Constant\ConstantIntegerType; | ||
|
||
class IntegerRangeTypeTest extends \PHPStan\Testing\TestCase | ||
{ | ||
|
||
public function dataIsSubTypeOf(): iterable | ||
{ | ||
yield [ | ||
IntegerRangeType::fromInterval(5, 10), | ||
new IntegerType(), | ||
TrinaryLogic::createYes(), | ||
]; | ||
|
||
yield [ | ||
IntegerRangeType::fromInterval(5, 10), | ||
new ConstantIntegerType(10), | ||
TrinaryLogic::createMaybe(), | ||
]; | ||
|
||
yield [ | ||
IntegerRangeType::fromInterval(5, 10), | ||
new ConstantIntegerType(20), | ||
TrinaryLogic::createNo(), | ||
]; | ||
|
||
yield [ | ||
IntegerRangeType::fromInterval(5, 10), | ||
IntegerRangeType::fromInterval(0, 15), | ||
TrinaryLogic::createYes(), | ||
]; | ||
|
||
yield [ | ||
IntegerRangeType::fromInterval(5, 10), | ||
IntegerRangeType::fromInterval(6, 9), | ||
TrinaryLogic::createMaybe(), | ||
]; | ||
|
||
yield [ | ||
IntegerRangeType::fromInterval(5, 10), | ||
new UnionType([new IntegerType(), new StringType()]), | ||
TrinaryLogic::createYes(), | ||
]; | ||
|
||
yield [ | ||
IntegerRangeType::fromInterval(5, 10), | ||
new UnionType([new ConstantIntegerType(5), IntegerRangeType::fromInterval(6, 10), new StringType()]), | ||
TrinaryLogic::createYes(), | ||
]; | ||
|
||
yield [ | ||
IntegerRangeType::fromInterval(5, 10), | ||
new StringType(), | ||
TrinaryLogic::createNo(), | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataIsSubTypeOf | ||
*/ | ||
public function testIsSubTypeOf(IntegerRangeType $type, Type $otherType, TrinaryLogic $expectedResult): void | ||
{ | ||
$actualResult = $type->isSubTypeOf($otherType); | ||
$this->assertSame( | ||
$expectedResult->describe(), | ||
$actualResult->describe(), | ||
sprintf('%s -> isSuperTypeOf(%s)', $type->describe(VerbosityLevel::precise()), $otherType->describe(VerbosityLevel::precise())) | ||
); | ||
} | ||
|
||
} |