-
Notifications
You must be signed in to change notification settings - Fork 65
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 4 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 |
---|---|---|
|
@@ -28,7 +28,11 @@ Atomic | |
/ TokenParenthesesOpen ParenthesizedType TokenParenthesesClose [Array] | ||
|
||
Generic | ||
= TokenAngleBracketOpen Type *(TokenComma Type) TokenAngleBracketClose | ||
= TokenAngleBracketOpen GenericTypeArgument *(TokenComma GenericTypeArgument) TokenAngleBracketClose | ||
|
||
GenericTypeArgument | ||
= [TokenContravariant / TokenCovariant] Type | ||
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. nit: tabs vs. spaces 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. But which one is correct? It's pretty inconsistent across this whole file 😃 |
||
/ TokenWildcard | ||
|
||
Callable | ||
= TokenParenthesesOpen [CallableParameters] TokenParenthesesClose TokenColon CallableReturnType | ||
|
@@ -188,6 +192,15 @@ TokenIs | |
TokenNot | ||
= %s"not" 1*ByteHorizontalWs | ||
|
||
TokenContravariant | ||
= %s"contravariant" 1*ByteHorizontalWs | ||
|
||
TokenCovariant | ||
= %s"covariant" 1*ByteHorizontalWs | ||
|
||
TokenWildcard | ||
= "*" *ByteHorizontalWs | ||
|
||
TokenIdentifier | ||
= [ByteBackslash] ByteIdentifierFirst *ByteIdentifierSecond *(ByteBackslash ByteIdentifierFirst *ByteIdentifierSecond) *ByteHorizontalWs | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,16 @@ | |
|
||
use PHPStan\PhpDocParser\Ast\NodeAttributes; | ||
use function implode; | ||
use function sprintf; | ||
|
||
class GenericTypeNode implements TypeNode | ||
{ | ||
|
||
public const VARIANCE_INVARIANT = 'invariant'; | ||
public const VARIANCE_COVARIANT = 'covariant'; | ||
public const VARIANCE_CONTRAVARIANT = 'contravariant'; | ||
public const VARIANCE_BIVARIANT = 'bivariant'; | ||
|
||
use NodeAttributes; | ||
|
||
/** @var IdentifierTypeNode */ | ||
|
@@ -16,16 +22,33 @@ class GenericTypeNode implements TypeNode | |
/** @var TypeNode[] */ | ||
public $genericTypes; | ||
|
||
public function __construct(IdentifierTypeNode $type, array $genericTypes) | ||
/** @var (self::VARIANCE_*)[] */ | ||
public $variances; | ||
|
||
public function __construct(IdentifierTypeNode $type, array $genericTypes, array $variances = []) | ||
{ | ||
$this->type = $type; | ||
$this->genericTypes = $genericTypes; | ||
$this->variances = $variances; | ||
} | ||
|
||
|
||
public function __toString(): string | ||
{ | ||
return $this->type . '<' . implode(', ', $this->genericTypes) . '>'; | ||
$genericTypes = []; | ||
|
||
foreach ($this->genericTypes as $index => $type) { | ||
$variance = $this->variances[$index]; | ||
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. This offset might not exist. |
||
if ($variance === self::VARIANCE_INVARIANT) { | ||
$genericTypes[] = (string) $type; | ||
} elseif ($variance === self::VARIANCE_BIVARIANT) { | ||
$genericTypes[] = '*'; | ||
} else { | ||
$genericTypes[] = sprintf('%s %s', $variance, $type); | ||
} | ||
} | ||
|
||
return $this->type . '<' . implode(', ', $genericTypes) . '>'; | ||
} | ||
|
||
} |
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]; | ||
} | ||
|
||
|
||
|