Skip to content

Commit 580065f

Browse files
staabmphpstan-bot
authored andcommitted
Fix "Parameter X of array_values is already a list, call has no effect"-false positive (phpstan#5271)
1 parent 5dbc952 commit 580065f

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/Type/IntersectionType.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,12 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
981981
}
982982
}
983983

984-
if ($this->isList()->yes() && $this->getIterableValueType()->isArray()->yes()) {
984+
if (
985+
$this->isList()->yes()
986+
&& $offsetType !== null
987+
&& $offsetType->toArrayKey()->isInteger()->yes()
988+
&& $this->getIterableValueType()->isArray()->yes()
989+
) {
985990
$result = TypeCombinator::intersect($result, new AccessoryArrayListType());
986991
}
987992

tests/PHPStan/Analyser/nsrt/bug-10089.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ class Test
1111
protected function create_matrix(int $size): array
1212
{
1313
$size = min(8, $size);
14+
assertType('int<min, 8>', $size);
1415
$matrix = [];
1516
for ($i = 0; $i < $size; $i++) {
17+
assertType('int<0, 7>', $i);
1618
$matrix[] = array_fill(0, $size, 0);
1719
}
1820

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13629;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param array<string, list<array{xmlNamespace: string, namespace: string, name: string}>> $xsdFiles
9+
* @param array<string, list<array{xmlNamespace: string, namespace: string, name: string}>> $groupedByNamespace
10+
* @param array<string, list<string>> $extraNamespaces
11+
*/
12+
function test(array $xsdFiles, array $groupedByNamespace, array $extraNamespaces): void {
13+
foreach ($extraNamespaces as $mergedNamespace) {
14+
if (count($mergedNamespace) < 2) {
15+
continue;
16+
}
17+
18+
$targetNamespace = end($mergedNamespace);
19+
if (!isset($groupedByNamespace[$targetNamespace])) {
20+
continue;
21+
}
22+
$xmlNamespace = $groupedByNamespace[$targetNamespace][0]['xmlNamespace'];
23+
24+
assertType('string', $xmlNamespace);
25+
assertType('non-empty-list<string>&hasOffsetValue(1, string)', $mergedNamespace);
26+
27+
$xsdFiles[$xmlNamespace] = [];
28+
foreach ($mergedNamespace as $namespace) {
29+
foreach ($groupedByNamespace[$namespace] ?? [] as $viewHelper) {
30+
assertType('string', $viewHelper['name']);
31+
$xsdFiles[$xmlNamespace][$viewHelper['name']] = $viewHelper;
32+
}
33+
}
34+
// After assigning with string keys ($viewHelper['name']), $xsdFiles[$xmlNamespace] should NOT be a list
35+
assertType('array<int<0, max>|string, array{xmlNamespace: string, namespace: string, name: string}>', $xsdFiles[$xmlNamespace]);
36+
$xsdFiles[$xmlNamespace] = array_values($xsdFiles[$xmlNamespace]);
37+
}
38+
}

0 commit comments

Comments
 (0)