Skip to content

Commit 311e561

Browse files
committed
keep covariant/contravariant of as a generic variance in class-string-map
1 parent 997af32 commit 311e561

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎doc/grammars/types.pp3‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,13 @@ ClassStringMapInReturnType
302302
: AngleBracketOpen() Trivia() ClassStringMapArguments()
303303
;
304304

305+
/**
306+
* A template named "covariant" or "contravariant" is not one: those words stay
307+
* the variance of an ordinary generic argument, so class-string-map<covariant
308+
* of[], int> is read as before.
309+
*/
305310
ClassStringMapArguments
306-
: Identifier() ( <T_KEYWORD_OF> | <T_KEYWORD_AS> ) Type() Trivia()
311+
: !Variance() Identifier() ( <T_KEYWORD_OF> | <T_KEYWORD_AS> ) Type() Trivia()
307312
<T_COMMA> Trivia() Type() Trivia() ( <T_COMMA> Trivia() )? <T_CLOSE_ANGLE_BRACKET>
308313
;
309314

‎src/Parser/TypeParser.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,12 @@ private function tryParseClassStringMap(TokenIterator $tokens, Ast\Type\Identifi
507507
$templateStartLine = $tokens->currentTokenLine();
508508
$templateStartIndex = $tokens->currentTokenIndex();
509509
$name = $tokens->currentTokenValue();
510+
// "covariant of" and "contravariant of" stay a variance on an ordinary
511+
// generic argument, as in class-string-map<covariant of[], int>
510512
if (
511513
$name === ''
514+
|| $name === 'covariant'
515+
|| $name === 'contravariant'
512516
|| !$tokens->tryConsumeTokenType(Lexer::TOKEN_IDENTIFIER)
513517
|| !($tokens->tryConsumeTokenValue('of') || $tokens->tryConsumeTokenValue('as'))
514518
) {

‎tests/PHPStan/Parser/TypeParserTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,20 @@ public function provideParseData(): array
16721672
new IdentifierTypeNode('T'),
16731673
),
16741674
],
1675+
[
1676+
'class-string-map<covariant of[], int>',
1677+
new GenericTypeNode(
1678+
new IdentifierTypeNode('class-string-map'),
1679+
[
1680+
new ArrayTypeNode(new IdentifierTypeNode('of')),
1681+
new IdentifierTypeNode('int'),
1682+
],
1683+
[
1684+
GenericTypeNode::VARIANCE_COVARIANT,
1685+
GenericTypeNode::VARIANCE_INVARIANT,
1686+
],
1687+
),
1688+
],
16751689
[
16761690
'class-string-map<T, V>',
16771691
new GenericTypeNode(

0 commit comments

Comments
 (0)