Skip to content
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

Sort IntegerRangeType numerically #357

Merged
merged 1 commit into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Type/UnionTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public static function sortTypes(array $types): array
return 0;
}

if ($a instanceof IntegerRangeType && $b instanceof IntegerRangeType) {
return $a->getMin() <=> $b->getMin();
}

if ($a instanceof ConstantStringType && $b instanceof ConstantStringType) {
return strcasecmp($a->getValue(), $b->getValue());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2343,7 +2343,7 @@ public function dataBinaryOperations(): array
'@$stringOrNull ?: 12',
],
[
'int<1, max>|int<min, -1>',
'int<min, -1>|int<1, max>',
'$integer ?: 12',
],
[
Expand Down Expand Up @@ -5244,7 +5244,7 @@ public function dataArrayFunctions(): array
'array_filter($union)',
],
[
'array<int, int<1, max>|int<min, -1>|true>',
'array<int, int<min, -1>|int<1, max>|true>',
'array_filter($withPossiblyFalsey)',
],
[
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/TypeSpecifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ public function dataCondition(): array
)
),
[
'$n' => '~int<6, max>|int<min, 2>',
'$n' => '~int<min, 2>|int<6, max>',
],
[
'$n' => '~int<3, 5>',
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/data/bug-2954.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@

function (int $x) {
if ($x === 0) return;
assertType('int<1, max>|int<min, -1>', $x);
assertType('int<min, -1>|int<1, max>', $x);

$x++;
assertType('int', $x);
};

function (int $x) {
if ($x === 0) return;
assertType('int<1, max>|int<min, -1>', $x);
assertType('int<min, -1>|int<1, max>', $x);

++$x;
assertType('int', $x);
};

function (int $x) {
if ($x === 0) return;
assertType('int<1, max>|int<min, -1>', $x);
assertType('int<min, -1>|int<1, max>', $x);

$x--;
assertType('int', $x);
};

function (int $x) {
if ($x === 0) return;
assertType('int<1, max>|int<min, -1>', $x);
assertType('int<min, -1>|int<1, max>', $x);

--$x;
assertType('int', $x);
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/data/integer-range-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ function (int $i) {
assertType('int<min, 1>', $i);
}

assertType('int<3, max>|int<min, 1>', $i);
assertType('int<min, 1>|int<3, max>', $i);

if ($i < 3 && $i > 5) {
assertType('*NEVER*', $i);
} else {
assertType('int<3, max>|int<min, 1>', $i);
assertType('int<min, 1>|int<3, max>', $i);
}

if ($i > 3 && $i < 5) {
assertType('4', $i);
} else {
assertType('3|int<5, max>|int<min, 1>', $i);
assertType('3|int<min, 1>|int<5, max>', $i);
}

if ($i >= 3 && $i <= 5) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ public function testStrictComparison(): void
438,
],
[
'Strict comparison using === between int<2, max>|int<min, 0>|string and 1.0 will always evaluate to false.',
'Strict comparison using === between int<min, 0>|int<2, max>|string and 1.0 will always evaluate to false.',
464,
],
[
'Strict comparison using === between int<2, max>|int<min, 0>|string and stdClass will always evaluate to false.',
'Strict comparison using === between int<min, 0>|int<2, max>|string and stdClass will always evaluate to false.',
466,
],
[
Expand Down Expand Up @@ -333,11 +333,11 @@ public function testStrictComparisonWithoutAlwaysTrue(): void
438,
],
[
'Strict comparison using === between int<2, max>|int<min, 0>|string and 1.0 will always evaluate to false.',
'Strict comparison using === between int<min, 0>|int<2, max>|string and 1.0 will always evaluate to false.',
464,
],
[
'Strict comparison using === between int<2, max>|int<min, 0>|string and stdClass will always evaluate to false.',
'Strict comparison using === between int<min, 0>|int<2, max>|string and stdClass will always evaluate to false.',
466,
],
[
Expand Down
10 changes: 9 additions & 1 deletion tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,14 @@ public function dataUnion(): array
UnionType::class,
'int<1, 3>|int<7, 9>',
],
[
[
IntegerRangeType::fromInterval(7, 9),
IntegerRangeType::fromInterval(1, 3),
],
UnionType::class,
'int<1, 3>|int<7, 9>',
],
[
[
IntegerRangeType::fromInterval(1, 3),
Expand Down Expand Up @@ -3204,7 +3212,7 @@ public function dataRemove(): array
new BenevolentUnionType([new IntegerType(), new StringType()]),
new ConstantIntegerType(1),
UnionType::class,
'int<2, max>|int<min, 0>|string',
'int<min, 0>|int<2, max>|string',
],
[
new BenevolentUnionType([new IntegerType(), new StringType()]),
Expand Down