From af981c61d94368f28c72e5e6710cd38ba1afc5c4 Mon Sep 17 00:00:00 2001 From: Can Vural Date: Sun, 12 Apr 2020 18:30:01 +0200 Subject: [PATCH] fix: add more method definitions to stubs --- extension.neon | 1 + stubs/Arrayable.stub | 13 ++++ stubs/BelongstoMany.stub | 85 +++++++++++++++++++++++ stubs/HasOneOrMany.stub | 58 +++++++++++++++- stubs/Model.stub | 2 + tests/Features/Methods/ModelExtension.php | 6 ++ 6 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 stubs/Arrayable.stub diff --git a/extension.neon b/extension.neon index 1dcbe6650..7fdcddb91 100644 --- a/extension.neon +++ b/extension.neon @@ -1,5 +1,6 @@ parameters: stubFiles: + - stubs/Arrayable.stub - stubs/Enumerable.stub - stubs/EloquentBuilder.stub - stubs/Collection.stub diff --git a/stubs/Arrayable.stub b/stubs/Arrayable.stub new file mode 100644 index 000000000..81724ce6a --- /dev/null +++ b/stubs/Arrayable.stub @@ -0,0 +1,13 @@ + + */ + public function toArray(); +} diff --git a/stubs/BelongstoMany.stub b/stubs/BelongstoMany.stub index 35bd661b9..50afac20a 100644 --- a/stubs/BelongstoMany.stub +++ b/stubs/BelongstoMany.stub @@ -8,6 +8,91 @@ namespace Illuminate\Database\Eloquent\Relations; */ class BelongsToMany extends Relation { + /** + * Find a related model by its primary key or return new instance of the related model. + * + * @param mixed $id + * @param array $columns + * @return \Illuminate\Support\Collection|TRelatedModel + */ + public function findOrNew($id, $columns = ['*']); + + /** + * Get the first related model record matching the attributes or instantiate it. + * + * @param array $attributes + * @return TRelatedModel + */ + public function firstOrNew(array $attributes); + + /** + * Get the first related record matching the attributes or create it. + * + * @param array $attributes + * @param array $joining + * @param bool $touch + * @return TRelatedModel + */ + public function firstOrCreate(array $attributes, array $joining = [], $touch = true); + + /** + * Create or update a related record matching the attributes, and fill it with values. + * + * @param array $attributes + * @param array $values + * @param array $joining + * @param bool $touch + * @return TRelatedModel + */ + public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true); + + /** + * Find a related model by its primary key. + * + * @param mixed $id + * @param array $columns + * @return TRelatedModel|\Illuminate\Database\Eloquent\Collection|null + */ + public function find($id, $columns = ['*']); + + /** + * Find multiple related models by their primary keys. + * + * @param \Illuminate\Contracts\Support\Arrayable|int[] $ids + * @param array $columns + * @return \Illuminate\Database\Eloquent\Collection + */ + public function findMany($ids, $columns = ['*']); + + /** + * Find a related model by its primary key or throw an exception. + * + * @param mixed $id + * @param array $columns + * @return TRelatedModel|\Illuminate\Database\Eloquent\Collection + * + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + */ + public function findOrFail($id, $columns = ['*']); + + /** + * Execute the query and get the first result. + * + * @param array $columns + * @return TRelatedModel|null + */ + public function first($columns = ['*']); + + /** + * Execute the query and get the first result or throw an exception. + * + * @param array $columns + * @return TRelatedModel + * + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + */ + public function firstOrFail($columns = ['*']); + /** * Get the results of the relationship. * diff --git a/stubs/HasOneOrMany.stub b/stubs/HasOneOrMany.stub index 83a88d4e1..3dc910ea9 100644 --- a/stubs/HasOneOrMany.stub +++ b/stubs/HasOneOrMany.stub @@ -8,6 +8,56 @@ namespace Illuminate\Database\Eloquent\Relations; */ abstract class HasOneOrMany extends Relation { + /** + * @param array $attributes + * @phpstan-return TRelatedModel + */ + public function make(array $attributes = []); + + /** + * Find a model by its primary key or return new instance of the related model. + * + * @param mixed $id + * @param array $columns + * @return \Illuminate\Support\Collection|TRelatedModel + */ + public function findOrNew($id, $columns = ['*']); + + /** + * Get the first related model record matching the attributes or instantiate it. + * + * @param array $attributes + * @param array $values + * @return TRelatedModel + */ + public function firstOrNew(array $attributes, array $values = []); + + /** + * Get the first related record matching the attributes or create it. + * + * @param array $attributes + * @param array $values + * @return TRelatedModel + */ + public function firstOrCreate(array $attributes, array $values = []); + + /** + * Create or update a related record matching the attributes, and fill it with values. + * + * @param array $attributes + * @param array $values + * @return TRelatedModel + */ + public function updateOrCreate(array $attributes, array $values = []); + + /** + * Attach a model instance to the parent model. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @return TRelatedModel|false + */ + public function save(\Illuminate\Database\Eloquent\Model $model); + /** * @param array $attributes * @@ -16,8 +66,10 @@ abstract class HasOneOrMany extends Relation public function create(array $attributes = []); /** - * @param array $attributes - * @phpstan-return TRelatedModel + * Create a Collection of new instances of the related model. + * + * @param iterable $records + * @return \Illuminate\Database\Eloquent\Collection */ - public function make(array $attributes = []); + public function createMany(iterable $records); } diff --git a/stubs/Model.stub b/stubs/Model.stub index f13e8888b..81b0918bd 100644 --- a/stubs/Model.stub +++ b/stubs/Model.stub @@ -8,3 +8,5 @@ namespace Illuminate\Database\Eloquent; abstract class Model implements \JsonSerializable, \ArrayAccess { } + +class ModelNotFoundException extends \RuntimeException {} diff --git a/tests/Features/Methods/ModelExtension.php b/tests/Features/Methods/ModelExtension.php index aac1af4ef..2bedcec1a 100644 --- a/tests/Features/Methods/ModelExtension.php +++ b/tests/Features/Methods/ModelExtension.php @@ -4,6 +4,7 @@ namespace Tests\Features\Methods; +use App\Account; use App\User; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; @@ -215,6 +216,11 @@ public function testCustomAccessorOnModels(): string return $thread->custom_property; } + + public function testFirstOrCreateWithRelation(User $user): Account + { + return $user->accounts()->firstOrCreate([]); + } } function foo(): string