Skip to content

Commit e71f3d5

Browse files
daniel-de-witmfn
andauthored
Check for traits recursively (#1216)
* Check for traits recursively * Remove autoload bool from class_uses_recursive * Add test * Update src/Console/ModelsCommand.php Co-authored-by: Markus Podar <markus@fischer.name> * Updated changelog Co-authored-by: Markus Podar <markus@fischer.name>
1 parent df67074 commit e71f3d5

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
@@ -1082,7 +1082,7 @@ protected function getReturnTypeFromReflection(\ReflectionMethod $reflection): ?
10821082
*/
10831083
protected function getSoftDeleteMethods($model)
10841084
{
1085-
$traits = class_uses(get_class($model), true);
1085+
$traits = class_uses_recursive($model);
10861086
if (in_array('Illuminate\\Database\\Eloquent\\SoftDeletes', $traits)) {
10871087
$modelName = $this->getClassNameInDestinationFile($model, get_class($model));
10881088
$builder = $this->getClassNameInDestinationFile($model, \Illuminate\Database\Query\Builder::class);
@@ -1105,7 +1105,8 @@ protected function getFactoryMethods($model)
11051105

11061106
$modelName = get_class($model);
11071107

1108-
$traits = class_uses($modelName, true);
1108+
1109+
$traits = class_uses_recursive($modelName);
11091110
if (!in_array('Illuminate\\Database\\Eloquent\\Factories\\HasFactory', $traits)) {
11101111
return;
11111112
}

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)