-
Notifications
You must be signed in to change notification settings - Fork 569
Generics on phpstan-type aliases #5378
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
Draft
shmax
wants to merge
27
commits into
phpstan:2.1.x
Choose a base branch
from
shmax:generics-on-phpstan-types
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.
+1,282
−12
Draft
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
1e8ca68
first pass
shmax 69eb401
enforce required generic params
shmax 3db23c7
more tests
shmax 53e48f6
tests for invalud usage
shmax 3ef5f1b
fix regression
shmax 4611b40
add more scenarios to test fixture
shmax b0d71fc
remove empty patch
shmax 3037b23
coalesce to empty array
shmax a8e4978
lint
shmax ccbcd95
fix use function ordering
shmax 09cda43
fix generic alias resolution: skip alias path when name resolves to a…
shmax edc20cd
remove resolveWithDefaults: bare generic alias usage restores old Tem…
shmax 69f8848
fix phpstan self-analysis errors: ignore property.notFound, add null …
shmax 19d3a8b
remove ignore
shmax ab6ca93
add property.notFound baseline entry for PHP < 8.0 (phpdoc-parser lac…
shmax dc0e04c
move property.notFound baseline entry to universal phpstan-baseline.n…
shmax 6a66921
fix generic alias bare-usage resolution and data-provider parameter s…
shmax 9a0f93f
update baseline after rebase onto 2.1.x
shmax b953d29
add temp patches
shmax c9ee238
use LateResolvableType
shmax da61755
remove
shmax ad70992
lint
shmax 406c762
fix array vs list error
shmax 154d016
remove dead code
shmax ed1adfd
fold getRawGenericTypeAliasesUsage into getNonGenericObjectTypesWithG…
shmax 7ea0af4
update lock hash
shmax 703a9a8
lint
shmax 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
Binary file not shown.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,39 @@ | ||
| --- src/Parser/PhpDocParser.php | ||
| +++ src/Parser/PhpDocParser.php | ||
| @@ -1067,6 +1067,21 @@ | ||
| $alias = $tokens->currentTokenValue(); | ||
| $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER); | ||
|
|
||
| + $templateTypes = []; | ||
| + if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) { | ||
| + do { | ||
| + $startLine = $tokens->currentTokenLine(); | ||
| + $startIndex = $tokens->currentTokenIndex(); | ||
| + $templateTypes[] = $this->enrichWithAttributes( | ||
| + $tokens, | ||
| + $this->typeParser->parseTemplateTagValue($tokens), | ||
| + $startLine, | ||
| + $startIndex, | ||
| + ); | ||
| + } while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)); | ||
| + $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET); | ||
| + } | ||
| + | ||
| // support phan-type/psalm-type syntax | ||
| $tokens->tryConsumeTokenType(Lexer::TOKEN_EQUAL); | ||
|
|
||
| @@ -1087,12 +1102,13 @@ | ||
| } | ||
| } | ||
|
|
||
| - return new Ast\PhpDoc\TypeAliasTagValueNode($alias, $type); | ||
| + return new Ast\PhpDoc\TypeAliasTagValueNode($alias, $type, $templateTypes); | ||
| } catch (ParserException $e) { | ||
| $this->parseOptionalDescription($tokens, false); | ||
| return new Ast\PhpDoc\TypeAliasTagValueNode( | ||
| $alias, | ||
| $this->enrichWithAttributes($tokens, new Ast\Type\InvalidTypeNode($e), $startLine, $startIndex), | ||
| + $templateTypes, | ||
| ); | ||
| } | ||
| } |
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,47 @@ | ||
| --- src/Ast/PhpDoc/TypeAliasTagValueNode.php | ||
| +++ src/Ast/PhpDoc/TypeAliasTagValueNode.php | ||
| @@ -4,6 +4,7 @@ | ||
|
|
||
| use PHPStan\PhpDocParser\Ast\NodeAttributes; | ||
| use PHPStan\PhpDocParser\Ast\Type\TypeNode; | ||
| +use function implode; | ||
| use function trim; | ||
|
|
||
| class TypeAliasTagValueNode implements PhpDocTagValueNode | ||
| @@ -15,15 +16,25 @@ | ||
|
|
||
| public TypeNode $type; | ||
|
|
||
| - public function __construct(string $alias, TypeNode $type) | ||
| + /** @var TemplateTagValueNode[] */ | ||
| + public array $templateTypes; | ||
| + | ||
| + /** | ||
| + * @param TemplateTagValueNode[] $templateTypes | ||
| + */ | ||
| + public function __construct(string $alias, TypeNode $type, array $templateTypes = []) | ||
| { | ||
| $this->alias = $alias; | ||
| $this->type = $type; | ||
| + $this->templateTypes = $templateTypes; | ||
| } | ||
|
|
||
| public function __toString(): string | ||
| { | ||
| - return trim("{$this->alias} {$this->type}"); | ||
| + $templateTypes = $this->templateTypes !== [] | ||
| + ? '<' . implode(', ', $this->templateTypes) . '>' | ||
| + : ''; | ||
| + return trim("{$this->alias}{$templateTypes} {$this->type}"); | ||
| } | ||
|
|
||
| /** | ||
| @@ -31,7 +42,7 @@ | ||
| */ | ||
| public static function __set_state(array $properties): self | ||
| { | ||
| - $instance = new self($properties['alias'], $properties['type']); | ||
| + $instance = new self($properties['alias'], $properties['type'], $properties['templateTypes'] ?? []); | ||
| if (isset($properties['attributes'])) { | ||
| foreach ($properties['attributes'] as $key => $value) { | ||
| $instance->setAttribute($key, $value); |
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
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.
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.
I don't think we need this BC coverage. It's true that
MyList<string>would be resolved to an actual class even ifMyListtype alias existed, but who the hell would do that...