Skip to content
Merged
Changes from 1 commit
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
39 changes: 23 additions & 16 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@
if ($constantArrayValuesCount <= ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
return $types;
}

Check failure on line 1054 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/TypeCombinator.php is expected to occur 21 times, but occurred 22 times.
// Stage 1: collapse same-key-set ConstantArrayType variants per-position
// before the (lossy) generalization below kicks in. Variants with the
// same key signature mergeWith losslessly into a single shape whose
Expand All @@ -1064,7 +1064,7 @@
$signatureGroups = [];
$nonConstantTypes = [];
foreach ($types as $idx => $type) {
if (!$type instanceof ConstantArrayType) {

Check failure on line 1067 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Ignored error "Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/TypeCombinator.php is expected to occur 21 times, but occurred 22 times.

Check failure on line 1067 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Ignored error "Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/TypeCombinator.php is expected to occur 21 times, but occurred 22 times.

Check failure on line 1067 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.5)

Ignored error "Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/TypeCombinator.php is expected to occur 21 times, but occurred 22 times.

Check failure on line 1067 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Ignored error "Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/TypeCombinator.php is expected to occur 21 times, but occurred 22 times.

Check failure on line 1067 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/TypeCombinator.php is expected to occur 21 times, but occurred 22 times.

Check failure on line 1067 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/TypeCombinator.php is expected to occur 21 times, but occurred 22 times.

Check failure on line 1067 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/TypeCombinator.php is expected to occur 21 times, but occurred 22 times.

Check failure on line 1067 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/TypeCombinator.php is expected to occur 21 times, but occurred 22 times.

Check failure on line 1067 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/TypeCombinator.php is expected to occur 21 times, but occurred 22 times.

Check failure on line 1067 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/TypeCombinator.php is expected to occur 21 times, but occurred 22 times.
$nonConstantTypes[$idx] = $type;
continue;
}
Expand Down Expand Up @@ -1609,9 +1609,14 @@
$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 @@
$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 Expand Up @@ -1878,7 +1885,7 @@
$newArrayType = $newArray->getArray();
}

if ($constArrayIsI) {

Check failure on line 1888 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.
$types[$i] = $newArrayType;
array_splice($types, $j--, 1);
} else {
Expand All @@ -1891,7 +1898,7 @@

if (
($types[$i] instanceof ArrayType || $types[$i] instanceof ConstantArrayType || $types[$i] instanceof IterableType) &&
($types[$j] instanceof ArrayType || $types[$j] instanceof ConstantArrayType || $types[$j] instanceof IterableType)

Check failure on line 1901 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.

Check failure on line 1901 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.

Check failure on line 1901 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.5)

Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.

Check failure on line 1901 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.

Check failure on line 1901 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.

Check failure on line 1901 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.

Check failure on line 1901 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.

Check failure on line 1901 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.

Check failure on line 1901 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.

Check failure on line 1901 in src/Type/TypeCombinator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.
) {
$keyType = self::intersect($types[$i]->getIterableKeyType(), $types[$j]->getKeyType());
$itemType = self::intersect($types[$i]->getItemType(), $types[$j]->getItemType());
Expand Down
Loading