diff --git a/src/Domain/Models/Output/ModelDocBlock.php b/src/Domain/Models/Output/ModelDocBlock.php index bd1b4e6..be35ccf 100644 --- a/src/Domain/Models/Output/ModelDocBlock.php +++ b/src/Domain/Models/Output/ModelDocBlock.php @@ -36,6 +36,7 @@ protected function docblock(): string $this->all(), $this->query(), $this->queryMixin(), + $this->factory(), ' */', ]) ->map(fn (?string $line): string => $this->line($line)) @@ -102,4 +103,21 @@ private function queryMixin(): ?string return " * @mixin {$this->model->queryBuilder->fqcn}"; } + + private function factory(): ?string + { + if (!trait_exists(\Illuminate\Database\Eloquent\Factories\HasFactory::class)) { + return null; + } + + if (!in_array(\Illuminate\Database\Eloquent\Factories\HasFactory::class, class_uses_recursive($this->model->fqcn))) { + return null; + } + + $factory = get_class( + ($this->model->fqcn)::factory() + ); + + return " * @method static \\{$factory} factory(\$count = 1, \$state = [])"; + } } diff --git a/tests/Feature/ModelsCommandTest.php b/tests/Feature/ModelsCommandTest.php index 5fe10c2..f476e06 100644 --- a/tests/Feature/ModelsCommandTest.php +++ b/tests/Feature/ModelsCommandTest.php @@ -2,6 +2,7 @@ namespace Soyhuce\NextIdeHelper\Tests\Feature; +use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Facades\File; use Soyhuce\NextIdeHelper\Tests\ResetsFixtures; use Soyhuce\NextIdeHelper\Tests\TestCase; @@ -13,6 +14,14 @@ class ModelsCommandTest extends TestCase { use ResetsFixtures; + protected function setUp(): void + { + parent::setUp(); + Factory::guessFactoryNamesUsing(function (string $modelFqcn) { + return 'Soyhuce\NextIdeHelper\Tests\Fixtures\Factories\\' . class_basename($modelFqcn) . 'Factory'; + }); + } + /** * @test */ diff --git a/tests/Fixtures/User.php b/tests/Fixtures/User.php index 307da0a..09b9dec 100644 --- a/tests/Fixtures/User.php +++ b/tests/Fixtures/User.php @@ -2,6 +2,7 @@ namespace Soyhuce\NextIdeHelper\Tests\Fixtures; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post; @@ -19,6 +20,8 @@ */ class User extends Model { + use HasFactory; + protected $casts = [ 'address' => AddressCaster::class, ]; diff --git a/tests/expected/User.stub b/tests/expected/User.stub index 5e1ba56..49c4708 100644 --- a/tests/expected/User.stub +++ b/tests/expected/User.stub @@ -2,6 +2,7 @@ namespace Soyhuce\NextIdeHelper\Tests\Fixtures; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post; @@ -16,9 +17,12 @@ use Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post; * @property \Illuminate\Support\Carbon $updated_at * @property-read \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection $laravelPosts * @property-read \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection $posts + * @method static \Soyhuce\NextIdeHelper\Tests\Fixtures\Factories\UserFactory factory($count = 1, $state = []) */ class User extends Model { + use HasFactory; + protected $casts = [ 'address' => AddressCaster::class, ];