Skip to content

Commit

Permalink
Allow only identifiers and integers in keys
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-lb authored and ondrejmirtes committed Jun 7, 2019
1 parent 12324c4 commit 9b27b84
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
5 changes: 2 additions & 3 deletions src/Ast/Type/ArrayShapeItemNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
namespace PHPStan\PhpDocParser\Ast\Type;

use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;

class ArrayShapeItemNode implements TypeNode
{

/** @var ConstExprStringNode|ConstExprIntegerNode|IdentifierTypeNode|null */
/** @var ConstExprIntegerNode|IdentifierTypeNode|null */
public $keyName;

/** @var bool */
Expand All @@ -18,7 +17,7 @@ class ArrayShapeItemNode implements TypeNode
public $valueType;

/**
* @param ConstExprStringNode|ConstExprIntegerNode|IdentifierTypeNode|null $keyName
* @param ConstExprIntegerNode|IdentifierTypeNode|null $keyName
*/
public function __construct($keyName, bool $optional, TypeNode $valueType)
{
Expand Down
12 changes: 2 additions & 10 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,11 @@ private function parseArrayShapeItem(TokenIterator $tokens): Ast\Type\ArrayShape
}

/**
* @return Ast\ConstExpr\ConstExprStringNode|Ast\ConstExpr\ConstExprIntegerNode|Ast\Type\IdentifierTypeNode
* @return Ast\ConstExpr\ConstExprIntegerNode|Ast\Type\IdentifierTypeNode
*/
private function parseArrayShapeKey(TokenIterator $tokens)
{
if ($tokens->isCurrentTokenType(Lexer::TOKEN_SINGLE_QUOTED_STRING)) {
$key = new Ast\ConstExpr\ConstExprStringNode($tokens->currentTokenValue());
$tokens->next();

} elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_DOUBLE_QUOTED_STRING)) {
$key = new Ast\ConstExpr\ConstExprStringNode($tokens->currentTokenValue());
$tokens->next();

} elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_INTEGER)) {
if ($tokens->isCurrentTokenType(Lexer::TOKEN_INTEGER)) {
$key = new Ast\ConstExpr\ConstExprIntegerNode($tokens->currentTokenValue());
$tokens->next();

Expand Down
66 changes: 40 additions & 26 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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;
Expand Down Expand Up @@ -280,20 +279,20 @@ public function provideParseData(): array
],

[
'array{\'a\': int}',
'array{a: int}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('\'a\''),
new IdentifierTypeNode('a'),
false,
new IdentifierTypeNode('int')
),
]),
],
[
'array{\'a\': ?int}',
'array{a: ?int}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('\'a\''),
new IdentifierTypeNode('a'),
false,
new NullableTypeNode(
new IdentifierTypeNode('int')
Expand All @@ -302,10 +301,10 @@ public function provideParseData(): array
]),
],
[
'array{\'a\'?: ?int}',
'array{a?: ?int}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('\'a\''),
new IdentifierTypeNode('a'),
true,
new NullableTypeNode(
new IdentifierTypeNode('int')
Expand All @@ -314,22 +313,27 @@ public function provideParseData(): array
]),
],
[
'array{\'a\': int, \'b\': string}',
'array{0: int}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('\'a\''),
new ConstExprIntegerNode('0'),
false,
new IdentifierTypeNode('int')
),
]),
],
[
'array{0?: int}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('\'b\''),
false,
new IdentifierTypeNode('string')
new ConstExprIntegerNode('0'),
true,
new IdentifierTypeNode('int')
),
]),
],
[
'array{int, string, "a": string}',
'array{int, int}',
new ArrayShapeNode([
new ArrayShapeItemNode(
null,
Expand All @@ -339,25 +343,35 @@ public function provideParseData(): array
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('string')
new IdentifierTypeNode('int')
),
]),
],
[
'array{a: int, b: string}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new IdentifierTypeNode('a'),
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
new ConstExprStringNode('"a"'),
new IdentifierTypeNode('b'),
false,
new IdentifierTypeNode('string')
),
]),
],
[
'array{"a"?: int, \'b\': string, 0: int, 1?: DateTime, hello: string}',
'array{a?: int, b: string, 0: int, 1?: DateTime, hello: string}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('"a"'),
new IdentifierTypeNode('a'),
true,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
new ConstExprStringNode('\'b\''),
new IdentifierTypeNode('b'),
false,
new IdentifierTypeNode('string')
),
Expand All @@ -379,19 +393,19 @@ public function provideParseData(): array
]),
],
[
'array{\'a\': int, \'b\': array{\'c\': callable(): int}}',
'array{a: int, b: array{c: callable(): int}}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('\'a\''),
new IdentifierTypeNode('a'),
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
new ConstExprStringNode('\'b\''),
new IdentifierTypeNode('b'),
false,
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('\'c\''),
new IdentifierTypeNode('c'),
false,
new CallableTypeNode(
new IdentifierTypeNode('callable'),
Expand All @@ -404,11 +418,11 @@ public function provideParseData(): array
]),
],
[
'?array{\'a\': int}',
'?array{a: int}',
new NullableTypeNode(
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('\'a\''),
new IdentifierTypeNode('a'),
false,
new IdentifierTypeNode('int')
),
Expand Down Expand Up @@ -509,13 +523,13 @@ public function provideParseData(): array
),
],
[
'callable(): array{\'a\': int}',
'callable(): array{a: int}',
new CallableTypeNode(
new IdentifierTypeNode('callable'),
[],
new ArrayShapeNode([
new ArrayShapeItemNode(
new ConstExprStringNode('\'a\''),
new IdentifierTypeNode('a'),
false,
new IdentifierTypeNode('int')
),
Expand Down

0 comments on commit 9b27b84

Please sign in to comment.