From e0b2f8632683aa37d6b7913184042204f3318e2f Mon Sep 17 00:00:00 2001 From: erikn69 Date: Thu, 15 May 2025 02:56:31 -0500 Subject: [PATCH] Configurable macro return type defaults (#1711) --- config/ide-helper.php | 14 ++++++++++++++ src/Generator.php | 10 +++++++--- src/Macro.php | 13 +++++++------ .../GeneratorCommand/GenerateIdeHelper/Test.php | 2 ++ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/config/ide-helper.php b/config/ide-helper.php index 40c5835..3017f87 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -274,6 +274,20 @@ return [ */ 'use_generics_annotations' => true, + /* + |-------------------------------------------------------------------------- + | Default return types for macros + |-------------------------------------------------------------------------- + | + | Define default return types for macros without explicit return types. + | e.g. `\Illuminate\Database\Query\Builder::class => 'static'`, + | `\Illuminate\Support\Str::class => 'string'` + | + */ + 'macro_default_return_types' => [ + \Illuminate\Http\Client\Factory::class => \Illuminate\Http\Client\PendingRequest::class, + ], + /* |-------------------------------------------------------------------------- | Additional relation types diff --git a/src/Generator.php b/src/Generator.php index 480047c..493b164 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -57,9 +57,13 @@ class Generator // Find the drivers to add to the extra/interfaces $this->detectDrivers(); - $this->extra = array_merge($this->extra, $this->config->get('ide-helper.extra'), []); - $this->magic = array_merge($this->magic, $this->config->get('ide-helper.magic'), []); - $this->interfaces = array_merge($this->interfaces, $this->config->get('ide-helper.interfaces'), []); + $this->extra = array_merge($this->extra, $this->config->get('ide-helper.extra', [])); + $this->magic = array_merge($this->magic, $this->config->get('ide-helper.magic', [])); + $this->interfaces = array_merge($this->interfaces, $this->config->get('ide-helper.interfaces', [])); + Macro::setDefaultReturnTypes($this->config->get('ide-helper.macro_default_return_types', [ + \Illuminate\Http\Client\Factory::class => \Illuminate\Http\Client\PendingRequest::class, + ])); + // Make all interface classes absolute foreach ($this->interfaces as &$interface) { $interface = '\\' . ltrim($interface, '\\'); diff --git a/src/Macro.php b/src/Macro.php index 42e409d..6e3fd45 100644 --- a/src/Macro.php +++ b/src/Macro.php @@ -5,14 +5,11 @@ namespace Barryvdh\LaravelIdeHelper; use Barryvdh\Reflection\DocBlock; use Barryvdh\Reflection\DocBlock\Tag; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Http\Client\PendingRequest; use Illuminate\Support\Collection; class Macro extends Method { - protected $macroDefaults = [ - \Illuminate\Http\Client\Factory::class => PendingRequest::class, - ]; + protected static $macroDefaults = []; /** * Macro constructor. @@ -37,6 +34,10 @@ class Macro extends Method parent::__construct($method, $alias, $class, $methodName, $interfaces, $classAliases, $returnTypeNormalizers); } + public static function setDefaultReturnTypes(array $map = []) + { + static::$macroDefaults = array_merge(static::$macroDefaults, $map); + } /** * @param \ReflectionFunctionAbstract $method */ @@ -84,8 +85,8 @@ class Macro extends Method } $class = ltrim($this->declaringClassName, '\\'); - if (!$this->phpdoc->hasTag('return') && isset($this->macroDefaults[$class])) { - $type = $this->macroDefaults[$class]; + if (!$this->phpdoc->hasTag('return') && isset(static::$macroDefaults[$class])) { + $type = static::$macroDefaults[$class]; $this->phpdoc->appendTag(Tag::createInstance("@return {$type}")); } } diff --git a/tests/Console/GeneratorCommand/GenerateIdeHelper/Test.php b/tests/Console/GeneratorCommand/GenerateIdeHelper/Test.php index 950cf90..fed9e0a 100644 --- a/tests/Console/GeneratorCommand/GenerateIdeHelper/Test.php +++ b/tests/Console/GeneratorCommand/GenerateIdeHelper/Test.php @@ -17,6 +17,7 @@ class Test extends AbstractGeneratorCommand }); DB::macro('db_custom_macro', function () { }); + $this->app['config']->set('ide-helper.macro_default_return_types', [Arr::class => 'Custom_Fake_Class']); $command = $this->app->make(GeneratorCommand::class); @@ -26,6 +27,7 @@ class Test extends AbstractGeneratorCommand $this->assertStringContainsString('A new helper file was written to _ide_helper.php', $tester->getDisplay()); $this->assertStringContainsString('public static function configure($basePath = null)', $this->mockFilesystemOutput); + $this->assertStringContainsString('* @return \Custom_Fake_Class', $this->mockFilesystemOutput); $this->assertStringContainsString('public static function arr_custom_macro()', $this->mockFilesystemOutput); $this->assertStringContainsString('public static function db_custom_macro()', $this->mockFilesystemOutput); }