Skip to content

Commit 9a0bd76

Browse files
committed
Merge pull request #72 from fabiang/fix-missing-parenthesis
Fix method name is empty when parenthesis is missing in @method
2 parents 52723f0 + 678cf68 commit 9a0bd76

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

src/DocBlock/Tags/Method.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ public static function create(
101101
# Method name
102102
([\w\|_\\\\]+)
103103
# Arguments
104-
\(([^\)]*)\)
104+
(?:
105+
\(([^\)]*)\)
106+
)?
105107
\s*
106108
# Description
107109
(.*)
@@ -118,21 +120,25 @@ public static function create(
118120
$returnType = $typeResolver->resolve($returnType, $context);
119121
$description = $descriptionFactory->create($description, $context);
120122

121-
$arguments = explode(',', $arguments);
122-
foreach($arguments as &$argument) {
123-
$argument = explode(' ', trim($argument));
124-
if ($argument[0][0] === '$') {
125-
$argumentName = substr($argument[0], 1);
126-
$argumentType = new Void();
127-
} else {
128-
$argumentType = $typeResolver->resolve($argument[0], $context);
129-
$argumentName = '';
130-
if (isset($argument[1])) {
131-
$argumentName = substr($argument[1], 1);
123+
if ('' !== $arguments) {
124+
$arguments = explode(',', $arguments);
125+
foreach($arguments as &$argument) {
126+
$argument = explode(' ', trim($argument));
127+
if ($argument[0][0] === '$') {
128+
$argumentName = substr($argument[0], 1);
129+
$argumentType = new Void();
130+
} else {
131+
$argumentType = $typeResolver->resolve($argument[0], $context);
132+
$argumentName = '';
133+
if (isset($argument[1])) {
134+
$argumentName = substr($argument[1], 1);
135+
}
132136
}
133-
}
134137

135-
$argument = [ 'name' => $argumentName, 'type' => $argumentType];
138+
$argument = [ 'name' => $argumentName, 'type' => $argumentType];
139+
}
140+
} else {
141+
$arguments = [];
136142
}
137143

138144
return new static($methodName, $arguments, $returnType, $static, $description);

tests/unit/DocBlock/Tags/MethodTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,37 @@ public function testCreationFailsIfArgumentRecordContainsInvalidEntry()
328328
{
329329
new Method('body', [ [ 'name' => 'myName', 'unknown' => 'nah' ] ]);
330330
}
331+
332+
/**
333+
* @covers ::create
334+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Method::<public>
335+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
336+
* @uses \phpDocumentor\Reflection\TypeResolver
337+
* @uses \phpDocumentor\Reflection\DocBlock\Description
338+
* @uses \phpDocumentor\Reflection\Fqsen
339+
* @uses \phpDocumentor\Reflection\Types\Context
340+
*/
341+
public function testCreateMethodParenthesisMissing()
342+
{
343+
$descriptionFactory = m::mock(DescriptionFactory::class);
344+
$resolver = new TypeResolver();
345+
$context = new Context('');
346+
347+
$description = new Description('My Description');
348+
349+
$descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description);
350+
351+
$fixture = Method::create(
352+
'static void myMethod My Description',
353+
$resolver,
354+
$descriptionFactory,
355+
$context
356+
);
357+
358+
$this->assertSame('static void myMethod() My Description', (string)$fixture);
359+
$this->assertSame('myMethod', $fixture->getMethodName());
360+
$this->assertEquals([], $fixture->getArguments());
361+
$this->assertInstanceOf(Void::class, $fixture->getReturnType());
362+
$this->assertSame($description, $fixture->getDescription());
363+
}
331364
}

0 commit comments

Comments
 (0)