Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,7 @@ parameters:
-
rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.'
identifier: phpstanApi.instanceofType
count: 21
count: 22
path: src/Type/TypeCombinator.php

-
Expand Down
39 changes: 23 additions & 16 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1609,9 +1609,14 @@ public static function intersect(Type ...$types): Type
$newTypes = [];
$hasOffsetValueTypeCount = 0;
$typesCount = count($types);
$typesNeedSorting = false;
for ($i = 0; $i < $typesCount; $i++) {
$type = $types[$i];

if ($type instanceof SubtractableType || $type instanceof ConstantArrayType) {
$typesNeedSorting = true;
}

if ($type instanceof IntersectionType && !$type instanceof TemplateType) {
// transform A & (B & C) to A & B & C
array_splice($types, $i--, 1, $type->getTypes());
Expand All @@ -1629,24 +1634,26 @@ public static function intersect(Type ...$types): Type
$typesCount = count($types);
}

usort($types, static function (Type $a, Type $b): int {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sort function only re-arranges SubtractableType, ConstantArrayType.

if we know beforehand that $types cannot contain one of these types -> skip the sort which will not change the order of types anyway

// move subtractables with subtracts before those without to avoid losing them in the union logic
if ($a instanceof SubtractableType && $a->getSubtractedType() !== null) {
return -1;
}
if ($b instanceof SubtractableType && $b->getSubtractedType() !== null) {
return 1;
}
if ($typesNeedSorting) {
usort($types, static function (Type $a, Type $b): int {
// move subtractables with subtracts before those without to avoid losing them in the union logic
if ($a instanceof SubtractableType && $a->getSubtractedType() !== null) {
return -1;
}
if ($b instanceof SubtractableType && $b->getSubtractedType() !== null) {
return 1;
}

if ($a instanceof ConstantArrayType && !$b instanceof ConstantArrayType) {
return -1;
}
if ($b instanceof ConstantArrayType && !$a instanceof ConstantArrayType) {
return 1;
}
if ($a instanceof ConstantArrayType && !$b instanceof ConstantArrayType) {
return -1;
}
if ($b instanceof ConstantArrayType && !$a instanceof ConstantArrayType) {
return 1;
}

return 0;
});
return 0;
});
}

// transform IntegerType & ConstantIntegerType to ConstantIntegerType
// transform Child & Parent to Child
Expand Down
Loading