Skip to content

Commit

Permalink
Add a return type using phpdoc to fix IDE autocomplete
Browse files Browse the repository at this point in the history
The make function can't define a return type, because the result could be a Decorator, or the actual action.
However, without docblocks or type-hinting, PHPStorm ends up providing incorrect autocomplete information.
The solution is to instead add back a phpdoc comment for this specific use case.

See lorisleiva#285 and lorisleiva#287 for more details.
  • Loading branch information
taylor-wilton-lightwire authored and TaylorWilton committed Sep 9, 2024
1 parent 4507d5b commit 96247db
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/Concerns/AsObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

trait AsObject
{
/**
* @return static
*/
public static function make()
{
return app(static::class);
Expand Down
12 changes: 8 additions & 4 deletions tests/AsActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ public function asCommand(Command $command): void
expect(AsActionTest::$latestResult)->toBe(42);
});

it('returns void when calling make()', function () {
// Make sure that the static function ::make() returns not a type.
$result = (new \ReflectionMethod(AsActionTest::class, 'make'))->getReturnType();
it('uses a comment instead of a return type on make()', function () {
// Make sure that the static function ::make() does not have a return type.
$reflectionMethod = new \ReflectionMethod(AsActionTest::class, 'make');

expect($result)->toBeEmpty();
$returnType = $reflectionMethod->getReturnType();
$docComment = $reflectionMethod->getDocComment();

expect($returnType)->toBeEmpty();
expect($docComment)->toContain('@return static');
});

it('runs as a controller', function () {
Expand Down

0 comments on commit 96247db

Please sign in to comment.