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

Fix generalizing constant arrays when the array is getting smaller #3864

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
use function usort;
use const PHP_INT_MAX;
use const PHP_INT_MIN;
use const PHP_VERSION_ID;

final class MutatingScope implements Scope
{
Expand Down Expand Up @@ -5138,7 +5139,10 @@ private static function generalizeType(Type $a, Type $b, int $depth): Type
} else {
$constantArraysA = TypeCombinator::union(...$constantArrays['a']);
$constantArraysB = TypeCombinator::union(...$constantArrays['b']);
if ($constantArraysA->getIterableKeyType()->equals($constantArraysB->getIterableKeyType())) {
if (
$constantArraysA->getIterableKeyType()->equals($constantArraysB->getIterableKeyType())
&& $constantArraysA->getArraySize()->equals($constantArraysB->getArraySize())
) {
$resultArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
foreach (TypeUtils::flattenTypes($constantArraysA->getIterableKeyType()) as $keyType) {
$resultArrayBuilder->setOffsetValueType(
Expand All @@ -5158,7 +5162,11 @@ private static function generalizeType(Type $a, Type $b, int $depth): Type
TypeCombinator::union(self::generalizeType($constantArraysA->getIterableKeyType(), $constantArraysB->getIterableKeyType(), $depth + 1)),
TypeCombinator::union(self::generalizeType($constantArraysA->getIterableValueType(), $constantArraysB->getIterableValueType(), $depth + 1)),
);
if ($constantArraysA->isIterableAtLeastOnce()->yes() && $constantArraysB->isIterableAtLeastOnce()->yes()) {
if (
$constantArraysA->isIterableAtLeastOnce()->yes()
&& $constantArraysB->isIterableAtLeastOnce()->yes()
&& $constantArraysA->getArraySize()->getGreaterOrEqualType(new PhpVersion(PHP_VERSION_ID))->isSuperTypeOf($constantArraysB->getArraySize())->yes()
) {
$resultType = TypeCombinator::intersect($resultType, new NonEmptyArrayType());
}
if ($constantArraysA->isList()->yes() && $constantArraysB->isList()->yes()) {
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-1021.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function foobar() {
}
}

assertType('array{0?: int<1, max>, 1?: 2|3, 2?: 3}', $x);
assertType('list<1|2|3>', $x);

if ($x) {
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug7856.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function doFoo() {
$endDate = new DateTimeImmutable('+1year');

do {
assertType("array{'+1week', '+1months', '+6months', '+17months'}|array{0: literal-string&lowercase-string&non-falsy-string, 1?: literal-string&lowercase-string&non-falsy-string, 2?: '+17months'}", $intervals);
assertType("list<literal-string&lowercase-string&non-falsy-string>", $intervals);
$periodEnd = $periodEnd->modify(array_shift($intervals));
} while (count($intervals) > 0 && $periodEnd->format('U') < $endDate);
}
Loading