Skip to content

Commit b453d2c

Browse files
committed
Allow omitting method return type
Support for `@method myMethod()` was missing but according to the docs of phpdocumentor this is possible.
1 parent 8ba1708 commit b453d2c

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/DocBlock/Tags/Method.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public static function create(
125125
list(, $static, $returnType, $methodName, $arguments, $description) = $matches;
126126

127127
$static = $static === 'static';
128+
129+
if ($returnType === '') {
130+
$returnType = 'void';
131+
}
132+
128133
$returnType = $typeResolver->resolve($returnType, $context);
129134
$description = $descriptionFactory->create($description, $context);
130135

@@ -196,11 +201,11 @@ public function __toString()
196201
$arguments[] = $argument['type'] . ' $' . $argument['name'];
197202
}
198203

199-
return ($this->isStatic() ? 'static ' : '')
204+
return trim(($this->isStatic() ? 'static ' : '')
200205
. (string)$this->returnType . ' '
201206
. $this->methodName
202207
. '(' . implode(', ', $arguments) . ')'
203-
. ($this->description ? ' ' . $this->description->render() : '');
208+
. ($this->description ? ' ' . $this->description->render() : ''));
204209
}
205210

206211
private function filterArguments($arguments)

tests/unit/DocBlock/Tags/MethodTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public function testReturnTypeThis()
305305
);
306306

307307
$this->assertTrue($fixture->isStatic());
308-
$this->assertSame('static $this myMethod() ', (string)$fixture);
308+
$this->assertSame('static $this myMethod()', (string)$fixture);
309309
$this->assertSame('myMethod', $fixture->getMethodName());
310310
$this->assertInstanceOf(This::class, $fixture->getReturnType());
311311
}
@@ -468,4 +468,28 @@ public function testCreateMethodParenthesisMissing()
468468
$this->assertInstanceOf(Void_::class, $fixture->getReturnType());
469469
$this->assertSame($description, $fixture->getDescription());
470470
}
471+
472+
public function testCreateWithoutReturnType()
473+
{
474+
$descriptionFactory = m::mock(DescriptionFactory::class);
475+
$resolver = new TypeResolver();
476+
$context = new Context('');
477+
478+
$description = new Description('');
479+
480+
$descriptionFactory->shouldReceive('create')->with('', $context)->andReturn($description);
481+
482+
$fixture = Method::create(
483+
'myMethod()',
484+
$resolver,
485+
$descriptionFactory,
486+
$context
487+
);
488+
489+
$this->assertSame('void myMethod()', (string)$fixture);
490+
$this->assertSame('myMethod', $fixture->getMethodName());
491+
$this->assertEquals([], $fixture->getArguments());
492+
$this->assertInstanceOf(Void_::class, $fixture->getReturnType());
493+
$this->assertSame($description, $fixture->getDescription());
494+
}
471495
}

0 commit comments

Comments
 (0)