-
Notifications
You must be signed in to change notification settings - Fork 66
Support unsealed array shapes #169
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
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 |
---|---|---|
|
@@ -13,15 +13,25 @@ class ArrayShapeNode implements TypeNode | |
/** @var ArrayShapeItemNode[] */ | ||
public $items; | ||
|
||
public function __construct(array $items) | ||
/** @var bool */ | ||
public $sealed; | ||
|
||
public function __construct(array $items, bool $sealed = true) | ||
|
||
{ | ||
$this->items = $items; | ||
$this->sealed = $sealed; | ||
} | ||
|
||
|
||
public function __toString(): string | ||
{ | ||
return 'array{' . implode(', ', $this->items) . '}'; | ||
$items = $this->items; | ||
|
||
if ($this->sealed) { | ||
$items[] = '...'; | ||
} | ||
|
||
return 'array{' . implode(', ', $items) . '}'; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -503,29 +503,32 @@ private function tryParseArrayOrOffsetAccess(TokenIterator $tokens, Ast\Type\Typ | |
private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type): Ast\Type\ArrayShapeNode | ||
{ | ||
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET); | ||
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET)) { | ||
return new Ast\Type\ArrayShapeNode([]); | ||
} | ||
|
||
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); | ||
$items = [$this->parseArrayShapeItem($tokens)]; | ||
$items = []; | ||
$sealed = true; | ||
|
||
|
||
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); | ||
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) { | ||
do { | ||
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); | ||
|
||
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET)) { | ||
// trailing comma case | ||
return new Ast\Type\ArrayShapeNode($items); | ||
} | ||
|
||
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_VARIADIC)) { | ||
$sealed = false; | ||
$tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA); | ||
break; | ||
} | ||
|
||
$items[] = $this->parseArrayShapeItem($tokens); | ||
|
||
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); | ||
} | ||
}while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)); | ||
|
||
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); | ||
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET); | ||
|
||
return new Ast\Type\ArrayShapeNode($items); | ||
return new Ast\Type\ArrayShapeNode($items, $sealed); | ||
} | ||
|
||
|
||
|