From bb1f24314038047d82e5415e1f4a106fa2b874bb Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Thu, 16 Jul 2020 10:10:39 +0200 Subject: [PATCH] Add option to generate larastan-friendly doc blocks for models --- assets/config.php | 5 ++ src/Domain/Models/Output/ModelDocBlock.php | 8 +- .../Models/Output/QueryBuilderDocBlock.php | 14 ++- tests/Feature/ModelsCommandTest.php | 37 ++++++++ tests/expected/PostLarastan.stub | 45 ++++++++++ tests/expected/PostQuery.stub | 2 - tests/expected/PostQueryLarastan.stub | 35 ++++++++ tests/expected/_ide_models.stub | 2 - tests/expected/_ide_modelsLarastan.stub | 85 +++++++++++++++++++ 9 files changed, 225 insertions(+), 8 deletions(-) create mode 100644 tests/expected/PostLarastan.stub create mode 100644 tests/expected/PostQueryLarastan.stub create mode 100644 tests/expected/_ide_modelsLarastan.stub diff --git a/assets/config.php b/assets/config.php index 20031ab..52969ff 100644 --- a/assets/config.php +++ b/assets/config.php @@ -34,6 +34,11 @@ * - Soyhuce\NextIdeHelper\Domain\Models\Extensions\VirtualAttributeResolver */ 'extensions' => [], + + /** + * Use Larastan friendly docblock when possible + */ + 'larastan_friendly' => false, ], /** diff --git a/src/Domain/Models/Output/ModelDocBlock.php b/src/Domain/Models/Output/ModelDocBlock.php index be35ccf..d5c1ad9 100644 --- a/src/Domain/Models/Output/ModelDocBlock.php +++ b/src/Domain/Models/Output/ModelDocBlock.php @@ -101,7 +101,13 @@ private function queryMixin(): ?string return null; } - return " * @mixin {$this->model->queryBuilder->fqcn}"; + $mixin = " * @mixin {$this->model->queryBuilder->fqcn}"; + + if (config('next-ide-helper.models.larastan_friendly', false)) { + $mixin .= "<{$this->model->fqcn}>"; + } + + return $mixin; } private function factory(): ?string diff --git a/src/Domain/Models/Output/QueryBuilderDocBlock.php b/src/Domain/Models/Output/QueryBuilderDocBlock.php index d8cab39..5ec7a95 100644 --- a/src/Domain/Models/Output/QueryBuilderDocBlock.php +++ b/src/Domain/Models/Output/QueryBuilderDocBlock.php @@ -38,7 +38,10 @@ public function docTags(): Collection ->merge($this->attributeScopes()) ->merge($this->scopeMethods()) ->merge($this->resultMethods()) - ->merge($this->templateBlock()); + ->when( + $this->larastanFriendly(), + fn (Collection $collection) => $collection->merge($this->templateBlock()) + ); } private function docblock(): string @@ -122,8 +125,13 @@ private function resultMethods(): Collection private function templateBlock(): Collection { return Collection::make([ - ' * @template TModelClass', - " * @extends \\Illuminate\\Database\\Eloquent\\Builder<{$this->model->fqcn}>", + " * @template TModelClass of {$this->model->fqcn}", + ' * @extends \Illuminate\Database\Eloquent\Builder', ]); } + + private function larastanFriendly(): bool + { + return config('next-ide-helper.models.larastan_friendly', false); + } } diff --git a/tests/Feature/ModelsCommandTest.php b/tests/Feature/ModelsCommandTest.php index f476e06..02647dd 100644 --- a/tests/Feature/ModelsCommandTest.php +++ b/tests/Feature/ModelsCommandTest.php @@ -58,6 +58,43 @@ public function theCommandIsSuccessful() ); } + /** + * @test + */ + public function theCommandIsSuccessfulWithLarastanFriendlyComments() + { + config([ + 'next-ide-helper.models' => [ + 'directories' => [$this->fixturePath()], + 'file_name' => $this->fixturePath() . '/_ide_models.php', + 'larastan_friendly' => true, + ], + ]); + + $this->artisan('next-ide-helper:models') + ->assertExitCode(0); + + $this->assertFileEquals( + $this->expectedPath('PostLarastan.stub'), + $this->fixturePath('Blog/Post.php') + ); + + $this->assertFileEquals( + $this->expectedPath('User.stub'), + $this->fixturePath('User.php') + ); + + $this->assertFileEquals( + $this->expectedPath('PostQueryLarastan.stub'), + $this->fixturePath('Blog/PostQuery.php') + ); + + $this->assertFileEquals( + $this->expectedPath('_ide_modelsLarastan.stub'), + $this->fixturePath('_ide_models.php') + ); + } + protected function tearDown(): void { File::delete($this->fixturePath('_ide_models.php')); diff --git a/tests/expected/PostLarastan.stub b/tests/expected/PostLarastan.stub new file mode 100644 index 0000000..50bbea8 --- /dev/null +++ b/tests/expected/PostLarastan.stub @@ -0,0 +1,45 @@ + + */ +class Post extends Model +{ + public function newEloquentBuilder($query) + { + return new PostQuery($query); + } + + public function newCollection(array $models = []) + { + return new PostCollection($models); + } + + public function user(): BelongsTo + { + return $this->belongsTo(User::class); + } + + public function getSlugAttribute(): string + { + return Str::slug($this->title); + } +} diff --git a/tests/expected/PostQuery.stub b/tests/expected/PostQuery.stub index 32d5220..b82333f 100644 --- a/tests/expected/PostQuery.stub +++ b/tests/expected/PostQuery.stub @@ -27,8 +27,6 @@ use Illuminate\Database\Eloquent\Builder; * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection getModels(array|string $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post newModelInstance(array $attributes = []) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post updateOrCreate(array $attributes, array $values = []) - * @template TModelClass - * @extends \Illuminate\Database\Eloquent\Builder<\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post> */ class PostQuery extends Builder { diff --git a/tests/expected/PostQueryLarastan.stub b/tests/expected/PostQueryLarastan.stub new file mode 100644 index 0000000..94117a5 --- /dev/null +++ b/tests/expected/PostQueryLarastan.stub @@ -0,0 +1,35 @@ + + */ +class PostQuery extends Builder +{ +} diff --git a/tests/expected/_ide_models.stub b/tests/expected/_ide_models.stub index 31d8b85..f5bd971 100644 --- a/tests/expected/_ide_models.stub +++ b/tests/expected/_ide_models.stub @@ -26,8 +26,6 @@ namespace IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures * @method \Illuminate\Database\Eloquent\Collection getModels(array|string $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User newModelInstance(array $attributes = []) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User updateOrCreate(array $attributes, array $values = []) - * @template TModelClass - * @extends \Illuminate\Database\Eloquent\Builder<\Soyhuce\NextIdeHelper\Tests\Fixtures\User> */ class UserQuery extends \Illuminate\Database\Eloquent\Builder { diff --git a/tests/expected/_ide_modelsLarastan.stub b/tests/expected/_ide_modelsLarastan.stub new file mode 100644 index 0000000..15b495e --- /dev/null +++ b/tests/expected/_ide_modelsLarastan.stub @@ -0,0 +1,85 @@ + + */ + class UserQuery extends \Illuminate\Database\Eloquent\Builder + { + } +} + +namespace IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post +{ + /** + * @mixin \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\UserQuery + */ + class User extends \Illuminate\Database\Eloquent\Relations\BelongsTo + { + } +} + +namespace IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\User +{ + /** + * @mixin \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostQuery + */ + class LaravelPosts extends \Illuminate\Database\Eloquent\Relations\HasMany + { + } + + /** + * @mixin \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostQuery + */ + class Posts extends \Illuminate\Database\Eloquent\Relations\HasMany + { + } +} + +namespace Soyhuce\NextIdeHelper\Tests\Fixtures +{ + /** + * @method static \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\UserQuery query() + * @mixin \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\UserQuery + * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\User\Posts posts() + * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\User\LaravelPosts laravelPosts() + */ + class User + { + } +} + +namespace Soyhuce\NextIdeHelper\Tests\Fixtures\Blog +{ + /** + * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post\User user() + */ + class Post + { + } +}