Skip to content

Commit 0198487

Browse files
committed
Add support for rest argument notation
1 parent 9270140 commit 0198487

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/DocBlock/Tags/Method.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,15 @@ public static function create(
127127
if ('' !== $arguments) {
128128
$arguments = explode(',', $arguments);
129129
foreach($arguments as &$argument) {
130-
$argument = explode(' ', trim($argument));
130+
$argument = explode(' ', self::stripRestArg(trim($argument)), 2);
131131
if ($argument[0][0] === '$') {
132132
$argumentName = substr($argument[0], 1);
133133
$argumentType = new Void_();
134134
} else {
135135
$argumentType = $typeResolver->resolve($argument[0], $context);
136136
$argumentName = '';
137137
if (isset($argument[1])) {
138+
$argument[1] = self::stripRestArg($argument[1]);
138139
$argumentName = substr($argument[1], 1);
139140
}
140141
}
@@ -217,4 +218,13 @@ private function filterArguments($arguments)
217218

218219
return $arguments;
219220
}
221+
222+
private static function stripRestArg($argument)
223+
{
224+
if (strpos($argument, '...') === 0) {
225+
$argument = trim(substr($argument, 3));
226+
}
227+
228+
return $argument;
229+
}
220230
}

tests/unit/DocBlock/Tags/MethodTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,33 @@ public function testArgumentTypeCanBeInferredAsVoid()
139139
$this->assertEquals($expected, $fixture->getArguments());
140140
}
141141

142+
/**
143+
* @covers ::create
144+
*/
145+
public function testRestArgumentIsParsedAsRegularArg()
146+
{
147+
$expected = [
148+
[ 'name' => 'arg1', 'type' => new Void_() ],
149+
[ 'name' => 'rest', 'type' => new Void_() ],
150+
[ 'name' => 'rest2', 'type' => new Array_() ],
151+
];
152+
153+
$descriptionFactory = m::mock(DescriptionFactory::class);
154+
$resolver = new TypeResolver();
155+
$context = new Context('');
156+
$description = new Description('');
157+
$descriptionFactory->shouldReceive('create')->with('', $context)->andReturn($description);
158+
159+
$fixture = Method::create(
160+
'void myMethod($arg1, ...$rest, array ... $rest2)',
161+
$resolver,
162+
$descriptionFactory,
163+
$context
164+
);
165+
166+
$this->assertEquals($expected, $fixture->getArguments());
167+
}
168+
142169
/**
143170
* @covers ::__construct
144171
* @covers ::getReturnType

0 commit comments

Comments
 (0)