From 7cc01b3f2a252f144581e62d1f0c3e1dedf9b1ef Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Mon, 29 Aug 2022 11:13:50 +0200 Subject: [PATCH] Change Larastan friendly rendering --- src/Domain/Models/Output/ModelDocBlock.php | 31 ++++++++++--------- .../Models/Output/QueryBuilderDocBlock.php | 25 +++------------ tests/expected/PostLarastan.stub | 5 ++- tests/expected/PostQueryLarastan.stub | 15 +++------ tests/expected/_ide_modelsLarastan.stub | 30 ++++++------------ 5 files changed, 38 insertions(+), 68 deletions(-) diff --git a/src/Domain/Models/Output/ModelDocBlock.php b/src/Domain/Models/Output/ModelDocBlock.php index 03ac8a0..9e4ad55 100644 --- a/src/Domain/Models/Output/ModelDocBlock.php +++ b/src/Domain/Models/Output/ModelDocBlock.php @@ -40,7 +40,6 @@ protected function docblock(): string $this->relations(), $this->all(), $this->query(), - $this->phpstanMethods(), $this->queryMixin(), $this->factory(), ' */', @@ -100,29 +99,26 @@ private function query(): ?string return null; } - return " * @method static {$this->model->queryBuilder->fqcn} query()"; - } - - private function all(): ?string - { - if ($this->model->collection->isBuiltIn()) { - return null; + $type = $this->model->queryBuilder->fqcn; + if ($this->larastanFriendly()) { + $type .= "<{$this->model->fqcn}>"; } - return " * @method static {$this->model->collection->fqcn} all(array|mixed \$columns = ['*'])"; + return " * @method static {$type} query()"; } - private function phpstanMethods(): ?string + private function all(): ?string { - if (!config('next-ide-helper.models.larastan_friendly', false)) { + if ($this->model->collection->isBuiltIn()) { return null; } - if ($this->model->collection->isBuiltIn()) { - return null; + $type = $this->model->collection->fqcn; + if ($this->larastanFriendly()) { + $type .= "model->fqcn}>"; } - return " * @phpstan-method static {$this->model->collection->fqcn}model->fqcn}> all(array|mixed \$columns = ['*'])"; + return " * @method static {$type} all(array|mixed \$columns = ['*'])"; } private function queryMixin(): ?string @@ -133,7 +129,7 @@ private function queryMixin(): ?string $mixin = " * @mixin {$this->model->queryBuilder->fqcn}"; - if (config('next-ide-helper.models.larastan_friendly', false)) { + if ($this->larastanFriendly()) { $mixin .= "<{$this->model->fqcn}>"; } @@ -161,4 +157,9 @@ class_uses_recursive($this->model->fqcn) return null; } } + + private function larastanFriendly(): bool + { + return (bool) config('next-ide-helper.models.larastan_friendly', false); + } } diff --git a/src/Domain/Models/Output/QueryBuilderDocBlock.php b/src/Domain/Models/Output/QueryBuilderDocBlock.php index 256b7ea..4dcf031 100644 --- a/src/Domain/Models/Output/QueryBuilderDocBlock.php +++ b/src/Domain/Models/Output/QueryBuilderDocBlock.php @@ -51,7 +51,6 @@ public function docTags(): Collection ->when( $this->larastanFriendly(), fn (Collection $collection) => $collection - ->merge($this->phpstanMethods()) ->merge($this->templateBlock()) ) ->merge($this->model->queryBuilder->extras); @@ -124,6 +123,10 @@ private function resultMethods(): Collection $model = $this->model->fqcn; $collection = $this->model->collection->fqcn; + if ($this->larastanFriendly()) { + $collection .= "model->fqcn}>"; + } + return Collection::make([ "{$model} create(array \$attributes = [])", "{$collection}|{$model}|null find(\$id, array \$columns = ['*'])", @@ -161,24 +164,6 @@ private function softDeletesMethods(): Collection ->map(static fn (string $method) => " * @method {$method}"); } - /** - * @return \Illuminate\Support\Collection - */ - private function phpstanMethods(): Collection - { - $model = $this->model->fqcn; - $collection = "{$this->model->collection->fqcn}"; - - return Collection::make([ - "{$collection}|{$model}|null find(\$id, array \$columns = ['*'])", - "{$collection} findMany(\$id, array \$columns = ['*'])", - "{$collection}|{$model} findOrFail(\$id, array \$columns = ['*'])", - "{$collection} get(array|string \$columns = ['*'])", - "{$collection} getModels(array|string \$columns = ['*'])", - ]) - ->map(static fn (string $method) => " * @phpstan-method {$method}"); - } - /** * @return \Illuminate\Support\Collection */ @@ -192,6 +177,6 @@ private function templateBlock(): Collection private function larastanFriendly(): bool { - return config('next-ide-helper.models.larastan_friendly', false); + return (bool) config('next-ide-helper.models.larastan_friendly', false); } } diff --git a/tests/expected/PostLarastan.stub b/tests/expected/PostLarastan.stub index d01daca..abad01c 100644 --- a/tests/expected/PostLarastan.stub +++ b/tests/expected/PostLarastan.stub @@ -25,9 +25,8 @@ use Soyhuce\NextIdeHelper\Tests\Fixtures\User; * @property-read mixed $mixed * @property-read \Illuminate\Database\Eloquent\Collection $comments * @property-read \Soyhuce\NextIdeHelper\Tests\Fixtures\User $user - * @method static \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection all(array|mixed $columns = ['*']) - * @method static \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostQuery query() - * @phpstan-method static \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection all(array|mixed $columns = ['*']) + * @method static \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection all(array|mixed $columns = ['*']) + * @method static \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostQuery<\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post> query() * @mixin \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostQuery<\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post> */ class Post extends Model diff --git a/tests/expected/PostQueryLarastan.stub b/tests/expected/PostQueryLarastan.stub index bd5c70f..586f6d9 100644 --- a/tests/expected/PostQueryLarastan.stub +++ b/tests/expected/PostQueryLarastan.stub @@ -16,18 +16,18 @@ use Illuminate\Database\Eloquent\Builder; * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostQuery whereUpdatedAt(\Illuminate\Support\Carbon|string $value) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostQuery whereDeletedAt(\Illuminate\Support\Carbon|string|null $value) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post create(array $attributes = []) - * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection|\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post|null find($id, array $columns = ['*']) - * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection findMany($id, array $columns = ['*']) - * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection|\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post findOrFail($id, array $columns = ['*']) + * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection|\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post|null find($id, array $columns = ['*']) + * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection findMany($id, array $columns = ['*']) + * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection|\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post findOrFail($id, array $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post findOrNew($id, array $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post|null first(array|string $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post firstOrCreate(array $attributes, array $values = []) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post firstOrFail(array $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post firstOrNew(array $attributes = [], array $values = []) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post forceCreate(array $attributes = []) - * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection get(array|string $columns = ['*']) + * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection get(array|string $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post getModel() - * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection getModels(array|string $columns = ['*']) + * @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 sole(array|string $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post updateOrCreate(array $attributes, array $values = []) @@ -35,11 +35,6 @@ use Illuminate\Database\Eloquent\Builder; * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostQuery withTrashed(bool $withTrashed = true) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostQuery withoutTrashed() * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostQuery onlyTrashed() - * @phpstan-method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection|\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post|null find($id, array $columns = ['*']) - * @phpstan-method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection findMany($id, array $columns = ['*']) - * @phpstan-method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection|\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post findOrFail($id, array $columns = ['*']) - * @phpstan-method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection get(array|string $columns = ['*']) - * @phpstan-method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection getModels(array|string $columns = ['*']) * @template TModelClass of \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post * @extends \Illuminate\Database\Eloquent\Builder */ diff --git a/tests/expected/_ide_modelsLarastan.stub b/tests/expected/_ide_modelsLarastan.stub index 56230a9..d178d06 100644 --- a/tests/expected/_ide_modelsLarastan.stub +++ b/tests/expected/_ide_modelsLarastan.stub @@ -10,26 +10,21 @@ namespace IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__CommentQuery whereCreatedAt(\Illuminate\Support\Carbon|string $value) * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__CommentQuery whereUpdatedAt(\Illuminate\Support\Carbon|string $value) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment create(array $attributes = []) - * @method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\Comment|null find($id, array $columns = ['*']) - * @method \Illuminate\Database\Eloquent\Collection findMany($id, array $columns = ['*']) - * @method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\Comment findOrFail($id, array $columns = ['*']) + * @method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\Comment|null find($id, array $columns = ['*']) + * @method \Illuminate\Database\Eloquent\Collection findMany($id, array $columns = ['*']) + * @method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\Comment findOrFail($id, array $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment findOrNew($id, array $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment|null first(array|string $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment firstOrCreate(array $attributes, array $values = []) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment firstOrFail(array $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment firstOrNew(array $attributes = [], array $values = []) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment forceCreate(array $attributes = []) - * @method \Illuminate\Database\Eloquent\Collection get(array|string $columns = ['*']) + * @method \Illuminate\Database\Eloquent\Collection get(array|string $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment getModel() - * @method \Illuminate\Database\Eloquent\Collection getModels(array|string $columns = ['*']) + * @method \Illuminate\Database\Eloquent\Collection getModels(array|string $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment newModelInstance(array $attributes = []) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment sole(array|string $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment updateOrCreate(array $attributes, array $values = []) - * @phpstan-method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\Comment|null find($id, array $columns = ['*']) - * @phpstan-method \Illuminate\Database\Eloquent\Collection findMany($id, array $columns = ['*']) - * @phpstan-method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\Comment findOrFail($id, array $columns = ['*']) - * @phpstan-method \Illuminate\Database\Eloquent\Collection get(array|string $columns = ['*']) - * @phpstan-method \Illuminate\Database\Eloquent\Collection getModels(array|string $columns = ['*']) * @template TModelClass of \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment * @extends \Illuminate\Database\Eloquent\Builder */ @@ -49,26 +44,21 @@ namespace IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereUpdatedAt(\Illuminate\Support\Carbon|string $value) * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereEmailDomain(string $domain, ?string $area = null) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User create(array $attributes = []) - * @method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\User|null find($id, array $columns = ['*']) - * @method \Illuminate\Database\Eloquent\Collection findMany($id, array $columns = ['*']) - * @method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\User findOrFail($id, array $columns = ['*']) + * @method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\User|null find($id, array $columns = ['*']) + * @method \Illuminate\Database\Eloquent\Collection findMany($id, array $columns = ['*']) + * @method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\User findOrFail($id, array $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User findOrNew($id, array $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User|null first(array|string $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User firstOrCreate(array $attributes, array $values = []) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User firstOrFail(array $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User firstOrNew(array $attributes = [], array $values = []) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User forceCreate(array $attributes = []) - * @method \Illuminate\Database\Eloquent\Collection get(array|string $columns = ['*']) + * @method \Illuminate\Database\Eloquent\Collection get(array|string $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User getModel() - * @method \Illuminate\Database\Eloquent\Collection getModels(array|string $columns = ['*']) + * @method \Illuminate\Database\Eloquent\Collection getModels(array|string $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User newModelInstance(array $attributes = []) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User sole(array|string $columns = ['*']) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User updateOrCreate(array $attributes, array $values = []) - * @phpstan-method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\User|null find($id, array $columns = ['*']) - * @phpstan-method \Illuminate\Database\Eloquent\Collection findMany($id, array $columns = ['*']) - * @phpstan-method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\User findOrFail($id, array $columns = ['*']) - * @phpstan-method \Illuminate\Database\Eloquent\Collection get(array|string $columns = ['*']) - * @phpstan-method \Illuminate\Database\Eloquent\Collection getModels(array|string $columns = ['*']) * @template TModelClass of \Soyhuce\NextIdeHelper\Tests\Fixtures\User * @extends \Illuminate\Database\Eloquent\Builder */