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 39faeec91d
commit 9da4a65065
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.