Repository navigation
Implement assertGreaterThanOrEqual() and assertLessThanOrEqual() type narrowing
#359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,20 @@ public function lessThanTest(array $arr): void | |
| assertType('array{}', $arr); | ||
| } | ||
|
|
||
| /** @param string[] $arr */ | ||
| public function greaterThanOrEqualStatic(array $arr): void | ||
| { | ||
| self::assertGreaterThanOrEqual(1, count($arr)); | ||
| assertType('non-empty-array<string>', $arr); | ||
| } | ||
|
|
||
| /** @param string[] $arr */ | ||
| public function lessThanOrEqualTest(array $arr): void | ||
| { | ||
| self::assertLessThanOrEqual(0, count($arr)); | ||
| assertType('array{}', $arr); | ||
| } | ||
|
|
||
| public function integers(int $a, int $c): void | ||
| { | ||
| self::assertGreaterThan(5, $a); | ||
|
|
@@ -54,4 +68,145 @@ public function integers(int $a, int $c): void | |
| self::assertLessThan(5, $c); | ||
| assertType('int<min, 4>', $c); | ||
| } | ||
|
|
||
| public function integersOrEqual(int $a, int $c): void | ||
| { | ||
| self::assertGreaterThanOrEqual(5, $a); | ||
| assertType('int<5, max>', $a); | ||
|
|
||
| self::assertLessThanOrEqual(5, $c); | ||
| assertType('int<min, 5>', $c); | ||
| } | ||
|
|
||
| /** | ||
| * @param 1|5|9.5 $a | ||
| * @param 1.5|5|9 $c | ||
| */ | ||
| public function integersAndFloatsOrEqual($a, $c): void | ||
| { | ||
| self::assertGreaterThanOrEqual(5.0, $a); | ||
| assertType('5|9.5', $a); | ||
|
|
||
| self::assertLessThanOrEqual(5.0, $c); | ||
| assertType('1.5|5', $c); | ||
| } | ||
|
Comment on lines
+81
to
+92
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we support float we should also test with INF and NAN
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added tests in 00749f3: |
||
|
|
||
| /** @param 1.5|5 $a */ | ||
| public function infiniteExpectedOrEqual($a): void | ||
| { | ||
| self::assertGreaterThanOrEqual(-INF, $a); | ||
| self::assertLessThanOrEqual(INF, $a); | ||
| assertType('1.5|5', $a); | ||
| } | ||
|
|
||
| /** @param 1.5|5 $a */ | ||
| public function infiniteActualOrEqual($a): void | ||
| { | ||
| self::assertGreaterThanOrEqual($a, INF); | ||
| self::assertLessThanOrEqual($a, -INF); | ||
| assertType('1.5|5', $a); | ||
| } | ||
|
|
||
| public function neverInfiniteOrEqual(int $a, int $b, int $c, int $d): void | ||
| { | ||
| self::assertGreaterThanOrEqual(INF, $a); | ||
| assertType('*NEVER*', $a); | ||
|
|
||
| self::assertLessThanOrEqual(-INF, $b); | ||
| assertType('*NEVER*', $b); | ||
|
|
||
| self::assertGreaterThanOrEqual($c, -INF); | ||
| assertType('*NEVER*', $c); | ||
|
|
||
| self::assertLessThanOrEqual($d, INF); | ||
| assertType('*NEVER*', $d); | ||
| } | ||
|
|
||
| /** | ||
| * @param int<min, -1> $a | ||
| * @param int<1, max> $b | ||
| * @param int<1, max> $c | ||
| * @param int<min, -1> $d | ||
| */ | ||
| public function neverNanOrEqual(int $a, int $b, int $c, int $d): void | ||
| { | ||
| $nan = NAN; | ||
| self::assertGreaterThanOrEqual($nan, $a); | ||
| assertType('*NEVER*', $a); | ||
|
|
||
| $nan = NAN; | ||
| self::assertLessThanOrEqual($nan, $b); | ||
| assertType('*NEVER*', $b); | ||
|
|
||
| $nan = NAN; | ||
| self::assertGreaterThanOrEqual($c, $nan); | ||
| assertType('*NEVER*', $c); | ||
|
|
||
| $nan = NAN; | ||
| self::assertLessThanOrEqual($d, $nan); | ||
| assertType('*NEVER*', $d); | ||
| } | ||
|
|
||
| public function notNarrowedActualOrEqual(?int $a, ?int $c): void | ||
| { | ||
| self::assertGreaterThanOrEqual(1, $a); | ||
| assertType('int|null', $a); | ||
|
|
||
| self::assertLessThanOrEqual(1, $c); | ||
| assertType('int|null', $c); | ||
| } | ||
|
|
||
| public function notNarrowedExpectedOrEqual(int $a, int $c): void | ||
| { | ||
| self::assertGreaterThanOrEqual('5', $a); | ||
| assertType('int', $a); | ||
|
|
||
| self::assertLessThanOrEqual('5', $c); | ||
| assertType('int', $c); | ||
| } | ||
|
|
||
| /** | ||
| * @param int|string $string | ||
| * @param int|numeric-string $numericString | ||
| * @param int|array<int> $array | ||
| * @param int|\stdClass $object | ||
| * @param mixed $mixed | ||
| */ | ||
| public function notNarrowedActualTypesOrEqual($string, $numericString, $array, $object, $mixed): void | ||
| { | ||
| self::assertGreaterThanOrEqual(5, $string); | ||
| self::assertLessThanOrEqual(5, $string); | ||
| assertType('int|string', $string); | ||
|
|
||
| self::assertGreaterThanOrEqual(5, $numericString); | ||
| self::assertLessThanOrEqual(5, $numericString); | ||
| assertType('int|numeric-string', $numericString); | ||
|
|
||
| self::assertGreaterThanOrEqual(5, $array); | ||
| self::assertLessThanOrEqual(5, $array); | ||
| assertType('array<int>|int', $array); | ||
|
|
||
| self::assertGreaterThanOrEqual(5, $object); | ||
| self::assertLessThanOrEqual(5, $object); | ||
| assertType('int|stdClass', $object); | ||
|
|
||
| self::assertGreaterThanOrEqual(5, $mixed); | ||
| self::assertLessThanOrEqual(5, $mixed); | ||
| assertType('mixed', $mixed); | ||
| } | ||
|
|
||
| /** | ||
| * @param int|array<int> $array | ||
| * @param mixed $mixed | ||
| */ | ||
| public function notNarrowedExpectedTypesOrEqual($array, $mixed): void | ||
| { | ||
| self::assertGreaterThanOrEqual($array, 5); | ||
| self::assertLessThanOrEqual($array, 5); | ||
| assertType('array<int>|int', $array); | ||
|
|
||
| self::assertGreaterThanOrEqual($mixed, 5); | ||
| self::assertLessThanOrEqual($mixed, 5); | ||
| assertType('mixed', $mixed); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs a test with values different from int|float
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
notNarrowedActualTypesOrEqual(string, numeric string, array, object and mixed) andnotNarrowedExpectedTypesOrEqual(array and mixed) in 00749f3. A nullable int as actual and'5'as expected were already covered bynotNarrowedActualOrEqualandnotNarrowedExpectedOrEqual.