-
Notifications
You must be signed in to change notification settings - Fork 66
Type projections #138
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
Type projections #138
Changes from all commits
6e37881
7eb0dd5
ace9e07
dc25aa4
a3f6f04
1495f4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,24 +323,54 @@ public function parseGeneric(TokenIterator $tokens, Ast\Type\IdentifierTypeNode | |
{ | ||
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET); | ||
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); | ||
$genericTypes = [$this->parse($tokens)]; | ||
|
||
$genericTypes = []; | ||
$variances = []; | ||
|
||
[$genericTypes[], $variances[]] = $this->parseGenericTypeArgument($tokens); | ||
|
||
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); | ||
|
||
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) { | ||
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); | ||
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET)) { | ||
// trailing comma case | ||
return new Ast\Type\GenericTypeNode($baseType, $genericTypes); | ||
return new Ast\Type\GenericTypeNode($baseType, $genericTypes, $variances); | ||
} | ||
$genericTypes[] = $this->parse($tokens); | ||
[$genericTypes[], $variances[]] = $this->parseGenericTypeArgument($tokens); | ||
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); | ||
} | ||
|
||
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); | ||
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET); | ||
|
||
return new Ast\Type\GenericTypeNode($baseType, $genericTypes); | ||
return new Ast\Type\GenericTypeNode($baseType, $genericTypes, $variances); | ||
} | ||
|
||
|
||
/** | ||
* @phpstan-impure | ||
* @return array{Ast\Type\TypeNode, Ast\Type\GenericTypeNode::VARIANCE_*} | ||
*/ | ||
public function parseGenericTypeArgument(TokenIterator $tokens): array | ||
{ | ||
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_WILDCARD)) { | ||
return [ | ||
new Ast\Type\IdentifierTypeNode('mixed'), | ||
Ast\Type\GenericTypeNode::VARIANCE_BIVARIANT, | ||
]; | ||
} | ||
|
||
if ($tokens->tryConsumeTokenValue('contravariant')) { | ||
$variance = Ast\Type\GenericTypeNode::VARIANCE_CONTRAVARIANT; | ||
} elseif ($tokens->tryConsumeTokenValue('covariant')) { | ||
$variance = Ast\Type\GenericTypeNode::VARIANCE_COVARIANT; | ||
} else { | ||
$variance = Ast\Type\GenericTypeNode::VARIANCE_INVARIANT; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some unsupported covariance name like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It falls back to invariant and proceeds to parse the value as a type. Thus,
|
||
} | ||
|
||
$type = $this->parse($tokens); | ||
return [$type, $variance]; | ||
} | ||
|
||
|
||
|
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.
nit: tabs vs. spaces
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.
But which one is correct? It's pretty inconsistent across this whole file 😃