diff --git a/src/Barryvdh/Reflection/DocBlock/Tag/ReturnTag.php b/src/Barryvdh/Reflection/DocBlock/Tag/ReturnTag.php index 990dd1b..65b01e4 100644 --- a/src/Barryvdh/Reflection/DocBlock/Tag/ReturnTag.php +++ b/src/Barryvdh/Reflection/DocBlock/Tag/ReturnTag.php @@ -26,7 +26,7 @@ class ReturnTag extends Tag { /** @var string The raw type component. */ protected $type = ''; - + /** @var Collection The parsed type component. */ protected $types = null; @@ -49,7 +49,7 @@ class ReturnTag extends Tag { parent::setContent($content); - $parts = preg_split('/\s+/Su', $this->description, 2); + $parts = preg_split('/(?description, 2); // any output is considered a type $this->type = $parts[0]; @@ -112,7 +112,7 @@ class ReturnTag extends Tag /** * Returns the type collection. - * + * * @return void */ protected function getTypesCollection() diff --git a/src/Barryvdh/Reflection/DocBlock/Type/Collection.php b/src/Barryvdh/Reflection/DocBlock/Type/Collection.php index 7306399..82d64fa 100644 --- a/src/Barryvdh/Reflection/DocBlock/Type/Collection.php +++ b/src/Barryvdh/Reflection/DocBlock/Type/Collection.php @@ -106,7 +106,7 @@ class Collection extends \ArrayObject } // separate the type by the OR operator - $type_parts = explode(self::OPERATOR_OR, $type); + $type_parts = $this->explode($type); foreach ($type_parts as $part) { $expanded_type = $this->expand($part); if ($expanded_type) { @@ -114,10 +114,10 @@ class Collection extends \ArrayObject } } } - + /** * Returns a string representation of the collection. - * + * * @return string The resolved types across the collection, separated with * {@link self::OPERATOR_OR}. */ @@ -126,6 +126,40 @@ class Collection extends \ArrayObject return implode(self::OPERATOR_OR, $this->getArrayCopy()); } + /** + * Analyzes the given union of types and returns separated by OR operator + * single types. + * + * @param string $type The type or union of types + * + * @return array + */ + protected function explode($type) + { + $type_parts = []; + $curr_type = ''; + $nest_level = 0; + + foreach (str_split($type) as $char) { + if ($char === self::OPERATOR_OR && $nest_level === 0) { + $type_parts[] = $curr_type; + $curr_type = ''; + } else { + if ($char === '<') { + $nest_level++; + } else if ($char === '>') { + $nest_level--; + } + + $curr_type .= $char; + } + } + + $type_parts[] = $curr_type; + + return $type_parts; + } + /** * Analyzes the given type and returns the FQCN variant. * diff --git a/tests/Barryvdh/Reflection/DocBlock/Tag/ReturnTagTest.php b/tests/Barryvdh/Reflection/DocBlock/Tag/ReturnTagTest.php index 086919e..92fb58d 100644 --- a/tests/Barryvdh/Reflection/DocBlock/Tag/ReturnTagTest.php +++ b/tests/Barryvdh/Reflection/DocBlock/Tag/ReturnTagTest.php @@ -1,7 +1,7 @@ @@ -96,6 +96,27 @@ class ReturnTagTest extends \PHPUnit_Framework_TestCase 'int', array('int'), "Number of Bobs" + ), + array( + 'return', + 'array Types of Bobs', + 'array', + array('array'), + 'Types of Bobs' + ), + array( + 'return', + 'array|string Types of Bobs', + 'array|string', + array('array', 'string'), + 'Types of Bobs' + ), + array( + 'return', + 'array|string Types of Bobs', + 'array|string', + array('array', 'string'), + 'Types of Bobs' ) ); } diff --git a/tests/Barryvdh/Reflection/DocBlock/Type/CollectionTest.php b/tests/Barryvdh/Reflection/DocBlock/Type/CollectionTest.php index 73384fe..f4df904 100644 --- a/tests/Barryvdh/Reflection/DocBlock/Type/CollectionTest.php +++ b/tests/Barryvdh/Reflection/DocBlock/Type/CollectionTest.php @@ -1,7 +1,7 @@ @@ -16,7 +16,7 @@ use Barryvdh\Reflection\DocBlock\Context; /** * Test class for \Barryvdh\Reflection\DocBlock\Type\Collection - * + * * @covers Barryvdh\Reflection\DocBlock\Type\Collection * * @author Mike van Riel @@ -29,7 +29,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase /** * @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct * @covers Barryvdh\Reflection\DocBlock\Type\Collection::getContext - * + * * @return void */ public function testConstruct() @@ -42,7 +42,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase /** * @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct - * + * * @return void */ public function testConstructWithTypes() @@ -53,7 +53,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase /** * @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct - * + * * @return void */ public function testConstructWithNamespace() @@ -70,7 +70,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase /** * @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct - * + * * @return void */ public function testConstructWithNamespaceAliases() @@ -89,7 +89,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase * * @dataProvider provideTypesToExpand * @covers Barryvdh\Reflection\DocBlock\Type\Collection::add - * + * * @return void */ public function testAdd($fixture, $expected) @@ -109,7 +109,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase * * @dataProvider provideTypesToExpandWithoutNamespace * @covers Barryvdh\Reflection\DocBlock\Type\Collection::add - * + * * @return void */ public function testAddWithoutNamespace($fixture, $expected) @@ -146,7 +146,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase /** * @covers Barryvdh\Reflection\DocBlock\Type\Collection::add * @expectedException InvalidArgumentException - * + * * @return void */ public function testAddWithInvalidArgument() @@ -197,6 +197,22 @@ class CollectionTest extends \PHPUnit_Framework_TestCase 'DocBlock[]|int[]', array($namespace.'DocBlock[]', 'int[]') ), + array( + 'array', + array('array') + ), + array( + 'array|string', + array('array', 'string') + ), + array( + 'array|string', + array('array', 'string') + ), + array( + 'array>|array|string', + array('array>', 'array', 'string') + ), array( 'LinkDescriptor::setLink()', array($namespace.'LinkDescriptor::setLink()')