From ae73fef28fcd431266b89edb4440ee9a80a4421d Mon Sep 17 00:00:00 2001 From: Simon Verwaard Date: Sun, 4 Jul 2021 21:02:44 +0200 Subject: [PATCH] Include default argument value when argument has no type --- src/DocBlock/Tags/Method.php | 2 +- tests/unit/DocBlock/Tags/MethodTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/DocBlock/Tags/Method.php b/src/DocBlock/Tags/Method.php index 08c0407e..c7168591 100644 --- a/src/DocBlock/Tags/Method.php +++ b/src/DocBlock/Tags/Method.php @@ -155,7 +155,7 @@ public static function create( foreach ($argumentsExploded as $argument) { $argument = explode(' ', self::stripRestArg(trim($argument)), 2); if (strpos($argument[0], '$') === 0) { - $argumentName = substr($argument[0], 1); + $argumentName = substr(implode(' ', $argument), 1); $argumentType = new Mixed_(); } else { $argumentType = $typeResolver->resolve($argument[0], $context); diff --git a/tests/unit/DocBlock/Tags/MethodTest.php b/tests/unit/DocBlock/Tags/MethodTest.php index a07c98a3..ba006304 100644 --- a/tests/unit/DocBlock/Tags/MethodTest.php +++ b/tests/unit/DocBlock/Tags/MethodTest.php @@ -193,6 +193,31 @@ public function testRestArgumentIsParsedAsRegularArg() : void $this->assertEquals($expected, $fixture->getArguments()); } + public function testArgumentHasDefaultValue() : void + { + $expected = [ + ['name' => "arg1 = ''", 'type' => new Mixed_()], + ['name' => "arg2 = ['value']", 'type' => new Mixed_()], + ['name' => "arg3 = 'test'", 'type' => new String_()], + ['name' => "arg4 = ['value']", '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 = \'\', $arg2 = [\'value\'], string $arg3 = \'test\', array $arg4 = [\'value\'])', + $resolver, + $descriptionFactory, + $context + ); + + $this->assertEquals($expected, $fixture->getArguments()); + } + /** * @covers ::__construct * @covers ::getReturnType