Disallow whitespaces between 'array' and '{'

This commit is contained in:
Arnaud Le Blanc 2019-06-04 10:55:52 +02:00 committed by Ondřej Mirtes
parent c6ffc4b4f1
commit 1e2a8ece06
3 changed files with 15 additions and 3 deletions

View File

@ -64,6 +64,12 @@ class TokenIterator
}
public function isPrecededByHorizontalWhitespace(): bool
{
return ($this->tokens[$this->index - 1][Lexer::TYPE_OFFSET] ?? -1) === Lexer::TOKEN_HORIZONTAL_WS;
}
/**
* @param int $tokenType
* @throws \PHPStan\PhpDocParser\Parser\ParserException

View File

@ -54,7 +54,7 @@ class TypeParser
} elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
$type = $this->tryParseArray($tokens, $type);
} elseif ($type->name === 'array' && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET)) {
} elseif ($type->name === 'array' && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
$type = $this->parseArrayShape($tokens, $type);
}
}
@ -97,7 +97,7 @@ class TypeParser
if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) {
$type = $this->parseGeneric($tokens, $type);
} elseif ($type->name === 'array' && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET)) {
} elseif ($type->name === 'array' && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
$type = $this->parseArrayShape($tokens, $type);
}
@ -174,7 +174,7 @@ class TypeParser
if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) {
$type = $this->parseGeneric($tokens, $type);
} elseif ($type->name === 'array' && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET)) {
} elseif ($type->name === 'array' && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
$type = $this->parseArrayShape($tokens, $type);
}
}

View File

@ -268,6 +268,12 @@ class TypeParserTest extends \PHPUnit\Framework\TestCase
]
),
],
[
'array {\'a\': int}',
new IdentifierTypeNode('array'),
Lexer::TOKEN_OPEN_CURLY_BRACKET,
],
[
'array{\'a\': int}',
new ArrayShapeNode([