Skip to content

Array shapes support #30

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

Merged
merged 5 commits into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/grammars/type.abnf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ CallableReturnType
Array
= 1*(TokenSquareBracketOpen TokenSquareBracketClose)

ArrayShape
= TokenCurlyBracketOpen ArrayShapeItem *(TokenComma ArrayShapeItem) TokenCurlyBracketClose

ArrayShapeItem
= (ConstantString / ConstantInt / TokenIdentifier) TokenNullable TokenColon Type
/ Type

; ---------------------------------------------------------------------------- ;
; ConstantExpr ;
Expand Down Expand Up @@ -139,6 +145,12 @@ TokenSquareBracketOpen
TokenSquareBracketClose
= "]" *ByteHorizontalWs

TokenCurlyBracketOpen
= "{" *ByteHorizontalWs

TokenCurlyBracketClose
= "}" *ByteHorizontalWs

TokenComma
= "," *ByteHorizontalWs

Expand Down
44 changes: 44 additions & 0 deletions src/Ast/Type/ArrayShapeItemNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Ast\Type;

use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;

class ArrayShapeItemNode implements TypeNode
{

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

/** @var bool */
public $optional;

/** @var TypeNode */
public $valueType;

/**
* @param ConstExprIntegerNode|IdentifierTypeNode|null $keyName
*/
public function __construct($keyName, bool $optional, TypeNode $valueType)
{
$this->keyName = $keyName;
$this->optional = $optional;
$this->valueType = $valueType;
}


public function __toString(): string
{
if ($this->keyName !== null) {
return sprintf(
'%s%s: %s',
(string) $this->keyName,
$this->optional ? '?' : '',
(string) $this->valueType
);
}

return (string) $this->valueType;
}

}
22 changes: 22 additions & 0 deletions src/Ast/Type/ArrayShapeNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Ast\Type;

class ArrayShapeNode implements TypeNode
{

/** @var ArrayShapeItemNode[] */
public $items;

public function __construct(array $items)
{
$this->items = $items;
}


public function __toString(): string
{
return 'array{' . implode(', ', $this->items) . '}';
}

}
6 changes: 6 additions & 0 deletions src/Lexer/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Lexer
public const TOKEN_CLOSE_ANGLE_BRACKET = 7;
public const TOKEN_OPEN_SQUARE_BRACKET = 8;
public const TOKEN_CLOSE_SQUARE_BRACKET = 9;
public const TOKEN_OPEN_CURLY_BRACKET = 30;
public const TOKEN_CLOSE_CURLY_BRACKET = 31;
public const TOKEN_COMMA = 10;
public const TOKEN_COLON = 29;
public const TOKEN_VARIADIC = 11;
Expand Down Expand Up @@ -50,6 +52,8 @@ class Lexer
self::TOKEN_CLOSE_ANGLE_BRACKET => '\'>\'',
self::TOKEN_OPEN_SQUARE_BRACKET => '\'[\'',
self::TOKEN_CLOSE_SQUARE_BRACKET => '\']\'',
self::TOKEN_OPEN_CURLY_BRACKET => '\'{\'',
self::TOKEN_CLOSE_CURLY_BRACKET => '\'}\'',
self::TOKEN_COMMA => '\',\'',
self::TOKEN_COLON => '\':\'',
self::TOKEN_VARIADIC => '\'...\'',
Expand Down Expand Up @@ -123,6 +127,8 @@ private function initialize(): void
self::TOKEN_CLOSE_ANGLE_BRACKET => '>',
self::TOKEN_OPEN_SQUARE_BRACKET => '\\[',
self::TOKEN_CLOSE_SQUARE_BRACKET => '\\]',
self::TOKEN_OPEN_CURLY_BRACKET => '\\{',
self::TOKEN_CLOSE_CURLY_BRACKET => '\\}',

self::TOKEN_COMMA => ',',
self::TOKEN_VARIADIC => '\\.\\.\\.',
Expand Down
6 changes: 6 additions & 0 deletions src/Parser/TokenIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public function isCurrentTokenType(int $tokenType): bool
}


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
Expand Down
61 changes: 61 additions & 0 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode

} elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
$type = $this->tryParseArray($tokens, $type);

} elseif ($type->name === 'array' && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
$type = $this->parseArrayShape($tokens, $type);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be maybe tryParseArrayShape, so that /** @return array {curly description} */ is not suddenly invalid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with tryParse is that it silently ignores errors. An array shape specification will be silently ignored if it has a minor typo in it.

Alternatively, if we don't allow spaces between array and {, this would prevent the problem you mention. We could do that by adding an array{ token. What do you think ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with tryParse is that it silently ignores errors.

That's not a problem of tryParse but inherent problem of phpDoc which is designed to be human friendly and not computer friendly (same problem as markdown).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without tryParse it's a BC break.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what do you think of not allowing a whitespace between array and { ? This would prevent the BC break, while also avoiding silenced errors (which are not user friendly).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that should be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

Expand Down Expand Up @@ -93,6 +96,9 @@ private function parseNullable(TokenIterator $tokens): Ast\Type\TypeNode

if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) {
$type = $this->parseGeneric($tokens, $type);

} elseif ($type->name === 'array' && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
$type = $this->parseArrayShape($tokens, $type);
}

return new Ast\Type\NullableTypeNode($type);
Expand Down Expand Up @@ -167,6 +173,9 @@ private function parseCallableReturnType(TokenIterator $tokens): Ast\Type\TypeNo

if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) {
$type = $this->parseGeneric($tokens, $type);

} elseif ($type->name === 'array' && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
$type = $this->parseArrayShape($tokens, $type);
}
}

Expand Down Expand Up @@ -208,4 +217,56 @@ private function tryParseArray(TokenIterator $tokens, Ast\Type\TypeNode $type):
return $type;
}


private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type): Ast\Type\TypeNode
{
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET);
$items = [$this->parseArrayShapeItem($tokens)];

while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
$items[] = $this->parseArrayShapeItem($tokens);
}

$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET);

return new Ast\Type\ArrayShapeNode($items);
}


private function parseArrayShapeItem(TokenIterator $tokens): Ast\Type\ArrayShapeItemNode
{
try {
$tokens->pushSavePoint();
$key = $this->parseArrayShapeKey($tokens);
$optional = $tokens->tryConsumeTokenType(Lexer::TOKEN_NULLABLE);
$tokens->consumeTokenType(Lexer::TOKEN_COLON);
$value = $this->parse($tokens);
$tokens->dropSavePoint();

return new Ast\Type\ArrayShapeItemNode($key, $optional, $value);
} catch (\PHPStan\PhpDocParser\Parser\ParserException $e) {
$tokens->rollback();
$value = $this->parse($tokens);

return new Ast\Type\ArrayShapeItemNode(null, false, $value);
}
}

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

} else {
$key = new Ast\Type\IdentifierTypeNode($tokens->currentTokenValue());
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
}

return $key;
}

}
Loading