Fix non-existent offset when guarding#4539
Conversation
| if ( | ||
| $context->true() | ||
| && $expr instanceof Node\Expr\BinaryOp\Smaller | ||
| && $argType->isList()->yes() | ||
| && IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($leftType)->yes() | ||
| ) { | ||
| $dimFetch = new ArrayDimFetch( | ||
| $expr->right->getArgs()[0]->value, | ||
| $expr->left, | ||
| ); | ||
| $result = $result->unionWith( | ||
| $this->create( | ||
| $dimFetch, | ||
| $argType->getIterableValueType(), | ||
| TypeSpecifierContext::createTrue(), | ||
| $scope, | ||
| ) | ||
| ); | ||
| } |
There was a problem hiding this comment.
is this PR trying to fix phpstan/phpstan#13773 ?
(since the PR does not at all contain a description.. I can only guess)
if so, you should either adjust the logic added with #4403 or maybe even delete it in case you want to re-implement it in a different place
There was a problem hiding this comment.
Thanks for pointing this out! I updated the description of this pr.
I have not opened a issue for this pr. But i will also try and fix phpstan/phpstan#13773 later in a diffrent pr. Here i am trying to fix array access on lists if we explicitly guard the index.
|
There seems to be a regression This did work before this pr. I am wondering if this is also true for the other checks in the method i changed. |
| if (count($c) > 0) { | ||
| $c = array_map(fn() => new stdClass(), $c); | ||
| assertType('non-empty-list<stdClass>', $c); | ||
| assertType('non-empty-list<stdClass>&hasOffsetValue(0, stdClass)', $c); |
There was a problem hiding this comment.
this type does not make sense, because non-empty-list<stdClass> already implies that the list has a offset 0 with value stdClass
dc64635 to
2c7e290
Compare
| $context->true() | ||
| && $expr instanceof Node\Expr\BinaryOp\Smaller | ||
| && $argType->isList()->yes() | ||
| && $argExpr instanceof Expr\Variable |
There was a problem hiding this comment.
instanceof Expr\Variable is usually a bug as most logic works for other expressions (e.g. a pure function call can also be remembered).
most of the time this works just by dropping this line
| $this->create( | ||
| $dimFetch, | ||
| $argType->getIterableValueType(), | ||
| TypeSpecifierContext::createTrue(), |
There was a problem hiding this comment.
usually we pass thru the $context, instead of creating a new one.
|
This PR looks abandonded. Feel free to reopen on latest 2.1.x if you still want to fix it. |
This pr is trying to fix a false positive.
Note that 1) $array is a list 2) $index is positive (or at least >= 0)