From 019848728e58b2a6f8b8d66b22a4eced52c5344e Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 28 Sep 2016 18:42:19 +0200 Subject: [PATCH] Add support for rest argument notation --- src/DocBlock/Tags/Method.php | 12 ++++++++++- tests/unit/DocBlock/Tags/MethodTest.php | 27 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/DocBlock/Tags/Method.php b/src/DocBlock/Tags/Method.php index 72dd1a0..d600aaa 100644 --- a/src/DocBlock/Tags/Method.php +++ b/src/DocBlock/Tags/Method.php @@ -127,7 +127,7 @@ final class Method extends BaseTag implements Factory\StaticMethod if ('' !== $arguments) { $arguments = explode(',', $arguments); foreach($arguments as &$argument) { - $argument = explode(' ', trim($argument)); + $argument = explode(' ', self::stripRestArg(trim($argument)), 2); if ($argument[0][0] === '$') { $argumentName = substr($argument[0], 1); $argumentType = new Void_(); @@ -135,6 +135,7 @@ final class Method extends BaseTag implements Factory\StaticMethod $argumentType = $typeResolver->resolve($argument[0], $context); $argumentName = ''; if (isset($argument[1])) { + $argument[1] = self::stripRestArg($argument[1]); $argumentName = substr($argument[1], 1); } } @@ -217,4 +218,13 @@ final class Method extends BaseTag implements Factory\StaticMethod return $arguments; } + + private static function stripRestArg($argument) + { + if (strpos($argument, '...') === 0) { + $argument = trim(substr($argument, 3)); + } + + return $argument; + } } diff --git a/tests/unit/DocBlock/Tags/MethodTest.php b/tests/unit/DocBlock/Tags/MethodTest.php index aae6472..5d6e981 100644 --- a/tests/unit/DocBlock/Tags/MethodTest.php +++ b/tests/unit/DocBlock/Tags/MethodTest.php @@ -139,6 +139,33 @@ class MethodTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $fixture->getArguments()); } + /** + * @covers ::create + */ + public function testRestArgumentIsParsedAsRegularArg() + { + $expected = [ + [ 'name' => 'arg1', 'type' => new Void_() ], + [ 'name' => 'rest', 'type' => new Void_() ], + [ 'name' => 'rest2', 'type' => new Array_() ], + ]; + + $descriptionFactory = m::mock(DescriptionFactory::class); + $resolver = new TypeResolver(); + $context = new Context(''); + $description = new Description(''); + $descriptionFactory->shouldReceive('create')->with('', $context)->andReturn($description); + + $fixture = Method::create( + 'void myMethod($arg1, ...$rest, array ... $rest2)', + $resolver, + $descriptionFactory, + $context + ); + + $this->assertEquals($expected, $fixture->getArguments()); + } + /** * @covers ::__construct * @covers ::getReturnType