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

add failing test from https://github.com/phpstan/phpstan/issues/8467 #2061

Merged
merged 4 commits into from Dec 8, 2022

Conversation

rajyan
Copy link
Contributor

@rajyan rajyan commented Dec 7, 2022

@rajyan
Copy link
Contributor Author

rajyan commented Dec 7, 2022

@ondrejmirtes
If my understanding is correct, this test should pass with 1.9.2, but it doesn't 🤔
I think I'm missing something...

@ondrejmirtes
Copy link
Member

Yeah, I probably didn't explain myself clearly.

The situation in Composer sources is complicated.

I came up with a simple example. I also thought it'd pass on 1.9.2 but it doesn't.

But if we fix it, it's gonna fix the problem in Composer too I think.

Because you made conditional types smarter, this situation is now happening in more places.

I hope this reasoning is more clear than before 😊

@rajyan
Copy link
Contributor Author

rajyan commented Dec 7, 2022

But if we fix it, it's gonna fix the problem in Composer too I think.
Because you made conditional types smarter, this situation is now happening in more places.
I hope this reasoning is more clear than before

I see. Thanks, I understand.
I haven't yet figured out the cause of the problem, but I'll look for it 👍

@ondrejmirtes
Copy link
Member

My theory is that we might need to disable this 35db779 for !$this->polluteScopeWithAlwaysIterableForeach.

@rajyan
Copy link
Contributor Author

rajyan commented Dec 7, 2022

This is one idea to solve the current problem.
Reading
https://phpstan.org/config-reference#pollutescopewithalwaysiterableforeach
I thought that certainty "no" is better for foreach value and key outside the loop?

@ondrejmirtes
Copy link
Member

You need "maybe" so that people can ask about it with isset. And for backward compatibility because there are gonna be baselines that have "might not be defined" in their messages.

"no" would not be precise, because that's not how PHP works. Even with the stricter setting (polluteScopeWithAlwaysIterableForeach: false).

@rajyan
Copy link
Contributor Author

rajyan commented Dec 7, 2022

Fair enough. I'll look for another solution.
The conditional expression is created in createConditionalExpressions of mergeWith, so invalidating them should be enough.

@rajyan
Copy link
Contributor Author

rajyan commented Dec 7, 2022

My theory is that we might need to disable this 35db779 for !$this->polluteScopeWithAlwaysIterableForeach.

Sorry, I read this comment now. Yeah, I think so too.

@rajyan
Copy link
Contributor Author

rajyan commented Dec 8, 2022

@ondrejmirtes
Copy link
Member

I've realized there's a problem. I think it'd be a nice idea to have a new TypeInferenceTestCase subclass, and set polluteScopeWithAlwaysIterableForeach: false in getAdditionalConfigFiles.

That way we can test it fully and not just through DefinedVariableRuleTest.

Because even with polluteScopeWithAlwaysIterableForeach: false, we want this to pass:

// $a is array
foreach ($a as $v) {
    // $a is non-empty-array
}

But where the problem is lies (and what might be hard to fix) is that the conditional types shouldn't create the connection (if $a is non-empty again then $v definitely exists, or something like that).

The current solution might be fine if we don't come up with anything better, but let's try :)

The problem in PocketMine-MP was already ignored, but with a different message probably (non-empty-array).

@ondrejmirtes
Copy link
Member

I've just had breakfast and changed my mind - I want to try this in practice :) Feel free to mark this current changeset as ready, I'm gonna merge it, and if you come up with any ideas how to improve it, we can try it out, but I think the current state is okay to try out :)

@rajyan
Copy link
Contributor Author

rajyan commented Dec 8, 2022

The problem in PocketMine-MP was already ignored, but with a different message probably (non-empty-array).

This was a good news, the only concern for me was this test change.

@rajyan rajyan marked this pull request as ready for review December 8, 2022 08:32
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@ondrejmirtes ondrejmirtes merged commit e4e5aa9 into phpstan:1.9.x Dec 8, 2022
@ondrejmirtes
Copy link
Member

Thank you :)

@rajyan
Copy link
Contributor Author

rajyan commented Dec 8, 2022

I'll add this to TODOs
phpstan/phpstan#8360 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants