From 9da4a6506518f22f571059cc5ec0f4e71ab41394 Mon Sep 17 00:00:00 2001 From: Vasil Rangelov Date: Sun, 18 Nov 2012 17:41:35 +0200 Subject: [PATCH] Reverted the move of explode() into Return, and moved the implode() into Collection's new __toString() method instead; Also had to made type resolution lazy, as a pleasant side effect. This reverts commit db20ae39fb515335c44f9cdd42db19f62c399b72. --- .../Reflection/DocBlock/Tag/ReturnTag.php | 34 ++++++++++++----- .../Reflection/DocBlock/Type/Collection.php | 25 ++++++++++-- .../DocBlock/Type/CollectionTest.php | 38 +++++++++---------- 3 files changed, 64 insertions(+), 33 deletions(-) diff --git a/src/phpDocumentor/Reflection/DocBlock/Tag/ReturnTag.php b/src/phpDocumentor/Reflection/DocBlock/Tag/ReturnTag.php index 985d890..236d4ee 100644 --- a/src/phpDocumentor/Reflection/DocBlock/Tag/ReturnTag.php +++ b/src/phpDocumentor/Reflection/DocBlock/Tag/ReturnTag.php @@ -14,6 +14,7 @@ namespace phpDocumentor\Reflection\DocBlock\Tag; use phpDocumentor\Reflection\DocBlock; use phpDocumentor\Reflection\DocBlock\Tag; +use phpDocumentor\Reflection\DocBlock\Type\Collection; /** * Reflection class for a @return tag in a Docblock. @@ -24,8 +25,11 @@ use phpDocumentor\Reflection\DocBlock\Tag; */ class ReturnTag extends Tag { - /** @var string */ + /** @var string The raw type component. */ protected $type = ''; + + /** @var Collection The parsed type component. */ + protected $types = null; /** * Parses a tag and populates the member variables. @@ -52,13 +56,8 @@ class ReturnTag extends Tag */ public function getTypes() { - $types = new \phpDocumentor\Reflection\DocBlock\Type\Collection( - explode('|', $this->type), - $this->docblock ? $this->docblock->getNamespace() : null, - $this->docblock ? $this->docblock->getNamespaceAliases() : array() - ); - - return $types->getArrayCopy(); + $this->refreshTypes(); + return $this->types->getArrayCopy(); } /** @@ -68,6 +67,23 @@ class ReturnTag extends Tag */ public function getType() { - return implode('|', $this->getTypes()); + $this->refreshTypes(); + return (string) $this->types; + } + + /** + * Parses the type, if needed. + * + * @return void + */ + protected function refreshTypes() + { + if (null === $this->types) { + $this->types = new Collection( + array($this->type), + $this->docblock ? $this->docblock->getNamespace() : null, + $this->docblock ? $this->docblock->getNamespaceAliases() : array() + ); + } } } diff --git a/src/phpDocumentor/Reflection/DocBlock/Type/Collection.php b/src/phpDocumentor/Reflection/DocBlock/Type/Collection.php index bcb49a6..0c94545 100644 --- a/src/phpDocumentor/Reflection/DocBlock/Type/Collection.php +++ b/src/phpDocumentor/Reflection/DocBlock/Type/Collection.php @@ -22,6 +22,9 @@ namespace phpDocumentor\Reflection\DocBlock\Type; */ class Collection extends \ArrayObject { + /** @var string Definition of the OR operator for types */ + const OPERATOR_OR = '|'; + /** @var string Definition of the ARRAY operator for types */ const OPERATOR_ARRAY = '[]'; @@ -164,11 +167,27 @@ class Collection extends \ArrayObject .var_export($type, true) ); } - $expanded_type = $this->expand($type); - if ($expanded_type) { - $this[] = $expanded_type; + + // separate the type by the OR operator + $type_parts = explode(self::OPERATOR_OR, $type); + foreach ($type_parts as $part) { + $expanded_type = $this->expand($part); + if ($expanded_type) { + $this[] = $expanded_type; + } } } + + /** + * Returns a string representation of the collection. + * + * @return string The resolved types across the collection, separated with + * {@link self::OPERATOR_OR}. + */ + public function __toString() + { + return implode(self::OPERATOR_OR, $this->getArrayCopy()); + } /** * Analyzes the given type and returns the FQCN variant. diff --git a/tests/phpDocumentor/Reflection/DocBlock/Type/CollectionTest.php b/tests/phpDocumentor/Reflection/DocBlock/Type/CollectionTest.php index b5ed4cd..0b6fa91 100644 --- a/tests/phpDocumentor/Reflection/DocBlock/Type/CollectionTest.php +++ b/tests/phpDocumentor/Reflection/DocBlock/Type/CollectionTest.php @@ -148,9 +148,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $collection = new Collection(); $collection->setNamespace('\My\Space'); $collection->setNamespaceAliases(array('Alias' => '\My\Space\Aliasing')); - foreach ($fixture as $type) { - $collection->add($type); - } + $collection->add($fixture); $this->assertSame($expected, $collection->getArrayCopy()); } @@ -168,9 +166,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase { $collection = new Collection(); $collection->setNamespaceAliases(array('Alias' => '\My\Space\Aliasing')); - foreach ($fixture as $type) { - $collection->add($type); - } + $collection->add($fixture); $this->assertSame($expected, $collection->getArrayCopy()); } @@ -199,34 +195,34 @@ class CollectionTest extends \PHPUnit_Framework_TestCase public function provideTypesToExpand($method, $namespace = '\My\Space\\') { return array( - array(array(''), array()), - array(array(' '), array()), - array(array('int'), array('int')), - array(array('int '), array('int')), - array(array('string'), array('string')), - array(array('DocBlock'), array($namespace.'DocBlock')), - array(array('DocBlock[]'), array($namespace.'DocBlock[]')), - array(array(' DocBlock '), array($namespace.'DocBlock')), - array(array('\My\Space\DocBlock'), array('\My\Space\DocBlock')), - array(array('Alias\DocBlock'), array('\My\Space\Aliasing\DocBlock')), + array('', array()), + array(' ', array()), + array('int', array('int')), + array('int ', array('int')), + array('string', array('string')), + array('DocBlock', array($namespace.'DocBlock')), + array('DocBlock[]', array($namespace.'DocBlock[]')), + array(' DocBlock ', array($namespace.'DocBlock')), + array('\My\Space\DocBlock', array('\My\Space\DocBlock')), + array('Alias\DocBlock', array('\My\Space\Aliasing\DocBlock')), array( - array('DocBlock', 'Tag'), + 'DocBlock|Tag', array($namespace .'DocBlock', $namespace .'Tag') ), array( - array('DocBlock', 'null'), + 'DocBlock|null', array($namespace.'DocBlock', 'null') ), array( - array('\My\Space\DocBlock', 'Tag'), + '\My\Space\DocBlock|Tag', array('\My\Space\DocBlock', $namespace.'Tag') ), array( - array('DocBlock[]', 'null'), + 'DocBlock[]|null', array($namespace.'DocBlock[]', 'null') ), array( - array('DocBlock[]', 'int[]'), + 'DocBlock[]|int[]', array($namespace.'DocBlock[]', 'int[]') ), );