Skip to content

Commit eb2d15e

Browse files
authored
Merge branch refs/heads/1.10.x into 1.11.x
2 parents 43cffe3 + 7c83225 commit eb2d15e

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/Analyser/MutatingScope.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5063,7 +5063,10 @@ private function exactInstantiation(New_ $node, string $className): ?Type
50635063
return $type->getBound();
50645064
}
50655065

5066-
if ($newType->isConstantValue()->yes() && (!$type->getBound()->isScalar()->yes() || $type->getBound()->describe(VerbosityLevel::precise()) === '(int|string)')) {
5066+
$isArrayKey = $type->getBound()->describe(VerbosityLevel::precise()) === '(int|string)';
5067+
if ($newType->isScalar()->yes() && !$type->getVariance()->covariant() && $isArrayKey) {
5068+
$newType = $newType->generalize(GeneralizePrecision::templateArgument());
5069+
} elseif ($newType->isConstantValue()->yes() && (!$type->getBound()->isScalar()->yes() || $isArrayKey)) {
50675070
$newType = $newType->generalize(GeneralizePrecision::templateArgument());
50685071
}
50695072

src/Reflection/ResolvedFunctionVariant.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ private function resolveResolvableTemplateTypes(Type $type, TemplateTypeVariance
184184
return $traverse($type);
185185
}
186186

187-
if ($newType->isConstantValue()->yes() && (!$type->getBound()->isScalar()->yes() || $type->getBound()->describe(VerbosityLevel::precise()) === '(int|string)')) {
187+
$isArrayKey = $type->getBound()->describe(VerbosityLevel::precise()) === '(int|string)';
188+
if ($newType->isScalar()->yes() && !$type->getVariance()->covariant() && $isArrayKey) {
189+
$newType = $newType->generalize(GeneralizePrecision::templateArgument());
190+
} elseif ($newType->isConstantValue()->yes() && (!$type->getBound()->isScalar()->yes() || $isArrayKey)) {
188191
$newType = $newType->generalize(GeneralizePrecision::templateArgument());
189192
}
190193

tests/PHPStan/Analyser/data/generics-do-not-generalize.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace GenericsDoNotGeneralize;
44

5+
use ArrayIterator;
56
use function PHPStan\Testing\assertType;
67

78
/**
@@ -91,3 +92,28 @@ function (): void {
9192

9293
assertType('list<1|2>', $bar);
9394
};
95+
96+
function (): void {
97+
/** @var list<string> $a */
98+
$a = doFoo();
99+
100+
assertType('ArrayIterator<int, string>', new ArrayIterator($a));
101+
};
102+
103+
/**
104+
* @template K of array-key
105+
* @template V
106+
* @param array<K, V> $a
107+
* @return ArrayIterator<K, V>
108+
*/
109+
function createArrayIterator(array $a): ArrayIterator
110+
{
111+
112+
}
113+
114+
function (): void {
115+
/** @var list<string> $a */
116+
$a = doFoo();
117+
118+
assertType('ArrayIterator<int, string>', createArrayIterator($a));
119+
};

0 commit comments

Comments
 (0)