Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.23.x' into 2.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 13, 2024
2 parents a131a15 + 82a311f commit c00d78f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Ast/Type/ArrayShapeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class ArrayShapeNode implements TypeNode

public const KIND_ARRAY = 'array';
public const KIND_LIST = 'list';
public const KIND_NON_EMPTY_ARRAY = 'non-empty-array';
public const KIND_NON_EMPTY_LIST = 'non-empty-list';

use NodeAttributes;

Expand Down
16 changes: 14 additions & 2 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,13 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode
} elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
$type = $this->tryParseArrayOrOffsetAccess($tokens, $type);

} elseif (in_array($type->name, ['array', 'list', 'object'], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
} elseif (in_array($type->name, [
Ast\Type\ArrayShapeNode::KIND_ARRAY,
Ast\Type\ArrayShapeNode::KIND_LIST,
Ast\Type\ArrayShapeNode::KIND_NON_EMPTY_ARRAY,
Ast\Type\ArrayShapeNode::KIND_NON_EMPTY_LIST,
'object',
], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
if ($type->name === 'object') {
$type = $this->parseObjectShape($tokens);
} else {
Expand Down Expand Up @@ -665,7 +671,13 @@ private function parseCallableReturnType(TokenIterator $tokens): Ast\Type\TypeNo
$startIndex,
));

} elseif (in_array($type->name, ['array', 'list', 'object'], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
} elseif (in_array($type->name, [
Ast\Type\ArrayShapeNode::KIND_ARRAY,
Ast\Type\ArrayShapeNode::KIND_LIST,
Ast\Type\ArrayShapeNode::KIND_NON_EMPTY_ARRAY,
Ast\Type\ArrayShapeNode::KIND_NON_EMPTY_LIST,
'object',
], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
if ($type->name === 'object') {
$type = $this->parseObjectShape($tokens);
} else {
Expand Down
78 changes: 78 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,84 @@ public function provideParseData(): array
ArrayShapeNode::KIND_LIST,
),
],
[
'non-empty-array{
int,
string
}',
ArrayShapeNode::createSealed(
[
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('int'),
),
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('string'),
),
],
ArrayShapeNode::KIND_NON_EMPTY_ARRAY,
),
],
[
'callable(): non-empty-array{int, string}',
new CallableTypeNode(new IdentifierTypeNode('callable'), [], ArrayShapeNode::createSealed(
[
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('int'),
),
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('string'),
),
],
ArrayShapeNode::KIND_NON_EMPTY_ARRAY,
), []),
],
[
'callable(): non-empty-list{int, string}',
new CallableTypeNode(new IdentifierTypeNode('callable'), [], ArrayShapeNode::createSealed(
[
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('int'),
),
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('string'),
),
],
ArrayShapeNode::KIND_NON_EMPTY_LIST,
), []),
],
[
'non-empty-list{
int,
string
}',
ArrayShapeNode::createSealed(
[
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('int'),
),
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('string'),
),
],
ArrayShapeNode::KIND_NON_EMPTY_LIST,
),
],
[
'array{...<string>}',
ArrayShapeNode::createUnsealed(
Expand Down

0 comments on commit c00d78f

Please sign in to comment.