Skip to content

Commit

Permalink
Change Larastan friendly rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi committed Aug 29, 2022
1 parent 6551ade commit 7cc01b3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 68 deletions.
31 changes: 16 additions & 15 deletions src/Domain/Models/Output/ModelDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ protected function docblock(): string
$this->relations(),
$this->all(),
$this->query(),
$this->phpstanMethods(),
$this->queryMixin(),
$this->factory(),
' */',
Expand Down Expand Up @@ -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 .= "<int, {$this->model->fqcn}>";
}

return " * @phpstan-method static {$this->model->collection->fqcn}<int, {$this->model->fqcn}> all(array|mixed \$columns = ['*'])";
return " * @method static {$type} all(array|mixed \$columns = ['*'])";
}

private function queryMixin(): ?string
Expand All @@ -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}>";
}

Expand Down Expand Up @@ -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);
}
}
25 changes: 5 additions & 20 deletions src/Domain/Models/Output/QueryBuilderDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -124,6 +123,10 @@ private function resultMethods(): Collection
$model = $this->model->fqcn;
$collection = $this->model->collection->fqcn;

if ($this->larastanFriendly()) {
$collection .= "<int, {$this->model->fqcn}>";
}

return Collection::make([
"{$model} create(array \$attributes = [])",
"{$collection}|{$model}|null find(\$id, array \$columns = ['*'])",
Expand Down Expand Up @@ -161,24 +164,6 @@ private function softDeletesMethods(): Collection
->map(static fn (string $method) => " * @method {$method}");
}

/**
* @return \Illuminate\Support\Collection<int, string>
*/
private function phpstanMethods(): Collection
{
$model = $this->model->fqcn;
$collection = "{$this->model->collection->fqcn}<int, {$model}>";

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<int, string>
*/
Expand All @@ -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);
}
}
5 changes: 2 additions & 3 deletions tests/expected/PostLarastan.stub
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ use Soyhuce\NextIdeHelper\Tests\Fixtures\User;
* @property-read mixed $mixed
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment> $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<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post> all(array|mixed $columns = ['*'])
* @method static \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post> 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
Expand Down
15 changes: 5 additions & 10 deletions tests/expected/PostQueryLarastan.stub
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,25 @@ 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<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post>|\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post|null find($id, array $columns = ['*'])
* @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post> findMany($id, array $columns = ['*'])
* @method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post>|\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<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post> 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<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post> 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 = [])
* @method int restore()
* @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<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post>|\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post|null find($id, array $columns = ['*'])
* @phpstan-method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post> findMany($id, array $columns = ['*'])
* @phpstan-method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post>|\Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post findOrFail($id, array $columns = ['*'])
* @phpstan-method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post> get(array|string $columns = ['*'])
* @phpstan-method \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post> getModels(array|string $columns = ['*'])
* @template TModelClass of \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post
* @extends \Illuminate\Database\Eloquent\Builder<TModelClass>
*/
Expand Down
30 changes: 10 additions & 20 deletions tests/expected/_ide_modelsLarastan.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment>|\Soyhuce\NextIdeHelper\Tests\Fixtures\Comment|null find($id, array $columns = ['*'])
* @method \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment> findMany($id, array $columns = ['*'])
* @method \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment>|\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<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment> 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<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment> 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<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment>|\Soyhuce\NextIdeHelper\Tests\Fixtures\Comment|null find($id, array $columns = ['*'])
* @phpstan-method \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment> findMany($id, array $columns = ['*'])
* @phpstan-method \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment>|\Soyhuce\NextIdeHelper\Tests\Fixtures\Comment findOrFail($id, array $columns = ['*'])
* @phpstan-method \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment> get(array|string $columns = ['*'])
* @phpstan-method \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment> getModels(array|string $columns = ['*'])
* @template TModelClass of \Soyhuce\NextIdeHelper\Tests\Fixtures\Comment
* @extends \Illuminate\Database\Eloquent\Builder<TModelClass>
*/
Expand All @@ -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<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\User>|\Soyhuce\NextIdeHelper\Tests\Fixtures\User|null find($id, array $columns = ['*'])
* @method \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\User> findMany($id, array $columns = ['*'])
* @method \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\User>|\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<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\User> 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<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\User> 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<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\User>|\Soyhuce\NextIdeHelper\Tests\Fixtures\User|null find($id, array $columns = ['*'])
* @phpstan-method \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\User> findMany($id, array $columns = ['*'])
* @phpstan-method \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\User>|\Soyhuce\NextIdeHelper\Tests\Fixtures\User findOrFail($id, array $columns = ['*'])
* @phpstan-method \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\User> get(array|string $columns = ['*'])
* @phpstan-method \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\User> getModels(array|string $columns = ['*'])
* @template TModelClass of \Soyhuce\NextIdeHelper\Tests\Fixtures\User
* @extends \Illuminate\Database\Eloquent\Builder<TModelClass>
*/
Expand Down

0 comments on commit 7cc01b3

Please sign in to comment.