Skip to content
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

Null-safe operator loses type in loop #9721

Closed
leongersen opened this issue Aug 4, 2023 · 3 comments
Closed

Null-safe operator loses type in loop #9721

leongersen opened this issue Aug 4, 2023 · 3 comments
Labels
Milestone

Comments

@leongersen
Copy link
Contributor

Bug report

These expressions should be equivalent (when the result of mergeWith cannot be null):

$mergedExample = $mergedExample !== null ? $mergedExample->mergeWith() : $example;
$mergedExample = $mergedExample?->mergeWith() ?? $example;

The expression with the null safe operator results in mixed for $mergedExample, the ternary expression is analysed correctly.

Code snippet that reproduces the problem

https://phpstan.org/r/a88e69f2-8ca7-471b-beb4-fd427772df0a

Expected output

The dumped type for $mergedExample should be Example.

Did PHPStan help you today? Did it make you happy in any way?

No response

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Aug 6, 2023
@phpstan-bot
Copy link
Contributor

@leongersen After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
 17: Dumped type: Example
-21: Dumped type: mixed
+21: Dumped type: Example
Full report
Line Error
17 Dumped type: Example
21 Dumped type: Example

@leongersen
Copy link
Contributor Author

Thank you @ondrejmirtes 😄

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants