Skip to content

Add support for list shapes #175

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 2 commits into from
Jan 29, 2023
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 @@ -8,6 +8,9 @@
class ArrayShapeNode implements TypeNode
{

public const KIND_ARRAY = 'array';
public const KIND_LIST = 'list';

use NodeAttributes;

/** @var ArrayShapeItemNode[] */
Expand All @@ -16,10 +19,17 @@ class ArrayShapeNode implements TypeNode
/** @var bool */
public $sealed;

public function __construct(array $items, bool $sealed = true)
/** @var self::KIND_* */
public $kind;

/**
* @param self::KIND_* $kind
*/
public function __construct(array $items, bool $sealed = true, string $kind = self::KIND_ARRAY)
{
$this->items = $items;
$this->sealed = $sealed;
$this->kind = $kind;
}


Expand All @@ -31,7 +41,7 @@ public function __toString(): string
$items[] = '...';
}

return 'array{' . implode(', ', $items) . '}';
return $this->kind . '{' . implode(', ', $items) . '}';
}

}
18 changes: 11 additions & 7 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use LogicException;
use PHPStan\PhpDocParser\Ast;
use PHPStan\PhpDocParser\Lexer\Lexer;
use function in_array;
use function strpos;
use function trim;

Expand Down Expand Up @@ -123,8 +124,8 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode
} elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
$type = $this->tryParseArrayOrOffsetAccess($tokens, $type);

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

if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
$type = $this->tryParseArrayOrOffsetAccess($tokens, $type);
Expand Down Expand Up @@ -439,8 +440,8 @@ 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);
} elseif (in_array($type->name, ['array', 'list'], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
$type = $this->parseArrayShape($tokens, $type, $type->name);
}
}

Expand Down Expand Up @@ -499,8 +500,11 @@ private function tryParseArrayOrOffsetAccess(TokenIterator $tokens, Ast\Type\Typ
}


/** @phpstan-impure */
private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type): Ast\Type\ArrayShapeNode
/**
* @phpstan-impure
* @param Ast\Type\ArrayShapeNode::KIND_* $kind
*/
private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type, string $kind): Ast\Type\ArrayShapeNode
{
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET);

Expand Down Expand Up @@ -528,7 +532,7 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type)
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET);

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


Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,28 @@ public function provideParseData(): array
Lexer::TOKEN_CLOSE_CURLY_BRACKET
),
],
[
'list{
int,
string
}',
new ArrayShapeNode(
[
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('string')
),
],
true,
ArrayShapeNode::KIND_LIST
),
],
[
'callable(): Foo',
new CallableTypeNode(
Expand Down