Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for traits recursively #1216

Merged
merged 5 commits into from
Jun 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------
Expand Down
5 changes: 3 additions & 2 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ protected function getReturnTypeFromReflection(\ReflectionMethod $reflection): ?
*/
protected function getSoftDeleteMethods($model)
{
$traits = class_uses(get_class($model), true);
$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);
Expand All @@ -1105,7 +1105,8 @@ protected function getFactoryMethods($model)

$modelName = get_class($model);

$traits = class_uses($modelName, true);

$traits = class_uses_recursive($modelName);
if (!in_array('Illuminate\\Database\\Eloquent\\Factories\\HasFactory', $traits)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Factories;

use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithNestedFactory;
use Illuminate\Database\Eloquent\Factories\Factory;

class ModelWithNestedFactoryFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = ModelWithNestedFactory::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
//
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

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

class ModelWithNestedFactory extends ModelWithFactory
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ class ModelWithFactory extends Model

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

/**
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithNestedFactory
*
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Factories\ModelWithNestedFactoryFactory factory(...$parameters)
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithNestedFactory newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithNestedFactory newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithNestedFactory query()
* @mixin \Eloquent
*/
class ModelWithNestedFactory extends ModelWithFactory
{
}
<?php

declare(strict_types=1);

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

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

Expand Down