From dc6a384b765776769bfa48a9bd9b385a7c3cd311 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 23 Sep 2022 15:39:52 +0200 Subject: [PATCH] added more integer range vs. int constants tests --- .../TypesAssignedToPropertiesRuleTest.php | 27 +++++++++++ .../data/int-ranges-and-constants.php | 47 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/PHPStan/Rules/Properties/data/int-ranges-and-constants.php diff --git a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php index 09123d73316..de3e512ea4b 100644 --- a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php @@ -475,5 +475,32 @@ public function testBug6356b(): void ], ]); } + + public function testIntegerRangesAndConstants(): void + { + $this->checkExplicitMixed = true; + $this->analyse([__DIR__ . '/data/int-ranges-and-constants.php'], [ + [ + 'Property IntegerRangesAndConstants\HelloWorld::$i (0|1|3) does not accept int<0, 3>.', + 17, + ], + [ + 'Property IntegerRangesAndConstants\HelloWorld::$x (int<0, 3>) does not accept 0|1|2|3|string.', + 42, + ], + [ + 'Property IntegerRangesAndConstants\HelloWorld::$x (int<0, 3>) does not accept 0|1|2|3|bool.', + 43, + ], + [ + 'Property IntegerRangesAndConstants\HelloWorld::$x (int<0, 3>) does not accept 0|1|3|bool.', + 44, + ], + [ + 'Property IntegerRangesAndConstants\HelloWorld::$x (int<0, 3>) does not accept 0|1|3|4.', + 45, + ], + ]); + } } diff --git a/tests/PHPStan/Rules/Properties/data/int-ranges-and-constants.php b/tests/PHPStan/Rules/Properties/data/int-ranges-and-constants.php new file mode 100644 index 00000000000..cb9d99d16e0 --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/int-ranges-and-constants.php @@ -0,0 +1,47 @@ +i = random_int(0, 3); + $this->j = random_int(0, 3); + $this->k = random_int(0, 3); + $this->l = random_int(0, 3); + } + + /** + * @var int<0,3> + */ + public $x = 0; + + /** + * @param 0| $a + * @param 0|1 $b + * @param 0|1|3 $c + * @param 0|1|2|3|string $j + * @param 0|1|bool|2|3 $k + * @param 0|1|bool|3 $l + * @param 0|1|3|4 $m + */ + public function test2($a, $b, $c, $j, $k, $l, $m): void { + $this->x = $a; + $this->x = $b; + $this->x = $c; + + $this->x = $j; + $this->x = $k; + $this->x = $l; + $this->x = $m; + } +}