Skip to content

Commit

Permalink
MixedType::toArrayKey() returns BenevolentUnionType
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 30, 2024
1 parent 48f8990 commit c586014
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public function toArray(): Type

public function toArrayKey(): Type
{
return new UnionType([new IntegerType(), new StringType()]);
return new BenevolentUnionType([new IntegerType(), new StringType()]);
}

public function isIterable(): TrinaryLogic
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3328,7 +3328,7 @@ public function dataLiteralArraysKeys(): array
"'BooleansArray'",
],
[
'int|string',
'(int|string)',
"'UnknownConstantArray'",
],
];
Expand Down Expand Up @@ -9147,7 +9147,7 @@ public function dataGeneralizeScope(): array
{
return [
[
'array<string, non-empty-array<int|string, array{saveCount: int<0, max>, removeCount: int<0, max>, loadCount: int<0, max>, hitCount: int<0, max>}>>',
'array<string, non-empty-array<array{saveCount: int<0, max>, removeCount: int<0, max>, loadCount: int<0, max>, hitCount: int<0, max>}>>',
'$statistics',
],
];
Expand Down
12 changes: 6 additions & 6 deletions tests/PHPStan/Analyser/nsrt/array-column-php82.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ public function testImprecise5(array $array): void
assertType('array<string, DOMElement>', array_column($array, null, 'tagName'));
assertType('list<mixed>', array_column($array, 'foo'));
assertType('array<string, mixed>', array_column($array, 'foo', 'tagName'));
assertType('array<int|string, string>', array_column($array, 'nodeName', 'foo'));
assertType('array<int|string, DOMElement>', array_column($array, null, 'foo'));
assertType('array<string>', array_column($array, 'nodeName', 'foo'));
assertType('array<DOMElement>', array_column($array, null, 'foo'));
}

/** @param non-empty-array<int, DOMElement> $array */
Expand All @@ -189,8 +189,8 @@ public function testObjects1(array $array): void
assertType('non-empty-array<string, DOMElement>', array_column($array, null, 'tagName'));
assertType('list<mixed>', array_column($array, 'foo'));
assertType('array<string, mixed>', array_column($array, 'foo', 'tagName'));
assertType('non-empty-array<int|string, string>', array_column($array, 'nodeName', 'foo'));
assertType('non-empty-array<int|string, DOMElement>', array_column($array, null, 'foo'));
assertType('non-empty-array<string>', array_column($array, 'nodeName', 'foo'));
assertType('non-empty-array<DOMElement>', array_column($array, null, 'foo'));
}

/** @param array{DOMElement} $array */
Expand All @@ -201,8 +201,8 @@ public function testObjects2(array $array): void
assertType('non-empty-array<string, DOMElement>', array_column($array, null, 'tagName'));
assertType('list<mixed>', array_column($array, 'foo'));
assertType('array<string, mixed>', array_column($array, 'foo', 'tagName'));
assertType('non-empty-array<int|string, string>', array_column($array, 'nodeName', 'foo'));
assertType('non-empty-array<int|string, DOMElement>', array_column($array, null, 'foo'));
assertType('non-empty-array<string>', array_column($array, 'nodeName', 'foo'));
assertType('non-empty-array<DOMElement>', array_column($array, null, 'foo'));
}

}
Expand Down
14 changes: 7 additions & 7 deletions tests/PHPStan/Analyser/nsrt/array-column.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ public function testImprecise5(array $array): void
assertType('array<string, DOMElement>', array_column($array, null, 'tagName'));
assertType('list<mixed>', array_column($array, 'foo'));
assertType('array<string, mixed>', array_column($array, 'foo', 'tagName'));
assertType('array<int|string, string>', array_column($array, 'nodeName', 'foo'));
assertType('array<int|string, DOMElement>', array_column($array, null, 'foo'));
assertType('array<string>', array_column($array, 'nodeName', 'foo'));
assertType('array<DOMElement>', array_column($array, null, 'foo'));
}

/** @param non-empty-array<int, DOMElement> $array */
Expand All @@ -203,8 +203,8 @@ public function testObjects1(array $array): void
assertType('non-empty-array<string, DOMElement>', array_column($array, null, 'tagName'));
assertType('list<mixed>', array_column($array, 'foo'));
assertType('array<string, mixed>', array_column($array, 'foo', 'tagName'));
assertType('non-empty-array<int|string, string>', array_column($array, 'nodeName', 'foo'));
assertType('non-empty-array<int|string, DOMElement>', array_column($array, null, 'foo'));
assertType('non-empty-array<string>', array_column($array, 'nodeName', 'foo'));
assertType('non-empty-array<DOMElement>', array_column($array, null, 'foo'));
}

