Skip to content

Commit

Permalink
Support for non-empty-array and non-empty-list array shape kind
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 13, 2024
1 parent 6ca22b1 commit 82a311f
Show file tree
Hide file tree
Showing 3 changed files with 98 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 @@ -180,7 +180,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 @@ -690,7 +696,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
82 changes: 82 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,88 @@ public function provideParseData(): array
ArrayShapeNode::KIND_LIST
),
],
[
'non-empty-array{
int,
string
}',
new ArrayShapeNode(
[
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('string')
),
],
true,
ArrayShapeNode::KIND_NON_EMPTY_ARRAY
),
],
[
'callable(): non-empty-array{int, string}',
new CallableTypeNode(new IdentifierTypeNode('callable'), [], new ArrayShapeNode(
[
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('string')
),
],
true,
ArrayShapeNode::KIND_NON_EMPTY_ARRAY
)),
],
[
'callable(): non-empty-list{int, string}',
new CallableTypeNode(new IdentifierTypeNode('callable'), [], new ArrayShapeNode(
[
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('string')
),
],
true,
ArrayShapeNode::KIND_NON_EMPTY_LIST
)),
],
[
'non-empty-list{
int,
string
}',
new ArrayShapeNode(
[
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('int')
),
new ArrayShapeItemNode(
null,
false,
new IdentifierTypeNode('string')
),
],
true,
ArrayShapeNode::KIND_NON_EMPTY_LIST
),
],
[
'array{...<string>}',
new ArrayShapeNode(
Expand Down

0 comments on commit 82a311f

Please sign in to comment.