Skip to content

Commit 5a168bf

Browse files
authored
Add a return type using phpdoc to fix IDE autocomplete (#292)
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 #285 and #287 for more details.
1 parent 4507d5b commit 5a168bf

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/Concerns/AsObject.php

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
trait AsObject
88
{
9+
/**
10+
* @return static
11+
*/
912
public static function make()
1013
{
1114
return app(static::class);

tests/AsActionTest.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,15 @@ public function asCommand(Command $command): void
8585
expect(AsActionTest::$latestResult)->toBe(42);
8686
});
8787

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

92-
expect($result)->toBeEmpty();
92+
$returnType = $reflectionMethod->getReturnType();
93+
$docComment = $reflectionMethod->getDocComment();
94+
95+
expect($returnType)->toBeEmpty();
96+
expect($docComment)->toContain('@return static');
9397
});
9498

9599
it('runs as a controller', function () {

0 commit comments

Comments
 (0)