Scope: use scope->getConstant instead#3666
Merged
ondrejmirtes merged 4 commits intophpstan:2.0.xfrom Dec 20, 2024
Merged
Conversation
scope->getConstant instead
staabm
commented
Nov 25, 2024
Comment on lines
-5730
to
-5732
| $versionExpr = new ConstFetch(new Name('PHP_VERSION_ID')); | ||
| if (!$this->hasExpressionType($versionExpr)->yes()) { |
Contributor
Author
There was a problem hiding this comment.
this previous code does not work in files without a namespace, e.g.
<?php // lint >= 7.4
if (PHP_VERSION_ID < 80000) {
\PHPStan\dumpType(PHP_VERSION_ID);
}the dumpType returns the correct value, but the scope uses a different expression name
$scope->debug();
array (
'\\PHP_VERSION_ID (Yes)' => 'int<50207, 79999>',
'native \\PHP_VERSION_ID (Yes)' => 'int<50207, 79999>',
)
vs. (note: file contains a namespace)
<?php // lint >= 7.4
namespace ANamespace;
if (PHP_VERSION_ID < 80000) {
\PHPStan\dumpType(PHP_VERSION_ID);
}$scope->debug();
array (
'PHP_VERSION_ID (Yes)' => 'int<50207, 79999>',
'native PHP_VERSION_ID (Yes)' => 'int<50207, 79999>',
)
Contributor
There was a problem hiding this comment.
Isn't \PHP_VERSION_ID const fetch enough?
Contributor
Author
There was a problem hiding this comment.
nope, thats the point of this PR
Collaborator
|
This pull request has been marked as ready for review. |
ondrejmirtes
requested changes
Nov 25, 2024
Member
ondrejmirtes
left a comment
There was a problem hiding this comment.
We should:
- Add (maybe private) method
getGlobalConstantTypein MutatingScope. - Refactor
hasConstantlogic that creates the correct ConstFetch - Reuse the logic from 2) in our
getPhpVersionmethod - Also add tests because without tests I don't trust any change :)
747d62e to
8095786
Compare
ondrejmirtes
requested changes
Nov 26, 2024
| return [ | ||
| [ | ||
| 'int<80000, 80499>', | ||
| __DIR__ . '/data/global-scope-constants.php', |
Member
There was a problem hiding this comment.
And a test for a namespaced file would be nice
db7d282 to
0eb7979
Compare
Member
|
Thank you. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
while doing some local tests, I realized the constant name was wrong.
I am not sure how to test this in isolation, therefore submitting without a test.
I think this will be covered after we use
$scope->getPhpVersion()in e.g. return type extensions.I realized there is a bug, as when testing non-namespaced files the PHP_VERSION_ID was not properly found within the scope