-
Notifications
You must be signed in to change notification settings - Fork 471
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
Scope: use scope->getConstant
instead
#3666
Conversation
scope->getConstant
instead
$versionExpr = new ConstFetch(new Name('PHP_VERSION_ID')); | ||
if (!$this->hasExpressionType($versionExpr)->yes()) { |
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.
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>',
)
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.
Isn't \PHP_VERSION_ID
const fetch enough?
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.
nope, thats the point of this PR
This pull request has been marked as ready for review. |
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.
We should:
- Add (maybe private) method
getGlobalConstantType
in MutatingScope. - Refactor
hasConstant
logic that creates the correct ConstFetch - Reuse the logic from 2) in our
getPhpVersion
method - Also add tests because without tests I don't trust any change :)
747d62e
to
8095786
Compare
return [ | ||
[ | ||
'int<80000, 80499>', | ||
__DIR__ . '/data/global-scope-constants.php', |
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.
And a test for a namespaced file would be nice
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.
here you are
db7d282
to
0eb7979
Compare
Thank you. |
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