Skip to content

Commit 8e397b9

Browse files
daniel-de-witmfn
authored andcommitted
Check for traits recursively (barryvdh#1216)
* Check for traits recursively * Remove autoload bool from class_uses_recursive * Add test * Update src/Console/ModelsCommand.php Co-authored-by: Markus Podar <[email protected]> * Updated changelog Co-authored-by: Markus Podar <[email protected]>
1 parent 84da601 commit 8e397b9

File tree

6 files changed

+63
-3
lines changed

6 files changed

+63
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
44

55
[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.10.0...master)
66
--------------
7+
### Fixed
8+
- Fix recursively searching for `HasFactory` and `Macroable` traits [\#1216 / daniel-de-wit](https://github.com/barryvdh/laravel-ide-helper/pull/1216)
79

810
2021-04-09, 2.10.0
911
------------------

src/Console/ModelsCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ protected function getReturnTypeFromReflection(\ReflectionMethod $reflection): ?
10931093
*/
10941094
protected function getSoftDeleteMethods($model)
10951095
{
1096-
$traits = class_uses(get_class($model), true);
1096+
$traits = class_uses_recursive($model);
10971097
if (in_array('Illuminate\\Database\\Eloquent\\SoftDeletes', $traits)) {
10981098
$modelName = $this->getClassNameInDestinationFile($model, get_class($model));
10991099
$builder = $this->getClassNameInDestinationFile($model, \Illuminate\Database\Query\Builder::class);
@@ -1116,7 +1116,8 @@ protected function getFactoryMethods($model)
11161116

11171117
$modelName = get_class($model);
11181118

1119-
$traits = class_uses($modelName, true);
1119+
1120+
$traits = class_uses_recursive($modelName);
11201121
if (!in_array('Illuminate\\Database\\Eloquent\\Factories\\HasFactory', $traits)) {
11211122
return;
11221123
}

src/Generator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ protected function getMacroableClasses(Collection $aliases)
296296
return !$reflection->isInternal() && $reflection->getName() === $class;
297297
})
298298
->filter(function ($class) {
299-
$traits = class_uses($class);
299+
$traits = class_uses_recursive($class);
300300

301301
// Filter only classes with the macroable trait
302302
return isset($traits[Macroable::class]);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Factories;
6+
7+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithNestedFactory;
8+
use Illuminate\Database\Eloquent\Factories\Factory;
9+
10+
class ModelWithNestedFactoryFactory extends Factory
11+
{
12+
/**
13+
* The name of the factory's corresponding model.
14+
*
15+
* @var string
16+
*/
17+
protected $model = ModelWithNestedFactory::class;
18+
19+
/**
20+
* Define the model's default state.
21+
*
22+
* @return array
23+
*/
24+
public function definition()
25+
{
26+
return [
27+
//
28+
];
29+
}
30+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
6+
7+
class ModelWithNestedFactory extends ModelWithFactory
8+
{
9+
}

tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ class ModelWithFactory extends Model
5959

6060
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
6161

62+
/**
63+
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithNestedFactory
64+
*
65+
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Factories\ModelWithNestedFactoryFactory factory(...$parameters)
66+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithNestedFactory newModelQuery()
67+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithNestedFactory newQuery()
68+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithNestedFactory query()
69+
* @mixin \Eloquent
70+
*/
71+
class ModelWithNestedFactory extends ModelWithFactory
72+
{
73+
}
74+
<?php
75+
76+
declare(strict_types=1);
77+
78+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
79+
6280
use Illuminate\Database\Eloquent\Factories\HasFactory;
6381
use Illuminate\Database\Eloquent\Model;
6482

0 commit comments

Comments
 (0)