Skip to content

Commit

Permalink
Fix wrongly convertion of list<T> to array{T}
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed Sep 7, 2024
1 parent 3592cf0 commit b3c99fb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,8 @@ private function turnListIntoConstantArray(FuncCall $countFuncCall, Type $type,
}
$valueTypesBuilder->setOffsetValueType($offsetType, $type->getOffsetValueType($offsetType), !$hasOffset->yes());
}

} else {
return null;
}

$arrayType = $valueTypesBuilder->getArray();
Expand Down
44 changes: 44 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11642.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace Bug11642;

use function PHPStan\Testing\assertType;

class Repository
{
/**
* @psalm-param array<string|int, mixed> $criteria
*
* @return object[] The objects.
* @psalm-return list<\stdClass>
*/
function findBy(array $criteria): array
{
return [new \stdClass, new \stdCLass, new \stdClass, new \stdClass];
}
}

class Payload {
/** @var non-empty-list<string> */
public array $ids = ['one', 'two'];
}

function doFoo() {
$payload = new Payload();

$fetcher = new Repository();
$entries = $fetcher->findBy($payload->ids);
assertType('list<stdClass>', $entries);
assertType('int<0, max>', count($entries));
assertType('int<1, max>', count($payload->ids));
if (count($entries) !== count($payload->ids)) {
exit();
}

assertType('non-empty-list<stdClass>', $entries);
if (count($entries) > 3) {
throw new \RuntimeException();
}

assertType('non-empty-list<stdClass>', $entries);
}

0 comments on commit b3c99fb

Please sign in to comment.