Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
ondrejmirtes merged 1 commit into phpstan:2.1.x from herndlm:superglobal-narrowing
Mar 24, 2025

Conversation

@herndlm
Copy link
Contributor

@herndlm herndlm commented Mar 24, 2025
edited
Loading

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
edited
Loading

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

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

]);
}

public function testBug12772(): void
Copy link
Contributor

@mvorisek mvorisek Mar 24, 2025
edited
Loading

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

herndlm reacted with thumbs up emoji

$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

@ondrejmirtes ondrejmirtes Mar 24, 2025

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

@ondrejmirtes ondrejmirtes Mar 24, 2025

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

@ondrejmirtes ondrejmirtes Mar 24, 2025

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 merged commit 02eb0a8 into phpstan:2.1.x Mar 24, 2025
415 of 417 checks passed
Copy link
Member

Thank you.

Copy link
Contributor Author

herndlm commented Mar 24, 2025

thank you for adapting the fix to be proper :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ondrejmirtes ondrejmirtes ondrejmirtes requested changes

+2 more reviewers

@mvorisek mvorisek mvorisek left review comments

@staabm staabm staabm approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

AltStyle によって変換されたページ (->オリジナル) /