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 db20ae39fb.
This commit is contained in:
Vasil Rangelov
2012-11-18 18:50:45 +02:00
parent cee63f991d
commit a4b899e96c
3 changed files with 64 additions and 33 deletions
@@ -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()
);
}
}
}
@@ -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.
@@ -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[]')
),
);