From 678cf682bfb789e7269a450cbe0002c881c6c499 Mon Sep 17 00:00:00 2001 From: Fabian Grutschus Date: Fri, 26 Feb 2016 16:51:43 +0100 Subject: [PATCH] Fix method name is empty when parenthesis is missing in @method --- src/DocBlock/Tags/Method.php | 34 +++++++++++++++---------- tests/unit/DocBlock/Tags/MethodTest.php | 33 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/DocBlock/Tags/Method.php b/src/DocBlock/Tags/Method.php index c0c9fa5..739c3e0 100644 --- a/src/DocBlock/Tags/Method.php +++ b/src/DocBlock/Tags/Method.php @@ -101,7 +101,9 @@ final class Method extends BaseTag implements Factory\StaticMethod # Method name ([\w\|_\\\\]+) # Arguments - \(([^\)]*)\) + (?: + \(([^\)]*)\) + )? \s* # Description (.*) @@ -118,21 +120,25 @@ final class Method extends BaseTag implements Factory\StaticMethod $returnType = $typeResolver->resolve($returnType, $context); $description = $descriptionFactory->create($description, $context); - $arguments = explode(',', $arguments); - foreach($arguments as &$argument) { - $argument = explode(' ', trim($argument)); - if ($argument[0][0] === '$') { - $argumentName = substr($argument[0], 1); - $argumentType = new Void(); - } else { - $argumentType = $typeResolver->resolve($argument[0], $context); - $argumentName = ''; - if (isset($argument[1])) { - $argumentName = substr($argument[1], 1); + if ('' !== $arguments) { + $arguments = explode(',', $arguments); + foreach($arguments as &$argument) { + $argument = explode(' ', trim($argument)); + if ($argument[0][0] === '$') { + $argumentName = substr($argument[0], 1); + $argumentType = new Void(); + } else { + $argumentType = $typeResolver->resolve($argument[0], $context); + $argumentName = ''; + if (isset($argument[1])) { + $argumentName = substr($argument[1], 1); + } } - } - $argument = [ 'name' => $argumentName, 'type' => $argumentType]; + $argument = [ 'name' => $argumentName, 'type' => $argumentType]; + } + } else { + $arguments = []; } return new static($methodName, $arguments, $returnType, $static, $description); diff --git a/tests/unit/DocBlock/Tags/MethodTest.php b/tests/unit/DocBlock/Tags/MethodTest.php index 4d53e99..ac2d0bb 100644 --- a/tests/unit/DocBlock/Tags/MethodTest.php +++ b/tests/unit/DocBlock/Tags/MethodTest.php @@ -328,4 +328,37 @@ class MethodTest extends \PHPUnit_Framework_TestCase { new Method('body', [ [ 'name' => 'myName', 'unknown' => 'nah' ] ]); } + + /** + * @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 + */ + public function testCreateMethodParenthesisMissing() + { + $descriptionFactory = m::mock(DescriptionFactory::class); + $resolver = new TypeResolver(); + $context = new Context(''); + + $description = new Description('My Description'); + + $descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description); + + $fixture = Method::create( + 'static void myMethod My Description', + $resolver, + $descriptionFactory, + $context + ); + + $this->assertSame('static void myMethod() My Description', (string)$fixture); + $this->assertSame('myMethod', $fixture->getMethodName()); + $this->assertEquals([], $fixture->getArguments()); + $this->assertInstanceOf(Void::class, $fixture->getReturnType()); + $this->assertSame($description, $fixture->getDescription()); + } }