Skip to content

Commit 270bdfc

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents b4430ff + 4f0ab8e commit 270bdfc

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/Type/ArrayType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ public function shiftArray(): Type
557557

558558
public function shuffleArray(): Type
559559
{
560-
return new IntersectionType([new self(IntegerRangeType::createAllGreaterThanOrEqualTo(0), $this->itemType), new AccessoryArrayListType()]);
560+
return new IntersectionType([$this->withTypes(IntegerRangeType::createAllGreaterThanOrEqualTo(0), $this->itemType), new AccessoryArrayListType()]);
561561
}
562562

563563
public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,52 @@ public function ksortArray(array $items): array
176176
}
177177

178178
}
179+
180+
/**
181+
* Cases where T is bounded by a plain array (TemplateArrayType),
182+
* so T is directly the subject of shuffleArray() in ArrayType.
183+
* These verify the $this->withTypes() fix rather than the IntersectionType fix.
184+
*/
185+
class Bar
186+
{
187+
188+
/**
189+
* @template T of array<int>
190+
* @param T $items
191+
* @return T
192+
*/
193+
public function shuffleTemplateArray(array $items): array
194+
{
195+
assertType('T of array<int> (method Bug14631\Bar::shuffleTemplateArray(), argument)', $items);
196+
shuffle($items);
197+
// T preserved with updated key bound; without $this->withTypes() fix, T was dropped → list<int>
198+
assertType('T of array<int<0, max>, int> (method Bug14631\Bar::shuffleTemplateArray(), argument)&list', $items);
199+
return $items;
200+
}
201+
202+
/**
203+
* @template T of array<int>
204+
* @param T $items
205+
* @return T
206+
*/
207+
public function sortTemplateArray(array $items): array
208+
{
209+
sort($items);
210+
assertType('T of array<int<0, max>, int> (method Bug14631\Bar::sortTemplateArray(), argument)&list', $items);
211+
return $items;
212+
}
213+
214+
/**
215+
* @template T of array<int>
216+
* @param T $items
217+
* @return T
218+
*/
219+
public function usortTemplateArray(array $items): array
220+
{
221+
usort($items, static fn (int $a, int $b) => $a <=> $b);
222+
assertType('T of array<int<0, max>, int> (method Bug14631\Bar::usortTemplateArray(), argument)&list', $items);
223+
return $items;
224+
}
225+
226+
}
227+

0 commit comments

Comments
 (0)