infer non-empty-list/array after isset($arr[$i])#4441
Conversation
0be85ea to
fe6216b
Compare
7d4d6af to
fd9b570
Compare
|
This pull request has been marked as ready for review. |
VincentLanglet
left a comment
There was a problem hiding this comment.
The getNative hack is definetly non acceptable since the bug will occurs if you add native typehint 'array' to the Bug7511.
namespace Bug7511;
use function PHPStan\Testing\assertType;
interface PositionEntityInterface {
public function getPosition(): int;
}
interface TgEntityInterface {}
abstract class HelloWorld
{
/**
* @phpstan-template T of PositionEntityInterface&TgEntityInterface
*
* @param array<T> $tgs
*
* @return array<T>
*
* @throws \Exception
*/
public function computeForFrontByPosition(array $tgs)
{
/** @phpstan-var array<T> $res */
$res = [];
assertType('T of Bug7511\PositionEntityInterface&Bug7511\TgEntityInterface (method Bug7511\HelloWorld::computeForFrontByPosition(), parameter)', $res[1]);
foreach ($tgs as $tgItem) {
$position = $tgItem->getPosition();
if (!isset($res[$position])) {
assertType('T of Bug7511\PositionEntityInterface&Bug7511\TgEntityInterface (method Bug7511\HelloWorld::computeForFrontByPosition(), argument)', $tgItem);
$res[$position] = $tgItem;
} else {
assertType('T of Bug7511\PositionEntityInterface&Bug7511\TgEntityInterface (method Bug7511\HelloWorld::computeForFrontByPosition(), argument)', $tgItem);
assertType('T of Bug7511\PositionEntityInterface&Bug7511\TgEntityInterface (method Bug7511\HelloWorld::computeForFrontByPosition(), parameter)', $res[$position]);
$tgItemToKeep = $this->compare($tgItem, $res[$position]);
assertType('T of Bug7511\PositionEntityInterface&Bug7511\TgEntityInterface (method Bug7511\HelloWorld::computeForFrontByPosition(), parameter)', $tgItemToKeep);
$res[$position] = $tgItemToKeep;
}
}
ksort($res);
assertType('array<T of Bug7511\PositionEntityInterface&Bug7511\TgEntityInterface (method Bug7511\HelloWorld::computeForFrontByPosition(), parameter)>', $res);
return $res;
}
/**
* @phpstan-template T of PositionEntityInterface&TgEntityInterface
*
* @param array<T> $tgs
*
* @return array<T>
*
* @throws \Exception
*/
public function computeForFrontByPosition2(array $tgs)
{
/** @phpstan-var array<T> $res */
$res = [];
assertType('array<T of Bug7511\PositionEntityInterface&Bug7511\TgEntityInterface (method Bug7511\HelloWorld::computeForFrontByPosition2(), parameter)>', $res);
foreach ($tgs as $tgItem) {
$position = $tgItem->getPosition();
assertType('T of Bug7511\PositionEntityInterface&Bug7511\TgEntityInterface (method Bug7511\HelloWorld::computeForFrontByPosition2(), parameter)', $res[$position]);
if (isset($res[$position])) {
assertType('T of Bug7511\PositionEntityInterface&Bug7511\TgEntityInterface (method Bug7511\HelloWorld::computeForFrontByPosition2(), parameter)', $res[$position]);
}
}
assertType('array<T of Bug7511\PositionEntityInterface&Bug7511\TgEntityInterface (method Bug7511\HelloWorld::computeForFrontByPosition2(), parameter)>', $res);
return $res;
}
/**
* @phpstan-template S of TgEntityInterface
* @phpstan-param S $nextTg
* @phpstan-param S $currentTg
* @phpstan-return S
*/
abstract protected function compare(TgEntityInterface $nextTg, TgEntityInterface $currentTg): TgEntityInterface;
}
|
@staabm with some debugging, I end up with The bug is created because We could eventually start by fix/improve I hope it helps you |
|
yeah, I agree. its a similar observeration that what we found out in #4441 (comment) maybe we need something like #3535 |
|
underlying generics problem was separated into phpstan/phpstan#14348 |
analog #4440 but for
isset($arr[$i])closes phpstan/phpstan#13674
closes phpstan/phpstan#12482
closes phpstan/phpstan#13675