Skip to content

Commit a17a8eb

Browse files
committed
Configurable macro return type defaults
1 parent cdefaee commit a17a8eb

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

config/ide-helper.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,20 @@
274274
*/
275275
'use_generics_annotations' => true,
276276

277+
/*
278+
|--------------------------------------------------------------------------
279+
| Default return types for macros
280+
|--------------------------------------------------------------------------
281+
|
282+
| Define default return types for macros without explicit return types.
283+
| e.g. `\Illuminate\Database\Query\Builder::class => 'static'`,
284+
| `\Illuminate\Support\Str::class => 'string'`
285+
|
286+
*/
287+
'macro_default_return_types' => [
288+
\Illuminate\Http\Client\Factory::class => \Illuminate\Http\Client\PendingRequest::class,
289+
],
290+
277291
/*
278292
|--------------------------------------------------------------------------
279293
| Additional relation types

src/Generator.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ public function __construct(
5757
// Find the drivers to add to the extra/interfaces
5858
$this->detectDrivers();
5959

60-
$this->extra = array_merge($this->extra, $this->config->get('ide-helper.extra'), []);
61-
$this->magic = array_merge($this->magic, $this->config->get('ide-helper.magic'), []);
62-
$this->interfaces = array_merge($this->interfaces, $this->config->get('ide-helper.interfaces'), []);
60+
$this->extra = array_merge($this->extra, $this->config->get('ide-helper.extra', []));
61+
$this->magic = array_merge($this->magic, $this->config->get('ide-helper.magic', []));
62+
$this->interfaces = array_merge($this->interfaces, $this->config->get('ide-helper.interfaces', []));
63+
Macro::setDefaultReturnTypes($this->config->get('ide-helper.macro_default_return_types', [
64+
\Illuminate\Http\Client\Factory::class => \Illuminate\Http\Client\PendingRequest::class,
65+
]));
66+
6367
// Make all interface classes absolute
6468
foreach ($this->interfaces as &$interface) {
6569
$interface = '\\' . ltrim($interface, '\\');

src/Macro.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
use Barryvdh\Reflection\DocBlock;
66
use Barryvdh\Reflection\DocBlock\Tag;
77
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
8-
use Illuminate\Http\Client\PendingRequest;
98
use Illuminate\Support\Collection;
109

1110
class Macro extends Method
1211
{
13-
protected $macroDefaults = [
14-
\Illuminate\Http\Client\Factory::class => PendingRequest::class,
15-
];
12+
protected static $macroDefaults = [];
1613

1714
/**
1815
* Macro constructor.
@@ -37,6 +34,10 @@ public function __construct(
3734
parent::__construct($method, $alias, $class, $methodName, $interfaces, $classAliases, $returnTypeNormalizers);
3835
}
3936

37+
public static function setDefaultReturnTypes(array $map = [])
38+
{
39+
static::$macroDefaults = array_merge(static::$macroDefaults, $map);
40+
}
4041
/**
4142
* @param \ReflectionFunctionAbstract $method
4243
*/
@@ -84,8 +85,8 @@ protected function initPhpDoc($method)
8485
}
8586

8687
$class = ltrim($this->declaringClassName, '\\');
87-
if (!$this->phpdoc->hasTag('return') && isset($this->macroDefaults[$class])) {
88-
$type = $this->macroDefaults[$class];
88+
if (!$this->phpdoc->hasTag('return') && isset(static::$macroDefaults[$class])) {
89+
$type = static::$macroDefaults[$class];
8990
$this->phpdoc->appendTag(Tag::createInstance("@return {$type}"));
9091
}
9192
}

tests/Console/GeneratorCommand/GenerateIdeHelper/Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function testGenerator(): void
1717
});
1818
DB::macro('db_custom_macro', function () {
1919
});
20+
$this->app['config']->set('ide-helper.macro_default_return_types', [Arr::class => 'Custom_Fake_Class']);
2021

2122
$command = $this->app->make(GeneratorCommand::class);
2223

@@ -26,6 +27,7 @@ public function testGenerator(): void
2627

2728
$this->assertStringContainsString('A new helper file was written to _ide_helper.php', $tester->getDisplay());
2829
$this->assertStringContainsString('public static function configure($basePath = null)', $this->mockFilesystemOutput);
30+
$this->assertStringContainsString('* @return \Custom_Fake_Class', $this->mockFilesystemOutput);
2931
$this->assertStringContainsString('public static function arr_custom_macro()', $this->mockFilesystemOutput);
3032
$this->assertStringContainsString('public static function db_custom_macro()', $this->mockFilesystemOutput);
3133
}

0 commit comments

Comments
 (0)