diff --git a/README.md b/README.md index fc6efaf..22b8e05 100644 --- a/README.md +++ b/README.md @@ -115,8 +115,6 @@ Str::macro('concat', function(string $str1, string $str2) : string { }); ``` -You can add any custom Macroable traits to detect in the `macroable_traits` config option. - ### Automatic PHPDocs for models If you don't want to write your properties yourself, you can use the command `php artisan ide-helper:models` to generate diff --git a/config/ide-helper.php b/config/ide-helper.php index 1500783..40c5835 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -335,18 +335,4 @@ return [ // 'ide-helper:models --nowrite', ], - /* - |-------------------------------------------------------------------------- - | Macroable Traits - |-------------------------------------------------------------------------- - | - | Define which traits should be considered capable of adding Macro. - | You can add any custom trait that behaves like the original Laravel one. - | - */ - 'macroable_traits' => [ - Filament\Support\Concerns\Macroable::class, - Spatie\Macroable\Macroable::class, - ], - ]; diff --git a/src/Alias.php b/src/Alias.php index dae3bd0..245edb4 100644 --- a/src/Alias.php +++ b/src/Alias.php @@ -22,6 +22,7 @@ use Illuminate\Config\Repository as ConfigRepository; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Support\Facades\Facade; +use Illuminate\Support\Traits\Macroable; use ReflectionClass; use Throwable; @@ -47,8 +48,6 @@ class Alias protected $phpdoc = null; protected $classAliases = []; - protected $isMacroable = false; - /** @var ConfigRepository */ protected $config; @@ -63,13 +62,12 @@ class Alias * @param array $magicMethods * @param array $interfaces */ - public function __construct($config, $alias, $facade, $magicMethods = [], $interfaces = [], $isMacroable = false) + public function __construct($config, $alias, $facade, $magicMethods = [], $interfaces = []) { $this->alias = $alias; $this->magicMethods = $magicMethods; $this->interfaces = $interfaces; $this->config = $config; - $this->isMacroable = $isMacroable; // Make the class absolute $facade = '\\' . ltrim($facade, '\\'); @@ -431,7 +429,7 @@ class Alias // Check if the class is macroable // (Eloquent\Builder is also macroable but doesn't use Macroable trait) - if ($this->isMacroable || $class === EloquentBuilder::class) { + if ($class === EloquentBuilder::class || in_array(Macroable::class, $reflection->getTraitNames())) { $properties = $reflection->getStaticProperties(); $macros = isset($properties['macros']) ? $properties['macros'] : []; foreach ($macros as $macro_name => $macro_func) { diff --git a/src/Generator.php b/src/Generator.php index dc312f2..480047c 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -35,7 +35,6 @@ class Generator protected $magic = []; protected $interfaces = []; protected $helpers; - protected array $macroableTraits = []; /** * @param \Illuminate\Config\Repository $config @@ -360,7 +359,7 @@ class Generator continue; } - $aliases[] = new Alias($this->config, $class, $class, [], $this->interfaces, true); + $aliases[] = new Alias($this->config, $class, $class, [], $this->interfaces); } } @@ -382,18 +381,8 @@ class Generator ->filter(function ($class) { $traits = class_uses_recursive($class); - if (isset($traits[Macroable::class])) { - return true; - } - - // Filter only classes with a macroable trait - foreach ($this->config->get('ide-helper.macroable_traits', []) as $trait) { - if (isset($traits[$trait])) { - return true; - } - } - - return false; + // Filter only classes with the macroable trait + return isset($traits[Macroable::class]); }) ->filter(function ($class) use ($aliases) { $class = Str::start($class, '\\'); diff --git a/tests/AliasTest.php b/tests/AliasTest.php index a5f2b32..e0aa433 100644 --- a/tests/AliasTest.php +++ b/tests/AliasTest.php @@ -7,6 +7,7 @@ namespace Barryvdh\LaravelIdeHelper\Tests; use Barryvdh\LaravelIdeHelper\Alias; use Barryvdh\LaravelIdeHelper\Macro; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use Illuminate\Database\Query\Builder; use Illuminate\Support\Arr; /** @@ -15,6 +16,31 @@ use Illuminate\Support\Arr; */ class AliasTest extends TestCase { + /** + * @covers ::detectMethods + */ + public function testDetectMethodsMacroableMacros(): void + { + // Mock + $macro = __FUNCTION__; + $alias = new AliasMock(); + + // Macros + Builder::macro( + $macro, + function () { + // empty + } + ); + + // Prepare + $alias->setClasses([Builder::class]); + $alias->detectMethods(); + + // Test + $this->assertNotNull($this->getAliasMacro($alias, Builder::class, $macro)); + } + /** * @covers ::detectMethods */ @@ -24,7 +50,7 @@ class AliasTest extends TestCase $macro = __FUNCTION__; $alias = new AliasMock(); - // Macrosx + // Macros EloquentBuilder::macro( $macro, function () { diff --git a/tests/Console/GeneratorCommand/GenerateIdeHelper/Test.php b/tests/Console/GeneratorCommand/GenerateIdeHelper/Test.php index 6955c82..ceb89bd 100644 --- a/tests/Console/GeneratorCommand/GenerateIdeHelper/Test.php +++ b/tests/Console/GeneratorCommand/GenerateIdeHelper/Test.php @@ -6,18 +6,26 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\GeneratorCommand\GenerateIdeHe use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand; use Barryvdh\LaravelIdeHelper\Tests\Console\GeneratorCommand\AbstractGeneratorCommand; +use Illuminate\Support\Arr; +use Illuminate\Support\Facades\DB; class Test extends AbstractGeneratorCommand { public function testGenerator(): void { + Arr::macro('arr_custom_macro',function(){}); + DB::macro('db_custom_macro',function(){}); + $command = $this->app->make(GeneratorCommand::class); $tester = $this->runCommand($command); $this->assertSame(0, $tester->getStatusCode()); + $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('public static function arr_custom_macro()', $this->mockFilesystemOutput); + $this->assertStringContainsString('public static function db_custom_macro()', $this->mockFilesystemOutput); } public function testFilename(): void