diff --git a/src/Type/Accessory/AccessoryArrayListType.php b/src/Type/Accessory/AccessoryArrayListType.php index ff3536f473..6042d14ced 100644 --- a/src/Type/Accessory/AccessoryArrayListType.php +++ b/src/Type/Accessory/AccessoryArrayListType.php @@ -162,6 +162,10 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type { + if ($this->hasOffsetValueType($offsetType)->no()) { + return new ErrorType(); + } + return $this; } diff --git a/tests/PHPStan/Analyser/nsrt/bug-13629.php b/tests/PHPStan/Analyser/nsrt/bug-13629.php new file mode 100644 index 0000000000..621b4169bb --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-13629.php @@ -0,0 +1,37 @@ +> $xsdFiles + * @param array> $groupedByNamespace + * @param array> $extraNamespaces + */ +function test(array $xsdFiles, array $groupedByNamespace, array $extraNamespaces): void { + foreach ($extraNamespaces as $mergedNamespace) { + if (count($mergedNamespace) < 2) { + continue; + } + + $targetNamespace = end($mergedNamespace); + if (!isset($groupedByNamespace[$targetNamespace])) { + continue; + } + $xmlNamespace = $groupedByNamespace[$targetNamespace][0]['xmlNamespace']; + + assertType('string', $xmlNamespace); + assertType('non-empty-list&hasOffsetValue(1, string)', $mergedNamespace); + + $xsdFiles[$xmlNamespace] = []; + foreach ($mergedNamespace as $namespace) { + foreach ($groupedByNamespace[$namespace] ?? [] as $viewHelper) { + $xsdFiles[$xmlNamespace][$viewHelper['name']] = $viewHelper; + } + } + // After assigning with string keys ($viewHelper['name']), $xsdFiles[$xmlNamespace] should NOT be a list + assertType('array, array{xmlNamespace: string, namespace: string, name: string}>', $xsdFiles[$xmlNamespace]); + $xsdFiles[$xmlNamespace] = array_values($xsdFiles[$xmlNamespace]); + } +} diff --git a/tests/PHPStan/Rules/Functions/ArrayValuesRuleTest.php b/tests/PHPStan/Rules/Functions/ArrayValuesRuleTest.php index 1f3e273b16..37cb87b0f0 100644 --- a/tests/PHPStan/Rules/Functions/ArrayValuesRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ArrayValuesRuleTest.php @@ -89,4 +89,9 @@ public function testFile(): void $this->analyse([__DIR__ . '/data/array_values_list.php'], $expectedErrors); } + public function testBug13629(): void + { + $this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-13629.php'], []); + } + }