Skip to content

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

Merged
merged 1 commit into from
Dec 15, 2022
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
14 changes: 12 additions & 2 deletions src/Ast/Type/ArrayShapeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Choose a reason for hiding this comment

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

@jrmajor this cause error on Rector side, that's sealed true make all changes to add ... which actually not, see

https://github.com/rectorphp/rector-src/actions/runs/3706328433/jobs/6281403238

we forced pin to use 1.5.0 and the issue disappear

/cc @TomasVotruba @ondrejmirtes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, that's actually a bug in __toString(). I'll provide a fix later today.

{
$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) . '}';
}

}
23 changes: 13 additions & 10 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Choose a reason for hiding this comment

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

@jrmajor this cause error on Rector side, if this goes to false for default value, it may reduce the possible error.

that's sealed true make all changes to add ... which actually not, see

https://github.com/rectorphp/rector-src/actions/runs/3706328433/jobs/6281403238

we forced pin to use 1.5.0 and the issue disappear

/cc @TomasVotruba @ondrejmirtes


$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);
}


Expand Down
69 changes: 69 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,75 @@ public function provideParseData(): array
),
]),
],
[
'array{a: int, b: int, ...}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new IdentifierTypeNode('a'),
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
new IdentifierTypeNode('b'),
false,
new IdentifierTypeNode('int')
),
], false),
],
[
'array{int, string, ...}',
new ArrayShapeNode([
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('string')
),
], false),
],
[
'array{...}',
new ArrayShapeNode([], false),
],
[
'array{
* a: int,
* ...
*}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new IdentifierTypeNode('a'),
false,
new IdentifierTypeNode('int')
),
], false),
],
[
'array{
a: int,
...,
}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new IdentifierTypeNode('a'),
false,
new IdentifierTypeNode('int')
),
], false),
],
[
'array{int, ..., string}',
new ParserException(
'string',
Lexer::TOKEN_IDENTIFIER,
16,
Lexer::TOKEN_CLOSE_CURLY_BRACKET
),
],
[
'callable(): Foo',
new CallableTypeNode(
Expand Down