Skip to content

Commit 45ada3e

Browse files
committed
Merge pull request #73 from hboomsma/feature/typed-array
Allow typed arrays in @method annotation.
2 parents 9a0bd76 + 10c9996 commit 45ada3e

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/DocBlock/Tags/Method.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ public static function create(
9191
)?
9292
# Return type
9393
(?:
94-
([\w\|_\\\\]+)
94+
(
95+
(?:[\w\|_\\\\]+)
96+
# array notation
97+
(?:\[\])*
98+
)?
9599
\s+
96100
)?
97101
# Legacy method name (not captured)

tests/unit/DocBlock/Tags/MethodTest.php

+48-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
use Mockery as m;
1616
use phpDocumentor\Reflection\DocBlock\Description;
1717
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
18-
use phpDocumentor\Reflection\Fqsen;
19-
use phpDocumentor\Reflection\FqsenResolver;
2018
use phpDocumentor\Reflection\TypeResolver;
19+
use phpDocumentor\Reflection\Types\Array_;
20+
use phpDocumentor\Reflection\Types\Compound;
2121
use phpDocumentor\Reflection\Types\Context;
22+
use phpDocumentor\Reflection\Types\Integer;
2223
use phpDocumentor\Reflection\Types\Object_;
2324
use phpDocumentor\Reflection\Types\String_;
2425
use phpDocumentor\Reflection\Types\Void;
@@ -248,6 +249,51 @@ public function testFactoryMethod()
248249
$this->assertSame($description, $fixture->getDescription());
249250
}
250251

252+
public function collectionReturnTypesProvider()
253+
{
254+
return [
255+
['int[]', Array_::class, Integer::class, Compound::class],
256+
['int[][]', Array_::class, Array_::class, Compound::class],
257+
['Object[]', Array_::class, Object_::class, Compound::class],
258+
['array[]', Array_::class, Array_::class, Compound::class],
259+
];
260+
}
261+
262+
/**
263+
* @dataProvider collectionReturnTypesProvider
264+
* @covers ::create
265+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Method::<public>
266+
* @uses \phpDocumentor\Reflection\DocBlock\Description
267+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
268+
* @uses \phpDocumentor\Reflection\TypeResolver
269+
* @uses \phpDocumentor\Reflection\Types\Array_
270+
* @uses \phpDocumentor\Reflection\Types\Compound
271+
* @uses \phpDocumentor\Reflection\Types\Integer
272+
* @uses \phpDocumentor\Reflection\Types\Object_
273+
* @param string $returnType
274+
* @param string $expectedType
275+
* @param string $expectedValueType
276+
* @param string null $expectedKeyType
277+
*/
278+
public function testCollectionReturnTypes(
279+
$returnType,
280+
$expectedType,
281+
$expectedValueType = null,
282+
$expectedKeyType = null
283+
) { $resolver = new TypeResolver();
284+
$descriptionFactory = m::mock(DescriptionFactory::class);
285+
$descriptionFactory->shouldReceive('create')->with('', null)->andReturn(new Description(''));
286+
287+
$fixture = Method::create("$returnType myMethod(\$arg)", $resolver, $descriptionFactory);
288+
$returnType = $fixture->getReturnType();
289+
$this->assertInstanceOf($expectedType, $returnType);
290+
291+
if ($returnType instanceof Array_) {
292+
$this->assertInstanceOf($expectedValueType, $returnType->getValueType());
293+
$this->assertInstanceOf($expectedKeyType, $returnType->getKeyType());
294+
}
295+
}
296+
251297
/**
252298
* @covers ::create
253299
* @expectedException \InvalidArgumentException

0 commit comments

Comments
 (0)