Skip to content

Commit b6ff9f9

Browse files
authored
Fixed parsing of string literals and array shapes (#27)
1 parent ffb733e commit b6ff9f9

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

src/Barryvdh/Reflection/DocBlock/Tag/ParamTag.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,15 @@ public function setContent($content)
5454
for($pos = 0, $stacks = []; $pos < strlen($rest); $pos++) {
5555
$char = $rest[$pos];
5656

57-
if($char === '<') {
57+
if(in_array($char, ['<', '(', '[', '{'])) {
5858
array_unshift($stacks, $char);
5959
}
60-
if($char === '(') {
61-
array_unshift($stacks, $char);
62-
}
63-
if($char === '>' && isset($stacks[0]) && $stacks[0] === '<') {
64-
array_shift($stacks);
65-
}
66-
if($char === ')' && isset($stacks[0]) && $stacks[0] === '(') {
60+
if(
61+
($char === '>' && isset($stacks[0]) && $stacks[0] === '<')
62+
|| ($char === ')' && isset($stacks[0]) && $stacks[0] === '(')
63+
|| ($char === ']' && isset($stacks[0]) && $stacks[0] === '[')
64+
|| ($char === '}' && isset($stacks[0]) && $stacks[0] === '{')
65+
) {
6766
array_shift($stacks);
6867
}
6968

src/Barryvdh/Reflection/DocBlock/Type/Collection.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ protected function explode($type)
161161
$type_parts[] = $curr_type;
162162
$curr_type = '';
163163
} else {
164-
if ($char === '<' || $char === '(') {
164+
if (in_array($char, ['<', '(', '[', '{'])) {
165165
$nest_level++;
166-
} else if ($char === '>' || $char === ')') {
166+
} else if (in_array($char, ['>', ')', ']', '}'])) {
167167
$nest_level--;
168168
}
169169

@@ -201,7 +201,8 @@ protected function expand($type)
201201
return '';
202202
}
203203

204-
if (preg_match('/^[\w-]+<.*>$/', $type)) {
204+
// Check for generics values and array shapes
205+
if (preg_match('/^[\w-]+(<.+>|\[.+\]|{.+})$/', $type)) {
205206
return $type;
206207
}
207208

@@ -214,6 +215,11 @@ protected function expand($type)
214215
return $type;
215216
}
216217

218+
// Literal strings
219+
if ($type[0] === '"' || $type[0] === "'") {
220+
return $type;
221+
}
222+
217223
if ($this->isTypeAnArray($type)) {
218224
return $this->expand(substr($type, 0, -2)) . self::OPERATOR_ARRAY;
219225
}

tests/Barryvdh/Reflection/DocBlock/Tag/ParamTagTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,24 @@ public function provideDataForConstructor()
183183
'$callback',
184184
''
185185
),
186+
187+
// array shapes
188+
array(
189+
'param',
190+
'array{foo: string, bar: int} $array',
191+
'array{foo: string, bar: int}',
192+
array('array{foo: string, bar: int}'),
193+
'$array',
194+
''
195+
),
196+
array(
197+
'param',
198+
'MyArray[\'key\'] $value',
199+
'MyArray[\'key\']',
200+
array('MyArray[\'key\']'),
201+
'$value',
202+
''
203+
)
186204
);
187205
}
188206
}

tests/Barryvdh/Reflection/DocBlock/Tag/ReturnTagTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ public function provideDataForConstructor()
119119
'array<int, string|bool>|string',
120120
array('array<int, string|bool>', 'string'),
121121
'Types of Bobs'
122+
),
123+
array(
124+
'return',
125+
'MyArray[\'key\'] Type of Bobs',
126+
'MyArray[\'key\']',
127+
array('MyArray[\'key\']'),
128+
'Type of Bobs'
122129
)
123130
);
124131
}

tests/Barryvdh/Reflection/DocBlock/Type/CollectionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,18 @@ public function provideTypesToExpand($method, $namespace = '\My\Space\\')
239239
'array<int, string|array<int, bool>>|array<int, float>|string',
240240
array('array<int, string|array<int, bool>>', 'array<int, float>', 'string')
241241
),
242+
array(
243+
'array{ 0: string, 1: string|int }',
244+
array('array{ 0: string, 1: string|int }')
245+
),
246+
array(
247+
"array{ 'key': string, 'value': string|int }",
248+
array("array{ 'key': string, 'value': string|int }")
249+
),
250+
array(
251+
"MyArray['bar']",
252+
array("MyArray['bar']")
253+
),
242254
array(
243255
'LinkDescriptor::setLink()',
244256
array($namespace.'LinkDescriptor::setLink()')
@@ -290,6 +302,10 @@ public function provideTypesToExpand($method, $namespace = '\My\Space\\')
290302
array(
291303
'callable(int, string): int',
292304
array('callable(int, string): int')
305+
),
306+
array(
307+
"'text'",
308+
array("'text'")
293309
)
294310
);
295311
}

0 commit comments

Comments
 (0)