Skip to content

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

Merged
merged 1 commit into from
Mar 24, 2025

Conversation

herndlm
Copy link
Contributor

@herndlm herndlm commented Mar 24, 2025

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

Comment on lines 47 to 48
assertType('array', $GLOBALS);
assertNativeType('array<mixed>', $GLOBALS);
Copy link
Contributor Author

@herndlm herndlm Mar 24, 2025

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

@herndlm
Copy link
Contributor Author

herndlm commented Mar 24, 2025

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
Copy link
Contributor

@mvorisek mvorisek Mar 24, 2025

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.

Copy link
Contributor Author

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

Copy link
Contributor

@staabm staabm left a 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


$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);
Copy link
Member

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.

  1. This is polluting $this->expressionTypes with any superglobal in the analysed code.
  2. It's mutating Scope in a getter method which is a big red-flag.

Copy link
Contributor Author

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

return TypeUtils::resolveLateResolvableTypes($this->expressionTypes[$varExprString]->getType());

Copy link
Contributor Author

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..

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 9710ba5

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh cool, thx!

Copy link
Contributor Author

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

@ondrejmirtes ondrejmirtes force-pushed the superglobal-narrowing branch from a11741d to 9710ba5 Compare March 24, 2025 10:38
@herndlm herndlm closed this Mar 24, 2025
@herndlm herndlm reopened this Mar 24, 2025
@ondrejmirtes ondrejmirtes force-pushed the superglobal-narrowing branch from 9710ba5 to f68cc6f Compare March 24, 2025 10:41
@ondrejmirtes ondrejmirtes merged commit 02eb0a8 into phpstan:2.1.x Mar 24, 2025
415 of 417 checks passed
@ondrejmirtes
Copy link
Member

Thank you.

@herndlm
Copy link
Contributor Author

herndlm commented Mar 24, 2025

thank you for adapting the fix to be proper :)

@herndlm herndlm deleted the superglobal-narrowing branch March 24, 2025 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants