-
Notifications
You must be signed in to change notification settings - Fork 554
Fix phpstan/phpstan#14211: incorrect type inference with dependent types #5092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.1.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3833,7 +3833,15 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self | |
| } | ||
|
|
||
| $conditions[$conditionalExprString][] = $conditionalExpression; | ||
| $specifiedExpressions[$conditionalExprString] = $conditionalExpression->getTypeHolder(); | ||
| $newTypeHolder = $conditionalExpression->getTypeHolder(); | ||
| if (array_key_exists($conditionalExprString, $specifiedExpressions) && $specifiedExpressions[$conditionalExprString]->getCertainty()->yes() && $newTypeHolder->getCertainty()->yes()) { | ||
| $specifiedExpressions[$conditionalExprString] = ExpressionTypeHolder::createYes( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ExpressionTypeHolder has a method Should we introduce a new method for ExpressionTypeHolder which does the intersect ? |
||
| $newTypeHolder->getExpr(), | ||
| TypeCombinator::intersect($specifiedExpressions[$conditionalExprString]->getType(), $newTypeHolder->getType()), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might add a lot of possibly useless intersect calls. For instance if the next newTypeHolder does not have a certainty of Yes. we will override the value. Might be better to check it first by computing all the newTypeHolder and
WDYT @staabm ? |
||
| ); | ||
| } else { | ||
| $specifiedExpressions[$conditionalExprString] = $newTypeHolder; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Bug14211; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** @param array<mixed> $data */ | ||
| function doSomething(array $data): bool { | ||
|
|
||
| if (!isset($data['x'])) | ||
| return false; | ||
|
|
||
| $m = isset($data['y']); | ||
|
|
||
| if ($m) { | ||
| assertType('true', $m); | ||
| } | ||
| assertType('bool', $m); | ||
|
|
||
| if ($m) { | ||
| assertType('true', $m); | ||
| } | ||
|
|
||
| return true; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unclear to me why we're only concerned about Yes & Yes.
Looking at this code, the last
newTypeHolderoverride all the previous one if there is some Maybe certainty in the game.Is this condition clear to you @staabm ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updating the code to
does not creates any failure. I feel like we're lacking of coverage. But I never worked with ExpressionTypeHolder...