-
Notifications
You must be signed in to change notification settings - Fork 64
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
Changes from all commits
e8fa232
0d2d902
db466fd
eeb4a02
c188001
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
|
||
} |
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) . '}'; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem with Alternatively, if we don't allow spaces between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's not a problem of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. without There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So what do you think of not allowing a whitespace between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that should be fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} | ||
|
||
|
@@ -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); | ||
|
@@ -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); | ||
} | ||
} | ||
|
||
|
@@ -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) | ||
arnaud-lb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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; | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.