Array shapes - support single-quoted and double-quoted strings

This commit is contained in:
Ondrej Mirtes 2020-04-13 18:11:25 +02:00
parent 9285b879a8
commit 97a0114d7f
No known key found for this signature in database
GPG Key ID: 8E730BA25823D8B5
3 changed files with 48 additions and 15 deletions

View File

@ -3,11 +3,12 @@
namespace PHPStan\PhpDocParser\Ast\Type;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
class ArrayShapeItemNode implements TypeNode
{
/** @var ConstExprIntegerNode|IdentifierTypeNode|null */
/** @var ConstExprIntegerNode|ConstExprStringNode|IdentifierTypeNode|null */
public $keyName;
/** @var bool */
@ -17,7 +18,7 @@ class ArrayShapeItemNode implements TypeNode
public $valueType;
/**
* @param ConstExprIntegerNode|IdentifierTypeNode|null $keyName
* @param ConstExprIntegerNode|ConstExprStringNode|IdentifierTypeNode|null $keyName
*/
public function __construct($keyName, bool $optional, TypeNode $valueType)
{

View File

@ -280,7 +280,7 @@ class TypeParser
}
/**
* @return Ast\ConstExpr\ConstExprIntegerNode|Ast\Type\IdentifierTypeNode
* @return Ast\ConstExpr\ConstExprIntegerNode|Ast\ConstExpr\ConstExprStringNode|Ast\Type\IdentifierTypeNode
*/
private function parseArrayShapeKey(TokenIterator $tokens)
{
@ -288,6 +288,14 @@ class TypeParser
$key = new Ast\ConstExpr\ConstExprIntegerNode($tokens->currentTokenValue());
$tokens->next();
} elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_SINGLE_QUOTED_STRING)) {
$key = new Ast\ConstExpr\ConstExprStringNode(trim($tokens->currentTokenValue(), "'"));
$tokens->next();
} elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_DOUBLE_QUOTED_STRING)) {
$key = new Ast\ConstExpr\ConstExprStringNode(trim($tokens->currentTokenValue(), '"'));
$tokens->next();
} else {
$key = new Ast\Type\IdentifierTypeNode($tokens->currentTokenValue());
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);

View File

@ -3,6 +3,7 @@
namespace PHPStan\PhpDocParser\Parser;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
@ -51,6 +52,7 @@ class TypeParserTest extends \PHPUnit\Framework\TestCase
$typeNode = $this->typeParser->parse($tokens);
$this->assertSame((string) $expectedResult, (string) $typeNode);
$this->assertInstanceOf(get_class($expectedResult), $typeNode);
$this->assertEquals($expectedResult, $typeNode);
$this->assertSame($nextTokenType, $tokens->currentTokenType());
}
@ -449,21 +451,43 @@ class TypeParserTest extends \PHPUnit\Framework\TestCase
],
[
'array{"a": int}',
new \PHPStan\PhpDocParser\Parser\ParserException(
'"a"',
Lexer::TOKEN_DOUBLE_QUOTED_STRING,
6,
Lexer::TOKEN_IDENTIFIER
),
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('a'),
false,
new IdentifierTypeNode('int')
),
]),
],
[
'array{\'a\': int}',
new \PHPStan\PhpDocParser\Parser\ParserException(
'\'a\'',
Lexer::TOKEN_SINGLE_QUOTED_STRING,
6,
Lexer::TOKEN_IDENTIFIER
),
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('a'),
false,
new IdentifierTypeNode('int')
),
]),
],
[
'array{\'$ref\': int}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('$ref'),
false,
new IdentifierTypeNode('int')
),
]),
],
[
'array{"$ref": int}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('$ref'),
false,
new IdentifierTypeNode('int')
),
]),
],
[
'array{