diff --git a/src/Barryvdh/Reflection/DocBlock/Tag/ParamTag.php b/src/Barryvdh/Reflection/DocBlock/Tag/ParamTag.php index e804408..d80e4d4 100644 --- a/src/Barryvdh/Reflection/DocBlock/Tag/ParamTag.php +++ b/src/Barryvdh/Reflection/DocBlock/Tag/ParamTag.php @@ -54,16 +54,15 @@ class ParamTag extends ReturnTag for($pos = 0, $stacks = []; $pos < strlen($rest); $pos++) { $char = $rest[$pos]; - if($char === '<') { + if(in_array($char, ['<', '(', '[', '{'])) { array_unshift($stacks, $char); } - if($char === '(') { - array_unshift($stacks, $char); - } - if($char === '>' && isset($stacks[0]) && $stacks[0] === '<') { - array_shift($stacks); - } - if($char === ')' && isset($stacks[0]) && $stacks[0] === '(') { + if( + ($char === '>' && isset($stacks[0]) && $stacks[0] === '<') + || ($char === ')' && isset($stacks[0]) && $stacks[0] === '(') + || ($char === ']' && isset($stacks[0]) && $stacks[0] === '[') + || ($char === '}' && isset($stacks[0]) && $stacks[0] === '{') + ) { array_shift($stacks); } diff --git a/src/Barryvdh/Reflection/DocBlock/Type/Collection.php b/src/Barryvdh/Reflection/DocBlock/Type/Collection.php index 5a81347..57d533e 100644 --- a/src/Barryvdh/Reflection/DocBlock/Type/Collection.php +++ b/src/Barryvdh/Reflection/DocBlock/Type/Collection.php @@ -161,9 +161,9 @@ class Collection extends \ArrayObject $type_parts[] = $curr_type; $curr_type = ''; } else { - if ($char === '<' || $char === '(') { + if (in_array($char, ['<', '(', '[', '{'])) { $nest_level++; - } else if ($char === '>' || $char === ')') { + } else if (in_array($char, ['>', ')', ']', '}'])) { $nest_level--; } @@ -201,7 +201,8 @@ class Collection extends \ArrayObject return ''; } - if (preg_match('/^[\w-]+<.*>$/', $type)) { + // Check for generics values and array shapes + if (preg_match('/^[\w-]+(<.+>|\[.+\]|{.+})$/', $type)) { return $type; } @@ -214,6 +215,11 @@ class Collection extends \ArrayObject return $type; } + // Literal strings + if ($type[0] === '"' || $type[0] === "'") { + return $type; + } + if ($this->isTypeAnArray($type)) { return $this->expand(substr($type, 0, -2)) . self::OPERATOR_ARRAY; } diff --git a/tests/Barryvdh/Reflection/DocBlock/Tag/ParamTagTest.php b/tests/Barryvdh/Reflection/DocBlock/Tag/ParamTagTest.php index 0a735cc..5c6c6f2 100644 --- a/tests/Barryvdh/Reflection/DocBlock/Tag/ParamTagTest.php +++ b/tests/Barryvdh/Reflection/DocBlock/Tag/ParamTagTest.php @@ -183,6 +183,24 @@ class ParamTagTest extends TestCase '$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', + '' + ) ); } } diff --git a/tests/Barryvdh/Reflection/DocBlock/Tag/ReturnTagTest.php b/tests/Barryvdh/Reflection/DocBlock/Tag/ReturnTagTest.php index a86b65b..847806b 100644 --- a/tests/Barryvdh/Reflection/DocBlock/Tag/ReturnTagTest.php +++ b/tests/Barryvdh/Reflection/DocBlock/Tag/ReturnTagTest.php @@ -119,6 +119,13 @@ class ReturnTagTest extends TestCase 'array|string', array('array', 'string'), 'Types of Bobs' + ), + array( + 'return', + 'MyArray[\'key\'] Type of Bobs', + 'MyArray[\'key\']', + array('MyArray[\'key\']'), + 'Type of Bobs' ) ); } diff --git a/tests/Barryvdh/Reflection/DocBlock/Type/CollectionTest.php b/tests/Barryvdh/Reflection/DocBlock/Type/CollectionTest.php index 6d8a508..ca5712c 100644 --- a/tests/Barryvdh/Reflection/DocBlock/Type/CollectionTest.php +++ b/tests/Barryvdh/Reflection/DocBlock/Type/CollectionTest.php @@ -239,6 +239,18 @@ class CollectionTest extends TestCase 'array>|array|string', array('array>', 'array', '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( 'LinkDescriptor::setLink()', array($namespace.'LinkDescriptor::setLink()') @@ -290,6 +302,10 @@ class CollectionTest extends TestCase array( 'callable(int, string): int', array('callable(int, string): int') + ), + array( + "'text'", + array("'text'") ) ); }