diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e69bfd..7bd1f98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ All notable changes to this project will be documented in this file. - Created a possibility to add custom relation type [\#987 / efinder2](https://github.com/barryvdh/laravel-ide-helper/pull/987) - Added `@see` with macro/mixin definition location to PhpDoc [\#1054 / riesjart](https://github.com/barryvdh/laravel-ide-helper/pull/1054) - Initial compatibility for PHP8 [\#1106 / mfn](https://github.com/barryvdh/laravel-ide-helper/pull/1106) +- Generate PHPDoc for laravel 8.x factories [\#1074 / ahmed-aliraqi](https://github.com/barryvdh/laravel-ide-helper/pull/1074) ### Changed - Implement DeferrableProvider [\#914 / kon-shou](https://github.com/barryvdh/laravel-ide-helper/pull/914) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 8b15c2b..aa804dc 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -18,6 +18,7 @@ use Barryvdh\Reflection\DocBlock\Tag; use Composer\Autoload\ClassMapGenerator; use Illuminate\Console\Command; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; +use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; @@ -277,6 +278,7 @@ class ModelsCommand extends Command $this->getPropertiesFromMethods($model); $this->getSoftDeleteMethods($model); $this->getCollectionMethods($model); + $this->getFactoryMethods($model); $output .= $this->createPhpDocs($name); $ignore[] = $name; $this->nullableColumns = []; @@ -1043,6 +1045,33 @@ class ModelsCommand extends Command } } + /** + * Generate factory method from "HasFactory" trait. + * + * @param \Illuminate\Database\Eloquent\Model $model + */ + protected function getFactoryMethods($model) + { + if(!class_exists(Factory::class)) { + return; + } + + $traits = class_uses(get_class($model), true); + if (! in_array('Illuminate\\Database\\Eloquent\\Factories\\HasFactory', $traits)) { + return; + } + + $modelName = get_class($model); + $factory = get_class($modelName::factory()); + $factory = '\\' . trim($factory, '\\'); + + if (! class_exists($factory)) { + return; + } + + $this->setMethod('factory', $factory, ['...$parameters']); + } + /** * Generates methods that return collections * @param \Illuminate\Database\Eloquent\Model $model diff --git a/tests/Console/ModelsCommand/Factories/Factories/SimpleFactory.php b/tests/Console/ModelsCommand/Factories/Factories/SimpleFactory.php new file mode 100644 index 0000000..6c28f7f --- /dev/null +++ b/tests/Console/ModelsCommand/Factories/Factories/SimpleFactory.php @@ -0,0 +1,30 @@ +=')) { + $this->markTestSkipped( + 'This test only works in Laravel >= 8.2' + ); + } + + $command = $this->app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, [ + '--write' => true, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay()); + $this->assertMatchesMockedSnapshot(); + } +} diff --git a/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php new file mode 100644 index 0000000..df8bef1 --- /dev/null +++ b/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php @@ -0,0 +1,35 @@ +