Skip to content

Commit 13309dc

Browse files
committed
Fix macros issue
1 parent 0fa96e5 commit 13309dc

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed

src/Alias.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ class Alias
4747
protected $phpdoc = null;
4848
protected $classAliases = [];
4949

50-
protected $isMacroable = false;
51-
5250
/** @var ConfigRepository */
5351
protected $config;
5452

@@ -63,13 +61,12 @@ class Alias
6361
* @param array $magicMethods
6462
* @param array $interfaces
6563
*/
66-
public function __construct($config, $alias, $facade, $magicMethods = [], $interfaces = [], $isMacroable = false)
64+
public function __construct($config, $alias, $facade, $magicMethods = [], $interfaces = [])
6765
{
6866
$this->alias = $alias;
6967
$this->magicMethods = $magicMethods;
7068
$this->interfaces = $interfaces;
7169
$this->config = $config;
72-
$this->isMacroable = $isMacroable;
7370

7471
// Make the class absolute
7572
$facade = '\\' . ltrim($facade, '\\');
@@ -431,7 +428,7 @@ protected function detectMethods()
431428

432429
// Check if the class is macroable
433430
// (Eloquent\Builder is also macroable but doesn't use Macroable trait)
434-
if ($this->isMacroable || $class === EloquentBuilder::class) {
431+
if ($class === EloquentBuilder::class || Generator::hasMacroableTrait(array_flip($reflection->getTraitNames()))) {
435432
$properties = $reflection->getStaticProperties();
436433
$macros = isset($properties['macros']) ? $properties['macros'] : [];
437434
foreach ($macros as $macro_name => $macro_func) {

src/Generator.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Illuminate\Foundation\AliasLoader;
1616
use Illuminate\Support\Collection;
1717
use Illuminate\Support\Facades\Facade;
18+
use Illuminate\Support\Facades\Config;
1819
use Illuminate\Support\Str;
1920
use Illuminate\Support\Traits\Macroable;
2021
use ReflectionClass;
@@ -35,7 +36,7 @@ class Generator
3536
protected $magic = [];
3637
protected $interfaces = [];
3738
protected $helpers;
38-
protected array $macroableTraits = [];
39+
protected static ?array $_macroableTraits = null;
3940

4041
/**
4142
* @param \Illuminate\Config\Repository $config
@@ -58,9 +59,9 @@ public function __construct(
5859
// Find the drivers to add to the extra/interfaces
5960
$this->detectDrivers();
6061

61-
$this->extra = array_merge($this->extra, $this->config->get('ide-helper.extra'), []);
62-
$this->magic = array_merge($this->magic, $this->config->get('ide-helper.magic'), []);
63-
$this->interfaces = array_merge($this->interfaces, $this->config->get('ide-helper.interfaces'), []);
62+
$this->extra = array_merge($this->extra, $this->config->get('ide-helper.extra', []));
63+
$this->magic = array_merge($this->magic, $this->config->get('ide-helper.magic', []));
64+
$this->interfaces = array_merge($this->interfaces, $this->config->get('ide-helper.interfaces', []));
6465
// Make all interface classes absolute
6566
foreach ($this->interfaces as &$interface) {
6667
$interface = '\\' . ltrim($interface, '\\');
@@ -360,7 +361,7 @@ protected function addMacroableClasses(Collection $aliases)
360361
continue;
361362
}
362363

363-
$aliases[] = new Alias($this->config, $class, $class, [], $this->interfaces, true);
364+
$aliases[] = new Alias($this->config, $class, $class, [], $this->interfaces);
364365
}
365366
}
366367

@@ -379,22 +380,7 @@ protected function getMacroableClasses(Collection $aliases)
379380
// Filter out internal classes and class aliases
380381
return !$reflection->isInternal() && $reflection->getName() === $class;
381382
})
382-
->filter(function ($class) {
383-
$traits = class_uses_recursive($class);
384-
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;
397-
})
383+
->filter(fn ($class) => static::hasMacroableTrait(class_uses_recursive($class)))
398384
->filter(function ($class) use ($aliases) {
399385
$class = Str::start($class, '\\');
400386

@@ -404,4 +390,19 @@ protected function getMacroableClasses(Collection $aliases)
404390
});
405391
});
406392
}
393+
394+
public static function hasMacroableTrait($traits)
395+
{
396+
if (!static::$_macroableTraits) {
397+
static::$_macroableTraits = array_merge([Macroable::class], Config::get('ide-helper.macroable_traits', []));;
398+
}
399+
400+
foreach (static::$_macroableTraits as $trait) {
401+
if (isset($traits[$trait])) {
402+
return true;
403+
}
404+
}
405+
406+
return false;
407+
}
407408
}

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 () {

0 commit comments

Comments
 (0)