-
Notifications
You must be signed in to change notification settings - Fork 523
Test the ability to define global types with ExpressionTypeResolverExtension
#4233
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
base: 2.1.x
Are you sure you want to change the base?
Conversation
src/Analyser/NodeScopeResolver.php
Outdated
@@ -1965,7 +1965,7 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void { | |||
continue; | |||
} | |||
|
|||
$scope = $scope->assignVariable($var->name, new MixedType(), new MixedType(), TrinaryLogic::createYes()); |
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.
are you sure this change is necessary?
I think it causes the test 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.
I assume without this it's always considered as MixedType
and do not use the ExpressionTypeResolverExtension
Looking at the failing test, it need to add an extra check to transform ErrorType into MixedType.
But I think the scope::getType
is "just" here to apply
foreach ($this->expressionTypeResolverExtensionRegistry->getExtensions() as $extension) {
$type = $extension->getType($var, $scope);
if ($type !== null) {
return $type;
}
}
so it seems better to write
$type = null;
foreach ($this->expressionTypeResolverExtensionRegistry->getExtensions() as $extension) {
$type = $extension->getType($var, $scope);
if ($type !== null) {
break;
}
}
$scope = $scope->assignVariable($var->name, $type ?? new MixedType(), new MixedType(), TrinaryLogic::createYes());
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 PRs tests do not fail when we delete this change here, therefore it seems either the tests are wonky or phpstan already works like expected before this PR
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.
Indeed. It looks like I missed something. At first I did non found in the documentation how to specify global types, so I did some reverse engeneering to see if it was possible. I understood the modified line as a force MixedType
that cannot be altered by any extension, but it seems that I was wrong. I will do some tests on my own extension, to see if I can achieve what I want to do.
Also, I did not found any documentation related to the usage of ExpressionTypeResolverExtension
. Maybe it should be documented, as it can be really usefull for extension developers.
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 ExpressionTypeResolverExtension
works as expected, as it permits to force any type during a variable declaration, but it does not completely fits my needs as I am not able to differenciate a global var statement from a function parameter.
Indeed both global $MY_VAR;
and function foo ($MY_VAR) {}
are similar from the extension point of view. However, it is probably out of scope of the current PR and I will use some hacks to achieve my goals.
I removed the modified line. Feel free to close this PR if the new test is considered useless.
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.
@cedric-anne please add the example (and failling assertions) which does not work as expected into your test-case of this PR, so we can look into what needs to be done to make it pass
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 failing test case would be the following:
function bar() {
global $MY_FRAMEWORK_GLOBAL;
// This one works as expected.
assertType('mixed', $MY_FRAMEWORK_GLOBAL);
}
function foo($MY_FRAMEWORK_GLOBAL) {
// I have no idea how I could distinguish the `$MY_FRAMEWORK_GLOBAL` parameter definition
// from a `global $MY_FRAMEWORK_GLOBAL` statement in my `GlobalExpressionTypeResolverExtension::getType()` method.
//
// The following assert with fail, as the extension currently defines it as a `bool`.
assertType('mixed', $MY_FRAMEWORK_GLOBAL);
}
As far as I understand, it is not possible to access the parent PhpParser\Node\Stmt\Global_
statement of the PhpParser\Node\Expr\Variable
expression. Without this, there is no way to distinguish a global statement like the one in the bar()
function from a parameter definition like the one in the foo()
function.
IMHO, we can consider that the extension works as expected, from the PHPStan point of view. There is no bug here, just a limitation.
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 think it might be possible to turn $scope->isGlobalVariable()
public, after we added support for recognizing the global
keyword.
one idea which comes to mind is, to mark variables which are preceeded by Global_
AST node with a attribute and in MutatingScope->isGlobalVariable
look whether the given attribute is set.
see e.g. src/Parser/ArrayFilterArgVisitor.php
for inspiration on how this can be achieved.
beware: ondrej might have a different idea of the problem and might not be happy with the above suggestion :)
This pull request has been marked as ready for review. |
ExpressionTypeResolverExtension
ExpressionTypeResolverExtension
See phpstan/phpstan#13243.
Being able to act on global statements through the
ExpressionTypeResolverExtension
may help to improve static analysis on applications that are massively using global variables.I work in an ecosystem with a huge number of plugins. Our long-term goal is to eliminate the use of these global variables, but in the meantime, we want to improve code analysis by enforcing their typing. This will enable us to detect method calls using incorrect parameters, calls to deprecated methods, etc.
Some tests are broken, and I do not yet understand why. A little help on this part would be appreciated.