-
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
Open
cedric-anne
wants to merge
2
commits into
phpstan:2.1.x
Choose a base branch
from
cedric-anne:feature/global-node-type
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+43
−1
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
27 changes: 27 additions & 0 deletions
27
tests/PHPStan/Analyser/data/GlobalExpressionTypeResolverExtension.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace ExpressionTypeResolverExtension; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Variable; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Type\BooleanType; | ||
use PHPStan\Type\ExpressionTypeResolverExtension; | ||
use PHPStan\Type\Type; | ||
|
||
class GlobalExpressionTypeResolverExtension implements ExpressionTypeResolverExtension { | ||
|
||
public function getType(Expr $expr, Scope $scope): ?Type | ||
{ | ||
if (!$expr instanceof Variable) { | ||
return null; | ||
} | ||
|
||
if ($expr->name === 'MY_FRAMEWORK_GLOBAL') { | ||
return new BooleanType(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
tests/PHPStan/Analyser/data/expression-type-resolver-extension-global-statement.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
// test file for ExpressionTypeResolverExtensionTest | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
global $MY_FRAMEWORK_GLOBAL, $ANOTHER_GLOBAL; | ||
|
||
assertType('bool', $MY_FRAMEWORK_GLOBAL); | ||
assertType('mixed', $ANOTHER_GLOBAL); |
File renamed without changes.
4 changes: 4 additions & 0 deletions
4
tests/PHPStan/Analyser/expression-type-resolver-extension.neon
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.
Uh oh!
There was an error while loading. Please reload this page.
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 ExpressionTypeResolverExtensionLooking 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 applyso it seems better to write
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;
andfunction 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.
Uh oh!
There was an error while loading. Please reload this page.
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:
As far as I understand, it is not possible to access the parent
PhpParser\Node\Stmt\Global_
statement of thePhpParser\Node\Expr\Variable
expression. Without this, there is no way to distinguish a global statement like the one in thebar()
function from a parameter definition like the one in thefoo()
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.
Uh oh!
There was an error while loading. Please reload this page.
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 theglobal
keyword.one idea which comes to mind is, to mark variables which are preceeded by
Global_
AST node with a attribute and inMutatingScope->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 :)