From 10c99960dcf6e5782f4810ad37bad7d2d9ac9c56 Mon Sep 17 00:00:00 2001 From: Hidde Boomsma Date: Thu, 7 Apr 2016 15:15:09 +0200 Subject: [PATCH] Allow typed arrays in @method annotation. Since the type resolver has support for typed arrays, I allowed the syntax for the @method annotation. In effect you can now annotate magic methods like: @method Object[] myMethod($arg1, $arg2) @method int[] myOtherMethod() --- src/DocBlock/Tags/Method.php | 6 ++- tests/unit/DocBlock/Tags/MethodTest.php | 50 ++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/DocBlock/Tags/Method.php b/src/DocBlock/Tags/Method.php index 86b4286..9e59c35 100644 --- a/src/DocBlock/Tags/Method.php +++ b/src/DocBlock/Tags/Method.php @@ -91,7 +91,11 @@ final class Method extends BaseTag implements Factory\StaticMethod )? # Return type (?: - ([\w\|_\\\\]+) + ( + (?:[\w\|_\\\\]+) + # array notation + (?:\[\])* + )? \s+ )? # Legacy method name (not captured) diff --git a/tests/unit/DocBlock/Tags/MethodTest.php b/tests/unit/DocBlock/Tags/MethodTest.php index 4d53e99..fe385af 100644 --- a/tests/unit/DocBlock/Tags/MethodTest.php +++ b/tests/unit/DocBlock/Tags/MethodTest.php @@ -15,10 +15,11 @@ namespace phpDocumentor\Reflection\DocBlock\Tags; use Mockery as m; use phpDocumentor\Reflection\DocBlock\Description; use phpDocumentor\Reflection\DocBlock\DescriptionFactory; -use phpDocumentor\Reflection\Fqsen; -use phpDocumentor\Reflection\FqsenResolver; use phpDocumentor\Reflection\TypeResolver; +use phpDocumentor\Reflection\Types\Array_; +use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Context; +use phpDocumentor\Reflection\Types\Integer; use phpDocumentor\Reflection\Types\Object_; use phpDocumentor\Reflection\Types\String_; use phpDocumentor\Reflection\Types\Void; @@ -248,6 +249,51 @@ class MethodTest extends \PHPUnit_Framework_TestCase $this->assertSame($description, $fixture->getDescription()); } + public function collectionReturnTypesProvider() + { + return [ + ['int[]', Array_::class, Integer::class, Compound::class], + ['int[][]', Array_::class, Array_::class, Compound::class], + ['Object[]', Array_::class, Object_::class, Compound::class], + ['array[]', Array_::class, Array_::class, Compound::class], + ]; + } + + /** + * @dataProvider collectionReturnTypesProvider + * @covers ::create + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Method:: + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\TypeResolver + * @uses \phpDocumentor\Reflection\Types\Array_ + * @uses \phpDocumentor\Reflection\Types\Compound + * @uses \phpDocumentor\Reflection\Types\Integer + * @uses \phpDocumentor\Reflection\Types\Object_ + * @param string $returnType + * @param string $expectedType + * @param string $expectedValueType + * @param string null $expectedKeyType + */ + public function testCollectionReturnTypes( + $returnType, + $expectedType, + $expectedValueType = null, + $expectedKeyType = null + ) { $resolver = new TypeResolver(); + $descriptionFactory = m::mock(DescriptionFactory::class); + $descriptionFactory->shouldReceive('create')->with('', null)->andReturn(new Description('')); + + $fixture = Method::create("$returnType myMethod(\$arg)", $resolver, $descriptionFactory); + $returnType = $fixture->getReturnType(); + $this->assertInstanceOf($expectedType, $returnType); + + if ($returnType instanceof Array_) { + $this->assertInstanceOf($expectedValueType, $returnType->getValueType()); + $this->assertInstanceOf($expectedKeyType, $returnType->getKeyType()); + } + } + /** * @covers ::create * @expectedException \InvalidArgumentException