Skip to content

Commit 9c9f139

Browse files
committed
Revert #1629 - Allow adding custom Macroable classes
1 parent 0fa96e5 commit 9c9f139

File tree

5 files changed

+32
-36
lines changed

5 files changed

+32
-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: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@
1515
*/
1616
class AliasTest extends TestCase
1717
{
18+
/**
19+
* @covers ::detectMethods
20+
*/
21+
public function testDetectMethodsMacroableMacros(): void
22+
{
23+
// Mock
24+
$macro = __FUNCTION__;
25+
$alias = new AliasMock();
26+
27+
// Macros
28+
Builder::macro(
29+
$macro,
30+
function () {
31+
// empty
32+
}
33+
);
34+
35+
// Prepare
36+
$alias->setClasses([Builder::class]);
37+
$alias->detectMethods();
38+
39+
// Test
40+
$this->assertNotNull($this->getAliasMacro($alias, Builder::class, $macro));
41+
}
42+
1843
/**
1944
* @covers ::detectMethods
2045
*/
@@ -24,7 +49,7 @@ public function testDetectMethodsEloquentBuilderMacros(): void
2449
$macro = __FUNCTION__;
2550
$alias = new AliasMock();
2651

27-
// Macrosx
52+
// Macros
2853
EloquentBuilder::macro(
2954
$macro,
3055
function () {

0 commit comments

Comments
 (0)