Skip to content

Allow omitting method return type #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/DocBlock/Tags/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public static function create(
(?:[\w\|_\\\\]+)
# array notation
(?:\[\])*
)
)?
)*
)
\s+
)?
# Legacy method name (not captured)
Expand All @@ -125,6 +125,11 @@ public static function create(
list(, $static, $returnType, $methodName, $arguments, $description) = $matches;

$static = $static === 'static';

if ($returnType === '') {
$returnType = 'void';
}

$returnType = $typeResolver->resolve($returnType, $context);
$description = $descriptionFactory->create($description, $context);

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

return ($this->isStatic() ? 'static ' : '')
return trim(($this->isStatic() ? 'static ' : '')
. (string)$this->returnType . ' '
. $this->methodName
. '(' . implode(', ', $arguments) . ')'
. ($this->description ? ' ' . $this->description->render() : '');
. ($this->description ? ' ' . $this->description->render() : ''));
}

private function filterArguments($arguments)
Expand Down
77 changes: 76 additions & 1 deletion tests/unit/DocBlock/Tags/MethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Mockery as m;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Compound;
Expand Down Expand Up @@ -305,7 +306,7 @@ public function testReturnTypeThis()
);

$this->assertTrue($fixture->isStatic());
$this->assertSame('static $this myMethod() ', (string)$fixture);
$this->assertSame('static $this myMethod()', (string)$fixture);
$this->assertSame('myMethod', $fixture->getMethodName());
$this->assertInstanceOf(This::class, $fixture->getReturnType());
}
Expand Down Expand Up @@ -468,4 +469,78 @@ public function testCreateMethodParenthesisMissing()
$this->assertInstanceOf(Void_::class, $fixture->getReturnType());
$this->assertSame($description, $fixture->getDescription());
}

/**
* @covers ::create
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Method::<public>
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
* @uses \phpDocumentor\Reflection\TypeResolver
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\Fqsen
* @uses \phpDocumentor\Reflection\Types\Context
* @uses \phpDocumentor\Reflection\Types\Void_
*/
public function testCreateWithoutReturnType()
{
$descriptionFactory = m::mock(DescriptionFactory::class);
$resolver = new TypeResolver();
$context = new Context('');

$description = new Description('');

$descriptionFactory->shouldReceive('create')->with('', $context)->andReturn($description);

$fixture = Method::create(
'myMethod()',
$resolver,
$descriptionFactory,
$context
);

$this->assertSame('void myMethod()', (string)$fixture);
$this->assertSame('myMethod', $fixture->getMethodName());
$this->assertEquals([], $fixture->getArguments());
$this->assertInstanceOf(Void_::class, $fixture->getReturnType());
$this->assertSame($description, $fixture->getDescription());
}

/**
* @covers ::create
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Method::<public>
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
* @uses \phpDocumentor\Reflection\TypeResolver
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\Fqsen
* @uses \phpDocumentor\Reflection\Types\Context
* @uses \phpDocumentor\Reflection\Types\Array_
* @uses \phpDocumentor\Reflection\Types\Compound
* @uses \phpDocumentor\Reflection\Types\Integer
* @uses \phpDocumentor\Reflection\Types\Object_
*/
public function testCreateWithMixedReturnTypes()
{
$descriptionFactory = m::mock(DescriptionFactory::class);
$resolver = new TypeResolver();
$context = new Context('');

$descriptionFactory->shouldReceive('create')->andReturn(new Description(''));

$fixture = Method::create(
'MyClass[]|int[] myMethod()',
$resolver,
$descriptionFactory,
$context
);

$this->assertSame('\MyClass[]|int[] myMethod()', (string)$fixture);
$this->assertSame('myMethod', $fixture->getMethodName());
$this->assertEquals([], $fixture->getArguments());

$this->assertEquals(
new Compound([
new Array_(new Object_(new Fqsen('\MyClass'))),
new Array_(new Integer()),
])
, $fixture->getReturnType());
}
}