Skip to content

Commit 7e5538a

Browse files
authored
Merge pull request #9 from bbprojectnet/generic-types-fix
Generic types recognitions bug fix
2 parents 144140c + 295d2fb commit 7e5538a

File tree

4 files changed

+87
-16
lines changed

4 files changed

+87
-16
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ReturnTag extends Tag
2626
{
2727
/** @var string The raw type component. */
2828
protected $type = '';
29-
29+
3030
/** @var Collection The parsed type component. */
3131
protected $types = null;
3232

@@ -49,7 +49,7 @@ public function setContent($content)
4949
{
5050
parent::setContent($content);
5151

52-
$parts = preg_split('/\s+/Su', $this->description, 2);
52+
$parts = preg_split('/(?<!,)\s+/Su', $this->description, 2);
5353

5454
// any output is considered a type
5555
$this->type = $parts[0];
@@ -112,7 +112,7 @@ public function addType($type)
112112

113113
/**
114114
* Returns the type collection.
115-
*
115+
*
116116
* @return void
117117
*/
118118
protected function getTypesCollection()

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,18 @@ public function add($type)
106106
}
107107

108108
// separate the type by the OR operator
109-
$type_parts = explode(self::OPERATOR_OR, $type);
109+
$type_parts = $this->explode($type);
110110
foreach ($type_parts as $part) {
111111
$expanded_type = $this->expand($part);
112112
if ($expanded_type) {
113113
$this[] = $expanded_type;
114114
}
115115
}
116116
}
117-
117+
118118
/**
119119
* Returns a string representation of the collection.
120-
*
120+
*
121121
* @return string The resolved types across the collection, separated with
122122
* {@link self::OPERATOR_OR}.
123123
*/
@@ -126,6 +126,40 @@ public function __toString()
126126
return implode(self::OPERATOR_OR, $this->getArrayCopy());
127127
}
128128

129+
/**
130+
* Analyzes the given union of types and returns separated by OR operator
131+
* single types.
132+
*
133+
* @param string $type The type or union of types
134+
*
135+
* @return array
136+
*/
137+
protected function explode($type)
138+
{
139+
$type_parts = [];
140+
$curr_type = '';
141+
$nest_level = 0;
142+
143+
foreach (str_split($type) as $char) {
144+
if ($char === self::OPERATOR_OR && $nest_level === 0) {
145+
$type_parts[] = $curr_type;
146+
$curr_type = '';
147+
} else {
148+
if ($char === '<') {
149+
$nest_level++;
150+
} else if ($char === '>') {
151+
$nest_level--;
152+
}
153+
154+
$curr_type .= $char;
155+
}
156+
}
157+
158+
$type_parts[] = $curr_type;
159+
160+
return $type_parts;
161+
}
162+
129163
/**
130164
* Analyzes the given type and returns the FQCN variant.
131165
*

tests/Barryvdh/Reflection/DocBlock/Tag/ReturnTagTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* phpDocumentor Return tag test.
4-
*
4+
*
55
* PHP version 5.3
66
*
77
* @author Mike van Riel <mike.vanriel@naenius.com>
@@ -96,6 +96,27 @@ public function provideDataForConstructor()
9696
'int',
9797
array('int'),
9898
"Number of Bobs"
99+
),
100+
array(
101+
'return',
102+
'array<int, string> Types of Bobs',
103+
'array<int, string>',
104+
array('array<int, string>'),
105+
'Types of Bobs'
106+
),
107+
array(
108+
'return',
109+
'array<int, string>|string Types of Bobs',
110+
'array<int, string>|string',
111+
array('array<int, string>', 'string'),
112+
'Types of Bobs'
113+
),
114+
array(
115+
'return',
116+
'array<int, string|bool>|string Types of Bobs',
117+
'array<int, string|bool>|string',
118+
array('array<int, string|bool>', 'string'),
119+
'Types of Bobs'
99120
)
100121
);
101122
}

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* phpDocumentor Collection Test
4-
*
4+
*
55
* PHP version 5.3
66
*
77
* @author Mike van Riel <mike.vanriel@naenius.com>
@@ -16,7 +16,7 @@
1616

1717
/**
1818
* Test class for \Barryvdh\Reflection\DocBlock\Type\Collection
19-
*
19+
*
2020
* @covers Barryvdh\Reflection\DocBlock\Type\Collection
2121
*
2222
* @author Mike van Riel <mike.vanriel@naenius.com>
@@ -29,7 +29,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
2929
/**
3030
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct
3131
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::getContext
32-
*
32+
*
3333
* @return void
3434
*/
3535
public function testConstruct()
@@ -42,7 +42,7 @@ public function testConstruct()
4242

4343
/**
4444
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct
45-
*
45+
*
4646
* @return void
4747
*/
4848
public function testConstructWithTypes()
@@ -53,7 +53,7 @@ public function testConstructWithTypes()
5353

5454
/**
5555
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct
56-
*
56+
*
5757
* @return void
5858
*/
5959
public function testConstructWithNamespace()
@@ -70,7 +70,7 @@ public function testConstructWithNamespace()
7070

7171
/**
7272
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::__construct
73-
*
73+
*
7474
* @return void
7575
*/
7676
public function testConstructWithNamespaceAliases()
@@ -89,7 +89,7 @@ public function testConstructWithNamespaceAliases()
8989
*
9090
* @dataProvider provideTypesToExpand
9191
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::add
92-
*
92+
*
9393
* @return void
9494
*/
9595
public function testAdd($fixture, $expected)
@@ -109,7 +109,7 @@ public function testAdd($fixture, $expected)
109109
*
110110
* @dataProvider provideTypesToExpandWithoutNamespace
111111
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::add
112-
*
112+
*
113113
* @return void
114114
*/
115115
public function testAddWithoutNamespace($fixture, $expected)
@@ -146,7 +146,7 @@ public function testAddMethodsAndProperties($fixture, $expected)
146146
/**
147147
* @covers Barryvdh\Reflection\DocBlock\Type\Collection::add
148148
* @expectedException InvalidArgumentException
149-
*
149+
*
150150
* @return void
151151
*/
152152
public function testAddWithInvalidArgument()
@@ -197,6 +197,22 @@ public function provideTypesToExpand($method, $namespace = '\My\Space\\')
197197
'DocBlock[]|int[]',
198198
array($namespace.'DocBlock[]', 'int[]')
199199
),
200+
array(
201+
'array<int, string>',
202+
array('array<int, string>')
203+
),
204+
array(
205+
'array<int, string>|string',
206+
array('array<int, string>', 'string')
207+
),
208+
array(
209+
'array<int, float|bool>|string',
210+
array('array<int, float|bool>', 'string')
211+
),
212+
array(
213+
'array<int, string|array<int, bool>>|array<int, float>|string',
214+
array('array<int, string|array<int, bool>>', 'array<int, float>', 'string')
215+
),
200216
array(
201217
'LinkDescriptor::setLink()',
202218
array($namespace.'LinkDescriptor::setLink()')

0 commit comments

Comments
 (0)