-
Notifications
You must be signed in to change notification settings - Fork 506
Fix narrowing of superglobals #3901
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
Conversation
assertType('array', $GLOBALS); | ||
assertNativeType('array<mixed>', $GLOBALS); |
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.
these 2 fail without the adaptions here
phpstan-doctrine failures are unrelated, those seemed to have appeared because of recent phpstan improvements, opened phpstan/phpstan-doctrine#650 for it |
@@ -1001,4 +1001,9 @@ public function testHashing(): void | |||
]); | |||
} | |||
|
|||
public function testBug12772(): void |
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.
Please add also $_ENV['CI']
test explicitly, as I want to make sure GitHub CI environment variable is never assumed to exist on runtime - https://github.com/atk4/ui/blob/235416b31066827226e462f1916d9fc99c0d14b4/demos/init-db.php#L556. And thank you very much for looking into this issue!
Our reproducible environment is the same as here, GitHub CI, so as long as the tests passes here, our should pass then too.
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.
I get your point, but adaptions here are not related to any environment variables. If Ondřej is fine with this, I expect it to be merged very soon and then we should also see the effects
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.
thanks for looking into it
src/Analyser/MutatingScope.php
Outdated
|
||
$superglobalType = new ArrayType(new BenevolentUnionType([new IntegerType(), new StringType()]), new MixedType(true)); | ||
$this->expressionTypes[$varExprString] = ExpressionTypeHolder::createYes(new Variable($variableName), $superglobalType); | ||
$this->nativeExpressionTypes[$varExprString] = ExpressionTypeHolder::createYes(new Variable($variableName), $superglobalType); |
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.
I don't get why the fix looks like this.
- This is polluting
$this->expressionTypes
with any superglobal in the analysed code. - It's mutating Scope in a getter method which is a big red-flag.
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.
good point, I don't understand how this even worked before without adding an expression tbh :P the idea of setting the expression for proper narrowing came from #3762 (comment) basically.
not sure if relevant for the bug or fix, but after the expression is created it is dealing with late resolveable types still in the same method in
phpstan-src/src/Analyser/MutatingScope.php
Line 566 in a11741d
return TypeUtils::resolveLateResolvableTypes($this->expressionTypes[$varExprString]->getType()); |
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.
looks like I can't get around step-debugging this issue :D I assume the expressions are just overriding something that is incorrectly set and retained or so? let's see..
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.
I found a better fix, the root issue has to be addressed in mergeVariableHolder. Stay tuned.
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.
See 9710ba5
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.
oh cool, thx!
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.
those variable type holders were the missing piece for me I guess
a11741d
to
9710ba5
Compare
9710ba5
to
f68cc6f
Compare
Thank you. |
thank you for adapting the fix to be proper :) |
Improvements of #3871, pulling in parts of #3762 basically
Fixes phpstan/phpstan#12771
Fixes phpstan/phpstan#12772
Maybe fixes phpstan/phpstan#12776
I did not take over the changes where the expressions are passed over through classes, functions and namespaces in scopes. Those would deal with some edge cases, but not sure if it's worth.
fyi @staabm