/** @param array{DOMElement} $array */
Expand All @@ -215,8 +215,8 @@ public function testObjects2(array $array): void
assertType('non-empty-array<string, DOMElement>', array_column($array, null, 'tagName'));
assertType('list<mixed>', array_column($array, 'foo'));
assertType('array<string, mixed>', array_column($array, 'foo', 'tagName'));
assertType('non-empty-array<int|string, string>', array_column($array, 'nodeName', 'foo'));
assertType('non-empty-array<int|string, DOMElement>', array_column($array, null, 'foo'));
assertType('non-empty-array<string>', array_column($array, 'nodeName', 'foo'));
assertType('non-empty-array<DOMElement>', array_column($array, null, 'foo'));
}

}
Expand All @@ -228,7 +228,7 @@ final class Foo
public function doFoo(array $a): void
{
assertType('list<mixed>', array_column($a, 'nodeName'));
assertType('array<int|string, mixed>', array_column($a, 'nodeName', 'tagName'));
assertType('array', array_column($a, 'nodeName', 'tagName'));
}

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/array-flip-php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
function mixedAndSubtractedArray($mixed)
{
if (is_array($mixed)) {
assertType('array<int|string, (int|string)>', array_flip($mixed));
assertType('array<(int|string)>', array_flip($mixed));
} else {
assertType('mixed~array', $mixed);
assertType('null', array_flip($mixed));
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/array-flip-php8.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
function mixedAndSubtractedArray($mixed)
{
if (is_array($mixed)) {
assertType('array<int|string, (int|string)>', array_flip($mixed));
assertType('array<(int|string)>', array_flip($mixed));
} else {
assertType('mixed~array', $mixed);
assertType('*NEVER*', array_flip($mixed));
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/array-flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function foo3($list)
{
$flip = array_flip($list);

assertType('array<int|string, (int|string)>', $flip);
assertType('array<(int|string)>', $flip);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/non-empty-array.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function arrayFunctions($array, $list, $stringArray): void
assertType('non-empty-array', array_replace($array, []));
assertType('non-empty-array', array_replace($array, $array));

assertType('non-empty-array<int|string, (int|string)>', array_flip($array));
assertType('non-empty-array<(int|string)>', array_flip($array));
assertType('non-empty-array<string, (int|string)>', array_flip($stringArray));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ public function doFoo(array $percentageIntervals, array $changes): void
if ($percentageInterval->isInInterval((float) $changeInPercents)) {
$key = $percentageInterval->getFormatted();
if (array_key_exists($key, $intervalResults)) {
assertType('array<int|string, array{itemsCount: mixed, interval: mixed}>', $intervalResults);
assertType('array<array{itemsCount: mixed, interval: mixed}>', $intervalResults);
assertType('array{itemsCount: mixed, interval: mixed}', $intervalResults[$key]);
$intervalResults[$key]['itemsCount'] += $itemsCount;
assertType('non-empty-array<int|string, array{itemsCount: (array|float|int), interval: mixed}>', $intervalResults);
assertType('non-empty-array<array{itemsCount: (array|float|int), interval: mixed}>', $intervalResults);
assertType('array{itemsCount: (array|float|int), interval: mixed}', $intervalResults[$key]);
} else {
assertType('array<int|string, array{itemsCount: mixed, interval: mixed}>', $intervalResults);
assertType('array<array{itemsCount: mixed, interval: mixed}>', $intervalResults);
assertType('array{itemsCount: mixed, interval: mixed}', $intervalResults[$key]);
$intervalResults[$key] = [
'itemsCount' => $itemsCount,
'interval' => $percentageInterval,
];
assertType('non-empty-array<int|string, array{itemsCount: mixed, interval: mixed}>', $intervalResults);
assertType('non-empty-array<array{itemsCount: mixed, interval: mixed}>', $intervalResults);
assertType('array{itemsCount: mixed, interval: mixed}', $intervalResults[$key]);
}
}
}
}

assertType('array<int|string, array{itemsCount: mixed, interval: mixed}>', $intervalResults);
assertType('array<array{itemsCount: mixed, interval: mixed}>', $intervalResults);
foreach ($intervalResults as $data) {
echo $data['interval'];
}
Expand Down

0 comments on commit c586014

Please sign in to comment.