Add support for strict arrays and list shapes

This commit is contained in:
Daniil Gentili 2022-11-05 13:29:17 +01:00
parent 066f9d07ee
commit 7978df2946
2 changed files with 57 additions and 2 deletions

View File

@ -5,6 +5,7 @@ namespace PHPStan\PhpDocParser\Parser;
use LogicException;
use PHPStan\PhpDocParser\Ast;
use PHPStan\PhpDocParser\Lexer\Lexer;
use function in_array;
use function strpos;
use function trim;
@ -123,7 +124,7 @@ class TypeParser
} 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()) {
} elseif (in_array($type->name, ['array', 'list', 'strict-array', 'strict-list'], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
$type = $this->parseArrayShape($tokens, $type);
if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
@ -409,7 +410,7 @@ class TypeParser
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()) {
} elseif (in_array($type->name, ['array', 'list', 'strict-array', 'strict-list'], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
$type = $this->parseArrayShape($tokens, $type);
}
}

View File

@ -592,6 +592,60 @@ class TypeParserTest extends TestCase
),
]),
],
[
'strict-array{
a: int,
b: string
}',
new ArrayShapeNode([
new ArrayShapeItemNode(
new IdentifierTypeNode('a'),
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
new IdentifierTypeNode('b'),
false,
new IdentifierTypeNode('string')
),
]),
],
[
'strict-list{
int,
string
}',
new ArrayShapeNode([
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('string')
),
]),
],
[
'list{
int,
string
}',
new ArrayShapeNode([
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('string')
),
]),
],
[
'callable(): Foo',
new CallableTypeNode(