Fixed parsing of string literals and array shapes (#27)

This commit is contained in:
Nereo Berardozzi
2025-01-18 20:26:32 +01:00
committed by GitHub
parent ffb733ef78
commit b6ff9f9360
5 changed files with 57 additions and 11 deletions
@@ -54,16 +54,15 @@ class ParamTag extends ReturnTag
for($pos = 0, $stacks = []; $pos < strlen($rest); $pos++) { for($pos = 0, $stacks = []; $pos < strlen($rest); $pos++) {
$char = $rest[$pos]; $char = $rest[$pos];
if($char === '<') { if(in_array($char, ['<', '(', '[', '{'])) {
array_unshift($stacks, $char); array_unshift($stacks, $char);
} }
if($char === '(') { if(
array_unshift($stacks, $char); ($char === '>' && isset($stacks[0]) && $stacks[0] === '<')
} || ($char === ')' && isset($stacks[0]) && $stacks[0] === '(')
if($char === '>' && isset($stacks[0]) && $stacks[0] === '<') { || ($char === ']' && isset($stacks[0]) && $stacks[0] === '[')
array_shift($stacks); || ($char === '}' && isset($stacks[0]) && $stacks[0] === '{')
} ) {
if($char === ')' && isset($stacks[0]) && $stacks[0] === '(') {
array_shift($stacks); array_shift($stacks);
} }
@@ -161,9 +161,9 @@ class Collection extends \ArrayObject
$type_parts[] = $curr_type; $type_parts[] = $curr_type;
$curr_type = ''; $curr_type = '';
} else { } else {
if ($char === '<' || $char === '(') { if (in_array($char, ['<', '(', '[', '{'])) {
$nest_level++; $nest_level++;
} else if ($char === '>' || $char === ')') { } else if (in_array($char, ['>', ')', ']', '}'])) {
$nest_level--; $nest_level--;
} }
@@ -201,7 +201,8 @@ class Collection extends \ArrayObject
return ''; return '';
} }
if (preg_match('/^[\w-]+<.*>$/', $type)) { // Check for generics values and array shapes
if (preg_match('/^[\w-]+(<.+>|\[.+\]|{.+})$/', $type)) {
return $type; return $type;
} }
@@ -214,6 +215,11 @@ class Collection extends \ArrayObject
return $type; return $type;
} }
// Literal strings
if ($type[0] === '"' || $type[0] === "'") {
return $type;
}
if ($this->isTypeAnArray($type)) { if ($this->isTypeAnArray($type)) {
return $this->expand(substr($type, 0, -2)) . self::OPERATOR_ARRAY; return $this->expand(substr($type, 0, -2)) . self::OPERATOR_ARRAY;
} }
@@ -183,6 +183,24 @@ class ParamTagTest extends TestCase
'$callback', '$callback',
'' ''
), ),
// array shapes
array(
'param',
'array{foo: string, bar: int} $array',
'array{foo: string, bar: int}',
array('array{foo: string, bar: int}'),
'$array',
''
),
array(
'param',
'MyArray[\'key\'] $value',
'MyArray[\'key\']',
array('MyArray[\'key\']'),
'$value',
''
)
); );
} }
} }
@@ -119,6 +119,13 @@ class ReturnTagTest extends TestCase
'array<int, string|bool>|string', 'array<int, string|bool>|string',
array('array<int, string|bool>', 'string'), array('array<int, string|bool>', 'string'),
'Types of Bobs' 'Types of Bobs'
),
array(
'return',
'MyArray[\'key\'] Type of Bobs',
'MyArray[\'key\']',
array('MyArray[\'key\']'),
'Type of Bobs'
) )
); );
} }
@@ -239,6 +239,18 @@ class CollectionTest extends TestCase
'array<int, string|array<int, bool>>|array<int, float>|string', 'array<int, string|array<int, bool>>|array<int, float>|string',
array('array<int, string|array<int, bool>>', 'array<int, float>', 'string') array('array<int, string|array<int, bool>>', 'array<int, float>', 'string')
), ),
array(
'array{ 0: string, 1: string|int }',
array('array{ 0: string, 1: string|int }')
),
array(
"array{ 'key': string, 'value': string|int }",
array("array{ 'key': string, 'value': string|int }")
),
array(
"MyArray['bar']",
array("MyArray['bar']")
),
array( array(
'LinkDescriptor::setLink()', 'LinkDescriptor::setLink()',
array($namespace.'LinkDescriptor::setLink()') array($namespace.'LinkDescriptor::setLink()')
@@ -290,6 +302,10 @@ class CollectionTest extends TestCase
array( array(
'callable(int, string): int', 'callable(int, string): int',
array('callable(int, string): int') array('callable(int, string): int')
),
array(
"'text'",
array("'text'")
) )
); );
} }