Skip to content

Commit c775aec

Browse files
authored
Revert #1629 - _Allow adding custom Macroable classes_ (#1707)
* Revert #1629 - Allow adding custom Macroable classes * Add Macros test
1 parent 0fa96e5 commit c775aec

File tree

6 files changed

+41
-36
lines changed

6 files changed

+41
-36
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ Str::macro('concat', function(string $str1, string $str2) : string {
115115
});
116116
```
117117

118-
You can add any custom Macroable traits to detect in the `macroable_traits` config option.
119-
120118
### Automatic PHPDocs for models
121119

122120
If you don't want to write your properties yourself, you can use the command `php artisan ide-helper:models` to generate

config/ide-helper.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,4 @@
335335
// 'ide-helper:models --nowrite',
336336
],
337337

338-
/*
339-
|--------------------------------------------------------------------------
340-
| Macroable Traits
341-
|--------------------------------------------------------------------------
342-
|
343-
| Define which traits should be considered capable of adding Macro.
344-
| You can add any custom trait that behaves like the original Laravel one.
345-
|
346-
*/
347-
'macroable_traits' => [
348-
Filament\Support\Concerns\Macroable::class,
349-
Spatie\Macroable\Macroable::class,
350-
],
351-
352338
];

src/Alias.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
2323
use Illuminate\Database\Query\Builder as QueryBuilder;
2424
use Illuminate\Support\Facades\Facade;
25+
use Illuminate\Support\Traits\Macroable;
2526
use ReflectionClass;
2627
use Throwable;
2728

@@ -47,8 +48,6 @@ class Alias
4748
protected $phpdoc = null;
4849
protected $classAliases = [];
4950

50-
protected $isMacroable = false;
51-
5251
/** @var ConfigRepository */
5352
protected $config;
5453

@@ -63,13 +62,12 @@ class Alias
6362
* @param array $magicMethods
6463
* @param array $interfaces
6564
*/
66-
public function __construct($config, $alias, $facade, $magicMethods = [], $interfaces = [], $isMacroable = false)
65+
public function __construct($config, $alias, $facade, $magicMethods = [], $interfaces = [])
6766
{
6867
$this->alias = $alias;
6968
$this->magicMethods = $magicMethods;
7069
$this->interfaces = $interfaces;
7170
$this->config = $config;
72-
$this->isMacroable = $isMacroable;
7371

7472
// Make the class absolute
7573
$facade = '\\' . ltrim($facade, '\\');
@@ -431,7 +429,7 @@ protected function detectMethods()
431429

432430
// Check if the class is macroable
433431
// (Eloquent\Builder is also macroable but doesn't use Macroable trait)
434-
if ($this->isMacroable || $class === EloquentBuilder::class) {
432+
if ($class === EloquentBuilder::class || in_array(Macroable::class, $reflection->getTraitNames())) {
435433
$properties = $reflection->getStaticProperties();
436434
$macros = isset($properties['macros']) ? $properties['macros'] : [];
437435
foreach ($macros as $macro_name => $macro_func) {

src/Generator.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class Generator
3535
protected $magic = [];
3636
protected $interfaces = [];
3737
protected $helpers;
38-
protected array $macroableTraits = [];
3938

4039
/**
4140
* @param \Illuminate\Config\Repository $config
@@ -360,7 +359,7 @@ protected function addMacroableClasses(Collection $aliases)
360359
continue;
361360
}
362361

363-
$aliases[] = new Alias($this->config, $class, $class, [], $this->interfaces, true);
362+
$aliases[] = new Alias($this->config, $class, $class, [], $this->interfaces);
364363
}
365364
}
366365

@@ -382,18 +381,8 @@ protected function getMacroableClasses(Collection $aliases)
382381
->filter(function ($class) {
383382
$traits = class_uses_recursive($class);
384383

385-
if (isset($traits[Macroable::class])) {
386-
return true;
387-
}
388-
389-
// Filter only classes with a macroable trait
390-
foreach ($this->config->get('ide-helper.macroable_traits', []) as $trait) {
391-
if (isset($traits[$trait])) {
392-
return true;
393-
}
394-
}
395-
396-
return false;
384+
// Filter only classes with the macroable trait
385+
return isset($traits[Macroable::class]);
397386
})
398387
->filter(function ($class) use ($aliases) {
399388
$class = Str::start($class, '\\');

tests/AliasTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Barryvdh\LaravelIdeHelper\Alias;
88
use Barryvdh\LaravelIdeHelper\Macro;
99
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
10+
use Illuminate\Database\Query\Builder;
1011
use Illuminate\Support\Arr;
1112

1213
/**
@@ -15,6 +16,31 @@
1516
*/
1617
class AliasTest extends TestCase
1718
{
19+
/**
20+
* @covers ::detectMethods
21+
*/
22+
public function testDetectMethodsMacroableMacros(): void
23+
{
24+
// Mock
25+
$macro = __FUNCTION__;
26+
$alias = new AliasMock();
27+
28+
// Macros
29+
Builder::macro(
30+
$macro,
31+
function () {
32+
// empty
33+
}
34+
);
35+
36+
// Prepare
37+
$alias->setClasses([Builder::class]);
38+
$alias->detectMethods();
39+
40+
// Test
41+
$this->assertNotNull($this->getAliasMacro($alias, Builder::class, $macro));
42+
}
43+
1844
/**
1945
* @covers ::detectMethods
2046
*/
@@ -24,7 +50,7 @@ public function testDetectMethodsEloquentBuilderMacros(): void
2450
$macro = __FUNCTION__;
2551
$alias = new AliasMock();
2652

27-
// Macrosx
53+
// Macros
2854
EloquentBuilder::macro(
2955
$macro,
3056
function () {

tests/Console/GeneratorCommand/GenerateIdeHelper/Test.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,26 @@
66

77
use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand;
88
use Barryvdh\LaravelIdeHelper\Tests\Console\GeneratorCommand\AbstractGeneratorCommand;
9+
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Facades\DB;
911

1012
class Test extends AbstractGeneratorCommand
1113
{
1214
public function testGenerator(): void
1315
{
16+
Arr::macro('arr_custom_macro',function(){});
17+
DB::macro('db_custom_macro',function(){});
18+
1419
$command = $this->app->make(GeneratorCommand::class);
1520

1621
$tester = $this->runCommand($command);
1722

1823
$this->assertSame(0, $tester->getStatusCode());
24+
1925
$this->assertStringContainsString('A new helper file was written to _ide_helper.php', $tester->getDisplay());
2026
$this->assertStringContainsString('public static function configure($basePath = null)', $this->mockFilesystemOutput);
27+
$this->assertStringContainsString('public static function arr_custom_macro()', $this->mockFilesystemOutput);
28+
$this->assertStringContainsString('public static function db_custom_macro()', $this->mockFilesystemOutput);
2129
}
2230

2331
public function testFilename(): void

0 commit comments

Comments
 (0)