Skip to content

Commit 9da4a65

Browse files
committed
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 db20ae3.
1 parent 39faeec commit 9da4a65

File tree

3 files changed

+64
-33
lines changed

3 files changed

+64
-33
lines changed

src/phpDocumentor/Reflection/DocBlock/Tag/ReturnTag.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use phpDocumentor\Reflection\DocBlock;
1616
use phpDocumentor\Reflection\DocBlock\Tag;
17+
use phpDocumentor\Reflection\DocBlock\Type\Collection;
1718

1819
/**
1920
* Reflection class for a @return tag in a Docblock.
@@ -24,8 +25,11 @@
2425
*/
2526
class ReturnTag extends Tag
2627
{
27-
/** @var string */
28+
/** @var string The raw type component. */
2829
protected $type = '';
30+
31+
/** @var Collection The parsed type component. */
32+
protected $types = null;
2933

3034
/**
3135
* Parses a tag and populates the member variables.
@@ -52,13 +56,8 @@ public function __construct($type, $content, DocBlock $docblock = null)
5256
*/
5357
public function getTypes()
5458
{
55-
$types = new \phpDocumentor\Reflection\DocBlock\Type\Collection(
56-
explode('|', $this->type),
57-
$this->docblock ? $this->docblock->getNamespace() : null,
58-
$this->docblock ? $this->docblock->getNamespaceAliases() : array()
59-
);
60-
61-
return $types->getArrayCopy();
59+
$this->refreshTypes();
60+
return $this->types->getArrayCopy();
6261
}
6362

6463
/**
@@ -68,6 +67,23 @@ public function getTypes()
6867
*/
6968
public function getType()
7069
{
71-
return implode('|', $this->getTypes());
70+
$this->refreshTypes();
71+
return (string) $this->types;
72+
}
73+
74+
/**
75+
* Parses the type, if needed.
76+
*
77+
* @return void
78+
*/
79+
protected function refreshTypes()
80+
{
81+
if (null === $this->types) {
82+
$this->types = new Collection(
83+
array($this->type),
84+
$this->docblock ? $this->docblock->getNamespace() : null,
85+
$this->docblock ? $this->docblock->getNamespaceAliases() : array()
86+
);
87+
}
7288
}
7389
}

src/phpDocumentor/Reflection/DocBlock/Type/Collection.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
class Collection extends \ArrayObject
2424
{
25+
/** @var string Definition of the OR operator for types */
26+
const OPERATOR_OR = '|';
27+
2528
/** @var string Definition of the ARRAY operator for types */
2629
const OPERATOR_ARRAY = '[]';
2730

@@ -164,11 +167,27 @@ public function add($type)
164167
.var_export($type, true)
165168
);
166169
}
167-
$expanded_type = $this->expand($type);
168-
if ($expanded_type) {
169-
$this[] = $expanded_type;
170+
171+
// separate the type by the OR operator
172+
$type_parts = explode(self::OPERATOR_OR, $type);
173+
foreach ($type_parts as $part) {
174+
$expanded_type = $this->expand($part);
175+
if ($expanded_type) {
176+
$this[] = $expanded_type;
177+
}
170178
}
171179
}
180+
181+
/**
182+
* Returns a string representation of the collection.
183+
*
184+
* @return string The resolved types across the collection, separated with
185+
* {@link self::OPERATOR_OR}.
186+
*/
187+
public function __toString()
188+
{
189+
return implode(self::OPERATOR_OR, $this->getArrayCopy());
190+
}
172191

173192
/**
174193
* Analyzes the given type and returns the FQCN variant.

tests/phpDocumentor/Reflection/DocBlock/Type/CollectionTest.php

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ public function testAdd($fixture, $expected)
148148
$collection = new Collection();
149149
$collection->setNamespace('\My\Space');
150150
$collection->setNamespaceAliases(array('Alias' => '\My\Space\Aliasing'));
151-
foreach ($fixture as $type) {
152-
$collection->add($type);
153-
}
151+
$collection->add($fixture);
154152

155153
$this->assertSame($expected, $collection->getArrayCopy());
156154
}
@@ -168,9 +166,7 @@ public function testAddWithoutNamespace($fixture, $expected)
168166
{
169167
$collection = new Collection();
170168
$collection->setNamespaceAliases(array('Alias' => '\My\Space\Aliasing'));
171-
foreach ($fixture as $type) {
172-
$collection->add($type);
173-
}
169+
$collection->add($fixture);
174170

175171
$this->assertSame($expected, $collection->getArrayCopy());
176172
}
@@ -199,34 +195,34 @@ public function testAddWithInvalidArgument()
199195
public function provideTypesToExpand($method, $namespace = '\My\Space\\')
200196
{
201197
return array(
202-
array(array(''), array()),
203-
array(array(' '), array()),
204-
array(array('int'), array('int')),
205-
array(array('int '), array('int')),
206-
array(array('string'), array('string')),
207-
array(array('DocBlock'), array($namespace.'DocBlock')),
208-
array(array('DocBlock[]'), array($namespace.'DocBlock[]')),
209-
array(array(' DocBlock '), array($namespace.'DocBlock')),
210-
array(array('\My\Space\DocBlock'), array('\My\Space\DocBlock')),
211-
array(array('Alias\DocBlock'), array('\My\Space\Aliasing\DocBlock')),
198+
array('', array()),
199+
array(' ', array()),
200+
array('int', array('int')),
201+
array('int ', array('int')),
202+
array('string', array('string')),
203+
array('DocBlock', array($namespace.'DocBlock')),
204+
array('DocBlock[]', array($namespace.'DocBlock[]')),
205+
array(' DocBlock ', array($namespace.'DocBlock')),
206+
array('\My\Space\DocBlock', array('\My\Space\DocBlock')),
207+
array('Alias\DocBlock', array('\My\Space\Aliasing\DocBlock')),
212208
array(
213-
array('DocBlock', 'Tag'),
209+
'DocBlock|Tag',
214210
array($namespace .'DocBlock', $namespace .'Tag')
215211
),
216212
array(
217-
array('DocBlock', 'null'),
213+
'DocBlock|null',
218214
array($namespace.'DocBlock', 'null')
219215
),
220216
array(
221-
array('\My\Space\DocBlock', 'Tag'),
217+
'\My\Space\DocBlock|Tag',
222218
array('\My\Space\DocBlock', $namespace.'Tag')
223219
),
224220
array(
225-
array('DocBlock[]', 'null'),
221+
'DocBlock[]|null',
226222
array($namespace.'DocBlock[]', 'null')
227223
),
228224
array(
229-
array('DocBlock[]', 'int[]'),
225+
'DocBlock[]|int[]',
230226
array($namespace.'DocBlock[]', 'int[]')
231227
),
232228
);

0 commit comments

Comments
 (0)