-
Notifications
You must be signed in to change notification settings - Fork 569
Generalize dynamicConstantNames constants whose type cannot be widened by generalize() to mixed
#5457
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
staabm
merged 6 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-zwukgp1
Apr 15, 2026
+23
−1
Merged
Generalize dynamicConstantNames constants whose type cannot be widened by generalize() to mixed
#5457
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
da1012f
Generalize `dynamicConstantNames` constants whose type cannot be wide…
ondrejmirtes dddb9a8
Use @var PHPDoc type for dynamicConstantNames instead of falling back…
phpstan-bot 62e0514
Revert generalize-to-mixed fallback, keep only PHPDoc type support
phpstan-bot e8939f4
Remove tests for constants without PHPDoc (null, true, false, empty a…
phpstan-bot f354773
Remove bug reference comment from test
phpstan-bot 794d8fe
Add test for PHPDoc type incompatible with native value
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we test a phpdoc type which is incompatible with the native type?
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.
Done. Added a test for a PHPDoc type that is incompatible with the native value:
DYNAMIC_INCOMPATIBLE_PHPDOC_CONSTANT— has@var intbut valuenull. When listed indynamicConstantNames, it resolves toint(the PHPDoc type is used as-is).This documents that the PHPDoc type takes priority regardless of compatibility with the actual constant value, which is consistent with how the explicit type path (key-value form in
dynamicConstantNames) already works. All tests pass (11857 tests, 79365 assertions) and PHPStan reports no errors.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.
A incompatible phpdoc should not take precedent over the native type
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 processed this review but have nothing to report.
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 agree with this @staabm.
When you write
we consider that the method return an int.
Also, see this example https://phpstan.org/r/b42fa21e-9e96-481f-8cc2-dc623a40c893
I think it's correct for PHPStan to consider that the phpdoc is true and we still report an error on level 2 https://phpstan.org/r/48a73981-93c4-4055-b2e2-c82fe5f5a4c6 for the incompatibility.
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.
All tests pass (11857 tests, 79365 assertions) and PHPStan reports no errors.
VincentLanglet's latest review is not requesting any code changes — they're defending the current implementation against staabm's suggestion that an incompatible PHPDoc should not take precedence. VincentLanglet argues (correctly) that this is consistent with how PHPStan already handles
@returntypes on methods: the PHPDoc type is trusted, and any incompatibility with the actual value is reported as a separate error at level 2. The PR has already been approved by VincentLanglet.No code changes are needed for this review comment. The current implementation and tests are correct.
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.
Convinced. :)
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.
The branch is clean and up to date. Both staabm and VincentLanglet have approved the PR. No code changes are needed — staabm's review comment "Convinced. :)" is acknowledging VincentLanglet's argument about PHPDoc type precedence, and the approval note "Please adjust the commit message on merge" is a request for when the PR is actually merged (squash merge commit message), not a code change.
There's nothing to commit or push here. The PR is approved and ready for merge with an adjusted commit message.