From 4b4cacf0413709bc7292e109522b148bd5fbca86 Mon Sep 17 00:00:00 2001 From: Daniel de Wit Date: Fri, 30 Apr 2021 12:59:12 +0200 Subject: [PATCH 1/5] Check for traits recursively --- src/Console/ModelsCommand.php | 4 ++-- src/Generator.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 35c1419d8..41e0bfbbb 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -1082,7 +1082,7 @@ protected function getReturnTypeFromReflection(\ReflectionMethod $reflection): ? */ protected function getSoftDeleteMethods($model) { - $traits = class_uses(get_class($model), true); + $traits = class_uses_recursive(get_class($model), true); if (in_array('Illuminate\\Database\\Eloquent\\SoftDeletes', $traits)) { $modelName = $this->getClassNameInDestinationFile($model, get_class($model)); $builder = $this->getClassNameInDestinationFile($model, \Illuminate\Database\Query\Builder::class); @@ -1105,7 +1105,7 @@ protected function getFactoryMethods($model) $modelName = get_class($model); - $traits = class_uses($modelName, true); + $traits = class_uses_recursive($modelName, true); if (!in_array('Illuminate\\Database\\Eloquent\\Factories\\HasFactory', $traits)) { return; } diff --git a/src/Generator.php b/src/Generator.php index a5822006a..9f0438fde 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -296,7 +296,7 @@ protected function getMacroableClasses(Collection $aliases) return !$reflection->isInternal() && $reflection->getName() === $class; }) ->filter(function ($class) { - $traits = class_uses($class); + $traits = class_uses_recursive($class); // Filter only classes with the macroable trait return isset($traits[Macroable::class]); From ea84d82356f1ab0bb5f30f5c84b10dd5276c2a65 Mon Sep 17 00:00:00 2001 From: Daniel de Wit Date: Fri, 30 Apr 2021 18:18:02 +0200 Subject: [PATCH 2/5] Remove autoload bool from class_uses_recursive --- src/Console/ModelsCommand.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 41e0bfbbb..a9cbc0c2c 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -1082,7 +1082,7 @@ protected function getReturnTypeFromReflection(\ReflectionMethod $reflection): ? */ protected function getSoftDeleteMethods($model) { - $traits = class_uses_recursive(get_class($model), true); + $traits = class_uses_recursive(get_class($model)); if (in_array('Illuminate\\Database\\Eloquent\\SoftDeletes', $traits)) { $modelName = $this->getClassNameInDestinationFile($model, get_class($model)); $builder = $this->getClassNameInDestinationFile($model, \Illuminate\Database\Query\Builder::class); @@ -1105,7 +1105,8 @@ protected function getFactoryMethods($model) $modelName = get_class($model); - $traits = class_uses_recursive($modelName, true); + + $traits = class_uses_recursive($modelName); if (!in_array('Illuminate\\Database\\Eloquent\\Factories\\HasFactory', $traits)) { return; } From 3f09f62feb7408a79f127dfb470b533df6a167f7 Mon Sep 17 00:00:00 2001 From: Daniel de Wit Date: Fri, 30 Apr 2021 18:24:25 +0200 Subject: [PATCH 3/5] Add test --- .../ModelWithNestedFactoryFactory.php | 30 +++++++++++++++++++ .../Models/ModelWithNestedFactory.php | 9 ++++++ .../Factories/__snapshots__/Test__test__1.php | 18 +++++++++++ 3 files changed, 57 insertions(+) create mode 100644 tests/Console/ModelsCommand/Factories/Factories/ModelWithNestedFactoryFactory.php create mode 100644 tests/Console/ModelsCommand/Factories/Models/ModelWithNestedFactory.php diff --git a/tests/Console/ModelsCommand/Factories/Factories/ModelWithNestedFactoryFactory.php b/tests/Console/ModelsCommand/Factories/Factories/ModelWithNestedFactoryFactory.php new file mode 100644 index 000000000..1362462b1 --- /dev/null +++ b/tests/Console/ModelsCommand/Factories/Factories/ModelWithNestedFactoryFactory.php @@ -0,0 +1,30 @@ + Date: Fri, 30 Apr 2021 20:36:10 +0200 Subject: [PATCH 4/5] Update src/Console/ModelsCommand.php Co-authored-by: Markus Podar --- src/Console/ModelsCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index a9cbc0c2c..12fe8b941 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -1082,7 +1082,7 @@ protected function getReturnTypeFromReflection(\ReflectionMethod $reflection): ? */ protected function getSoftDeleteMethods($model) { - $traits = class_uses_recursive(get_class($model)); + $traits = class_uses_recursive($model); if (in_array('Illuminate\\Database\\Eloquent\\SoftDeletes', $traits)) { $modelName = $this->getClassNameInDestinationFile($model, get_class($model)); $builder = $this->getClassNameInDestinationFile($model, \Illuminate\Database\Query\Builder::class); From 5c1bf5cca1e63733f374f9020ce5f2632c389f9b Mon Sep 17 00:00:00 2001 From: Daniel de Wit Date: Fri, 30 Apr 2021 20:43:49 +0200 Subject: [PATCH 5/5] Updated changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98ffdc4cb..6c753169e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. [Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.10.0...master) -------------- +### Fixed +- Fix recursively searching for `HasFactory` and `Macroable` traits [\#1216 / daniel-de-wit](https://github.com/barryvdh/laravel-ide-helper/pull/1216) 2021-04-09, 2.10.0 ------------------