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

Improve intersecting constant array with general array #1429

Merged
merged 1 commit into from Jun 16, 2022
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
26 changes: 25 additions & 1 deletion src/Type/TypeCombinator.php
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand Down Expand Up @@ -674,15 +675,22 @@ public static function intersect(Type ...$types): Type
$typesCount = count($types);
}

// move subtractables with subtracts before those without to avoid loosing them in the union logic
usort($types, static function (Type $a, Type $b): int {
// move subtractables with subtracts before those without to avoid loosing 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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

the comment above the sort callback might need adjustments

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

return 0;
});

Expand Down Expand Up @@ -771,6 +779,22 @@ public static function intersect(Type ...$types): Type
continue 2;
}

if ($types[$i] instanceof ConstantArrayType && $types[$j] instanceof ArrayType && !$types[$j] instanceof ConstantArrayType) {
$newArray = ConstantArrayTypeBuilder::createEmpty();
$valueTypes = $types[$i]->getValueTypes();
foreach ($types[$i]->getKeyTypes() as $k => $keyType) {
$newArray->setOffsetValueType(
self::intersect($keyType, $types[$j]->getIterableKeyType()),
self::intersect($valueTypes[$k], $types[$j]->getIterableValueType()),
$types[$i]->isOptionalKey($k),
);
}
$types[$i] = $newArray->getArray();
array_splice($types, $j--, 1);
$typesCount--;
continue 2;
}

if (
($types[$i] instanceof ArrayType || $types[$i] instanceof IterableType) &&
($types[$j] instanceof ArrayType || $types[$j] instanceof IterableType)
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -912,6 +912,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7387.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7353.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7031.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/constant-array-intersect.php');
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Analyser/data/constant-array-intersect.php
@@ -0,0 +1,17 @@
<?php

namespace ConstantArrayIntersect;

use function PHPStan\Testing\assertType;

/**
* @param array{key: string|null}&array<string> $array1
* @param array<string>&array{key: string|null} $array2
*/
function test(
array $array1,
array $array2,
): void {
assertType('array{key: string}', $array1);
assertType('array{key: string}', $array2);
}