Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ public function shiftArray(): Type

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

public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
Expand Down
49 changes: 49 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14631.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,52 @@ public function ksortArray(array $items): array
}

}

/**
* Cases where T is bounded by a plain array (TemplateArrayType),
* so T is directly the subject of shuffleArray() in ArrayType.
* These verify the $this->withTypes() fix rather than the IntersectionType fix.
*/
class Bar
{

/**
* @template T of array<int>
* @param T $items
* @return T
*/
public function shuffleTemplateArray(array $items): array
{
assertType('T of array<int> (method Bug14631\Bar::shuffleTemplateArray(), argument)', $items);
shuffle($items);
// T preserved with updated key bound; without $this->withTypes() fix, T was dropped → list<int>
assertType('T of array<int<0, max>, int> (method Bug14631\Bar::shuffleTemplateArray(), argument)&list', $items);
return $items;
}

/**
* @template T of array<int>
* @param T $items
* @return T
*/
public function sortTemplateArray(array $items): array
{
sort($items);
assertType('T of array<int<0, max>, int> (method Bug14631\Bar::sortTemplateArray(), argument)&list', $items);
return $items;
}

/**
* @template T of array<int>
* @param T $items
* @return T
*/
public function usortTemplateArray(array $items): array
{
usort($items, static fn (int $a, int $b) => $a <=> $b);
assertType('T of array<int<0, max>, int> (method Bug14631\Bar::usortTemplateArray(), argument)&list', $items);
return $items;
}

}

Loading