From ceed52846938ae40dd433df90a2666deb6980339 Mon Sep 17 00:00:00 2001 From: George Bateman Date: Mon, 25 Jan 2021 22:53:17 +0000 Subject: [PATCH] Apply some feedback from PR --- .../Rules/Classes/InstantiationRuleTest.php | 4 +++ .../Rules/Classes/data/instantiation.php | 26 +++++++++++++++++++ .../Rules/Functions/ReturnTypeRuleTest.php | 4 +++ .../Rules/Functions/data/returnTypes.php | 22 ++++++++++++++++ tests/PHPStan/Type/IntegerRangeTypeTest.php | 2 +- 5 files changed, 57 insertions(+), 1 deletion(-) diff --git a/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php b/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php index 62bf98915ba..1eeefc90f60 100644 --- a/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php +++ b/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php @@ -193,6 +193,10 @@ public function testInstantiation(): void 'Class TestInstantiation\ClassExtendingAbstractConstructor constructor invoked with 0 parameters, 1 required.', 273, ], + [ + 'Parameter #2 $y of class TestInstantiation\IntRange constructor expects 1|2|3|4|5|6|7, int<1, 8> given.', + 291, + ], ] ); } diff --git a/tests/PHPStan/Rules/Classes/data/instantiation.php b/tests/PHPStan/Rules/Classes/data/instantiation.php index 58c5b2cf60c..265afeb081b 100644 --- a/tests/PHPStan/Rules/Classes/data/instantiation.php +++ b/tests/PHPStan/Rules/Classes/data/instantiation.php @@ -274,3 +274,29 @@ public function doBar() } } + +final class IntRange +{ + /** + * @psalm-var 1|2|3|4|5|6|7|8 + */ + private $x; + + public static function fromInt(int $x): self + { + if ($x < 1 || $x > 8) { + throw new InvalidArgumentException; + } + + return new self($x, $x); + } + + /** + * @psalm-param 1|2|3|4|5|6|7|8 $x + * @psalm-param 1|2|3|4|5|6|7 $y + */ + private function __construct(int $x, int $y) + { + $y = $this->x = $x; + } +} diff --git a/tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php b/tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php index 3781881114b..a1d1d0631d2 100644 --- a/tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php @@ -61,6 +61,10 @@ public function testReturnTypeRule(): void 'Function ReturnTypes\returnNever() should never return but return statement found.', 181, ], + [ + 'Function ReturnTypes\returnRangeBad() should return 1|2|3|4|5|6|7 but returns int<1, 8>.', + 203, + ], ]); } diff --git a/tests/PHPStan/Rules/Functions/data/returnTypes.php b/tests/PHPStan/Rules/Functions/data/returnTypes.php index 3e15651c697..49355cc5a2d 100644 --- a/tests/PHPStan/Rules/Functions/data/returnTypes.php +++ b/tests/PHPStan/Rules/Functions/data/returnTypes.php @@ -180,3 +180,25 @@ function returnNever() { return; } + +/** + * @return 1|2|3|4|5|6|7|8 + */ +function returnRange(int $x) : int { + if ($x < 1 || $x > 8) { + throw new InvalidArgumentException; + } + + return $x; +} + +/** + * @return 1|2|3|4|5|6|7 + */ +function returnRangeBad(int $x) : int { + if ($x < 1 || $x > 8) { + throw new InvalidArgumentException; + } + + return $x; +} diff --git a/tests/PHPStan/Type/IntegerRangeTypeTest.php b/tests/PHPStan/Type/IntegerRangeTypeTest.php index 3cd6e410f84..3101c7f38d6 100644 --- a/tests/PHPStan/Type/IntegerRangeTypeTest.php +++ b/tests/PHPStan/Type/IntegerRangeTypeTest.php @@ -68,7 +68,7 @@ public function testIsSubTypeOf(IntegerRangeType $type, Type $otherType, Trinary $this->assertSame( $expectedResult->describe(), $actualResult->describe(), - sprintf('%s -> isSuperTypeOf(%s)', $type->describe(VerbosityLevel::precise()), $otherType->describe(VerbosityLevel::precise())) + sprintf('%s -> isSubTypeOf(%s)', $type->describe(VerbosityLevel::precise()), $otherType->describe(VerbosityLevel::precise())) ); }