diff --git a/src/DocBlock/Tags/Method.php b/src/DocBlock/Tags/Method.php index 5a5d22d4..d3c1cd11 100644 --- a/src/DocBlock/Tags/Method.php +++ b/src/DocBlock/Tags/Method.php @@ -98,8 +98,8 @@ public static function create( (?:[\w\|_\\\\]+) # array notation (?:\[\])* - ) - )? + )* + ) \s+ )? # Legacy method name (not captured) @@ -125,6 +125,11 @@ public static function create( list(, $static, $returnType, $methodName, $arguments, $description) = $matches; $static = $static === 'static'; + + if ($returnType === '') { + $returnType = 'void'; + } + $returnType = $typeResolver->resolve($returnType, $context); $description = $descriptionFactory->create($description, $context); @@ -196,11 +201,11 @@ public function __toString() $arguments[] = $argument['type'] . ' $' . $argument['name']; } - return ($this->isStatic() ? 'static ' : '') + return trim(($this->isStatic() ? 'static ' : '') . (string)$this->returnType . ' ' . $this->methodName . '(' . implode(', ', $arguments) . ')' - . ($this->description ? ' ' . $this->description->render() : ''); + . ($this->description ? ' ' . $this->description->render() : '')); } private function filterArguments($arguments) diff --git a/tests/unit/DocBlock/Tags/MethodTest.php b/tests/unit/DocBlock/Tags/MethodTest.php index dce6dfd7..5860abd1 100644 --- a/tests/unit/DocBlock/Tags/MethodTest.php +++ b/tests/unit/DocBlock/Tags/MethodTest.php @@ -15,6 +15,7 @@ use Mockery as m; use phpDocumentor\Reflection\DocBlock\Description; use phpDocumentor\Reflection\DocBlock\DescriptionFactory; +use phpDocumentor\Reflection\Fqsen; use phpDocumentor\Reflection\TypeResolver; use phpDocumentor\Reflection\Types\Array_; use phpDocumentor\Reflection\Types\Compound; @@ -305,7 +306,7 @@ public function testReturnTypeThis() ); $this->assertTrue($fixture->isStatic()); - $this->assertSame('static $this myMethod() ', (string)$fixture); + $this->assertSame('static $this myMethod()', (string)$fixture); $this->assertSame('myMethod', $fixture->getMethodName()); $this->assertInstanceOf(This::class, $fixture->getReturnType()); } @@ -468,4 +469,78 @@ public function testCreateMethodParenthesisMissing() $this->assertInstanceOf(Void_::class, $fixture->getReturnType()); $this->assertSame($description, $fixture->getDescription()); } + + /** + * @covers ::create + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Method:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\TypeResolver + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Fqsen + * @uses \phpDocumentor\Reflection\Types\Context + * @uses \phpDocumentor\Reflection\Types\Void_ + */ + public function testCreateWithoutReturnType() + { + $descriptionFactory = m::mock(DescriptionFactory::class); + $resolver = new TypeResolver(); + $context = new Context(''); + + $description = new Description(''); + + $descriptionFactory->shouldReceive('create')->with('', $context)->andReturn($description); + + $fixture = Method::create( + 'myMethod()', + $resolver, + $descriptionFactory, + $context + ); + + $this->assertSame('void myMethod()', (string)$fixture); + $this->assertSame('myMethod', $fixture->getMethodName()); + $this->assertEquals([], $fixture->getArguments()); + $this->assertInstanceOf(Void_::class, $fixture->getReturnType()); + $this->assertSame($description, $fixture->getDescription()); + } + + /** + * @covers ::create + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Method:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\TypeResolver + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Fqsen + * @uses \phpDocumentor\Reflection\Types\Context + * @uses \phpDocumentor\Reflection\Types\Array_ + * @uses \phpDocumentor\Reflection\Types\Compound + * @uses \phpDocumentor\Reflection\Types\Integer + * @uses \phpDocumentor\Reflection\Types\Object_ + */ + public function testCreateWithMixedReturnTypes() + { + $descriptionFactory = m::mock(DescriptionFactory::class); + $resolver = new TypeResolver(); + $context = new Context(''); + + $descriptionFactory->shouldReceive('create')->andReturn(new Description('')); + + $fixture = Method::create( + 'MyClass[]|int[] myMethod()', + $resolver, + $descriptionFactory, + $context + ); + + $this->assertSame('\MyClass[]|int[] myMethod()', (string)$fixture); + $this->assertSame('myMethod', $fixture->getMethodName()); + $this->assertEquals([], $fixture->getArguments()); + + $this->assertEquals( + new Compound([ + new Array_(new Object_(new Fqsen('\MyClass'))), + new Array_(new Integer()), + ]) + , $fixture->getReturnType()); + } }