From cf60634eaf6416356418c2f9a9f77e1f8affb189 Mon Sep 17 00:00:00 2001 From: Ruslan Asanov Date: Sun, 2 Jun 2024 08:36:02 +0500 Subject: [PATCH 1/4] Add generic PHPDoc to relations --- .../Database/Eloquent/Relations/BelongsTo.php | 18 +++++--- .../Eloquent/Relations/BelongsToMany.php | 44 ++++++++++--------- .../Database/Eloquent/Relations/HasMany.php | 6 ++- .../Eloquent/Relations/HasManyThrough.php | 34 +++++++------- .../Database/Eloquent/Relations/HasOne.php | 4 ++ .../Eloquent/Relations/HasOneOrMany.php | 34 +++++++------- .../Eloquent/Relations/HasOneThrough.php | 4 ++ .../Database/Eloquent/Relations/MorphMany.php | 10 +++-- .../Database/Eloquent/Relations/MorphOne.php | 6 ++- .../Eloquent/Relations/MorphOneOrMany.php | 6 ++- .../Database/Eloquent/Relations/MorphTo.php | 20 +++++---- .../Eloquent/Relations/MorphToMany.php | 4 ++ .../Database/Eloquent/Relations/Relation.php | 11 +++-- 13 files changed, 126 insertions(+), 75 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php index 112a0edba022..790d1cf0120b 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php @@ -10,6 +10,10 @@ use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; +/** + * @template T of \Illuminate\Database\Eloquent\Model + * @extends Relation + */ class BelongsTo extends Relation { use ComparesRelatedModels, @@ -19,7 +23,7 @@ class BelongsTo extends Relation /** * The child model instance of the relation. * - * @var \Illuminate\Database\Eloquent\Model + * @var T */ protected $child; @@ -195,8 +199,8 @@ public function match(array $models, Collection $results, $relation) /** * Associate the model instance to the given parent. * - * @param \Illuminate\Database\Eloquent\Model|int|string|null $model - * @return \Illuminate\Database\Eloquent\Model + * @param T|int|string|null $model + * @return T */ public function associate($model) { @@ -216,7 +220,7 @@ public function associate($model) /** * Dissociate previously associated model from the given parent. * - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function dissociate() { @@ -228,7 +232,7 @@ public function dissociate() /** * Alias of "dissociate" method. * - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function disassociate() { @@ -290,7 +294,7 @@ protected function relationHasIncrementingId() * Make a new related instance for the given model. * * @param \Illuminate\Database\Eloquent\Model $parent - * @return \Illuminate\Database\Eloquent\Model + * @return T */ protected function newRelatedInstanceFor(Model $parent) { @@ -300,7 +304,7 @@ protected function newRelatedInstanceFor(Model $parent) /** * Get the child of the relationship. * - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function getChild() { diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index ef17cce6371c..dd3344519498 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -16,6 +16,10 @@ use Illuminate\Support\Str; use InvalidArgumentException; +/** + * @template T of \Illuminate\Database\Eloquent\Model + * @extends Relation + */ class BelongsToMany extends Relation { use InteractsWithDictionary, InteractsWithPivotTable; @@ -583,7 +587,7 @@ public function orderByPivot($column, $direction = 'asc') * * @param mixed $id * @param array $columns - * @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model + * @return \Illuminate\Support\Collection|T */ public function findOrNew($id, $columns = ['*']) { @@ -599,7 +603,7 @@ public function findOrNew($id, $columns = ['*']) * * @param array $attributes * @param array $values - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function firstOrNew(array $attributes = [], array $values = []) { @@ -617,7 +621,7 @@ public function firstOrNew(array $attributes = [], array $values = []) * @param array $values * @param array $joining * @param bool $touch - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function firstOrCreate(array $attributes = [], array $values = [], array $joining = [], $touch = true) { @@ -643,7 +647,7 @@ public function firstOrCreate(array $attributes = [], array $values = [], array * @param array $values * @param array $joining * @param bool $touch - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function createOrFirst(array $attributes = [], array $values = [], array $joining = [], $touch = true) { @@ -669,7 +673,7 @@ public function createOrFirst(array $attributes = [], array $values = [], array * @param array $values * @param array $joining * @param bool $touch - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true) { @@ -687,7 +691,7 @@ public function updateOrCreate(array $attributes, array $values = [], array $joi * * @param mixed $id * @param array $columns - * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|null + * @return T|\Illuminate\Database\Eloquent\Collection|null */ public function find($id, $columns = ['*']) { @@ -705,7 +709,7 @@ public function find($id, $columns = ['*']) * * @param \Illuminate\Contracts\Support\Arrayable|array $ids * @param array $columns - * @return \Illuminate\Database\Eloquent\Collection + * @return \Illuminate\Database\Eloquent\Collection */ public function findMany($ids, $columns = ['*']) { @@ -725,7 +729,7 @@ public function findMany($ids, $columns = ['*']) * * @param mixed $id * @param array $columns - * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection + * @return T|\Illuminate\Database\Eloquent\Collection * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ @@ -752,7 +756,7 @@ public function findOrFail($id, $columns = ['*']) * @param mixed $id * @param \Closure|array $columns * @param \Closure|null $callback - * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|mixed + * @return T|\Illuminate\Database\Eloquent\Collection|mixed */ public function findOr($id, $columns = ['*'], ?Closure $callback = null) { @@ -784,7 +788,7 @@ public function findOr($id, $columns = ['*'], ?Closure $callback = null) * @param mixed $operator * @param mixed $value * @param string $boolean - * @return \Illuminate\Database\Eloquent\Model|static|null + * @return T|static|null */ public function firstWhere($column, $operator = null, $value = null, $boolean = 'and') { @@ -795,7 +799,7 @@ public function firstWhere($column, $operator = null, $value = null, $boolean = * Execute the query and get the first result. * * @param array $columns - * @return \Illuminate\Database\Eloquent\Model|static|null + * @return T|static|null */ public function first($columns = ['*']) { @@ -808,9 +812,9 @@ public function first($columns = ['*']) * Execute the query and get the first result or throw an exception. * * @param array $columns - * @return \Illuminate\Database\Eloquent\Model|static + * @return T|static * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException */ public function firstOrFail($columns = ['*']) { @@ -826,7 +830,7 @@ public function firstOrFail($columns = ['*']) * * @param \Closure|array $columns * @param \Closure|null $callback - * @return \Illuminate\Database\Eloquent\Model|static|mixed + * @return T|static|mixed */ public function firstOr($columns = ['*'], ?Closure $callback = null) { @@ -859,7 +863,7 @@ public function getResults() * Execute the query as a "select" statement. * * @param array $columns - * @return \Illuminate\Database\Eloquent\Collection + * @return \Illuminate\Database\Eloquent\Collection */ public function get($columns = ['*']) { @@ -1284,7 +1288,7 @@ public function allRelatedIds() * @param \Illuminate\Database\Eloquent\Model $model * @param array $pivotAttributes * @param bool $touch - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function save(Model $model, array $pivotAttributes = [], $touch = true) { @@ -1301,7 +1305,7 @@ public function save(Model $model, array $pivotAttributes = [], $touch = true) * @param \Illuminate\Database\Eloquent\Model $model * @param array $pivotAttributes * @param bool $touch - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function saveQuietly(Model $model, array $pivotAttributes = [], $touch = true) { @@ -1313,7 +1317,7 @@ public function saveQuietly(Model $model, array $pivotAttributes = [], $touch = /** * Save an array of new models and attach them to the parent model. * - * @param \Illuminate\Support\Collection|array $models + * @param \Illuminate\Support\Collection|array $models * @param array $pivotAttributes * @return array */ @@ -1331,7 +1335,7 @@ public function saveMany($models, array $pivotAttributes = []) /** * Save an array of new models without raising any events and attach them to the parent model. * - * @param \Illuminate\Support\Collection|array $models + * @param \Illuminate\Support\Collection|array $models * @param array $pivotAttributes * @return array */ @@ -1348,7 +1352,7 @@ public function saveManyQuietly($models, array $pivotAttributes = []) * @param array $attributes * @param array $joining * @param bool $touch - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function create(array $attributes = [], array $joining = [], $touch = true) { diff --git a/src/Illuminate/Database/Eloquent/Relations/HasMany.php b/src/Illuminate/Database/Eloquent/Relations/HasMany.php index 27bcd73e39b2..f91f382f8840 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasMany.php @@ -4,12 +4,16 @@ use Illuminate\Database\Eloquent\Collection; +/** + * @template T + * @extends HasOneOrMany + */ class HasMany extends HasOneOrMany { /** * Convert the relationship to a "has one" relationship. * - * @return \Illuminate\Database\Eloquent\Relations\HasOne + * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function one() { diff --git a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php index 2f3dc31fef60..4b62a3a60213 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php @@ -13,6 +13,10 @@ use Illuminate\Database\Query\Grammars\MySqlGrammar; use Illuminate\Database\UniqueConstraintViolationException; +/** + * @template T of \Illuminate\Database\Eloquent\Model + * @extends Relation + */ class HasManyThrough extends Relation { use InteractsWithDictionary; @@ -254,7 +258,7 @@ protected function buildDictionary(Collection $results) * * @param array $attributes * @param array $values - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function firstOrNew(array $attributes = [], array $values = []) { @@ -270,7 +274,7 @@ public function firstOrNew(array $attributes = [], array $values = []) * * @param array $attributes * @param array $values - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function firstOrCreate(array $attributes = [], array $values = []) { @@ -286,7 +290,7 @@ public function firstOrCreate(array $attributes = [], array $values = []) * * @param array $attributes * @param array $values - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function createOrFirst(array $attributes = [], array $values = []) { @@ -302,7 +306,7 @@ public function createOrFirst(array $attributes = [], array $values = []) * * @param array $attributes * @param array $values - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function updateOrCreate(array $attributes, array $values = []) { @@ -320,7 +324,7 @@ public function updateOrCreate(array $attributes, array $values = []) * @param mixed $operator * @param mixed $value * @param string $boolean - * @return \Illuminate\Database\Eloquent\Model|static|null + * @return T|static|null */ public function firstWhere($column, $operator = null, $value = null, $boolean = 'and') { @@ -331,7 +335,7 @@ public function firstWhere($column, $operator = null, $value = null, $boolean = * Execute the query and get the first related model. * * @param array $columns - * @return \Illuminate\Database\Eloquent\Model|static|null + * @return T|static|null */ public function first($columns = ['*']) { @@ -344,9 +348,9 @@ public function first($columns = ['*']) * Execute the query and get the first result or throw an exception. * * @param array $columns - * @return \Illuminate\Database\Eloquent\Model|static + * @return T|static * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException */ public function firstOrFail($columns = ['*']) { @@ -362,7 +366,7 @@ public function firstOrFail($columns = ['*']) * * @param \Closure|array $columns * @param \Closure|null $callback - * @return \Illuminate\Database\Eloquent\Model|static|mixed + * @return T|static|mixed */ public function firstOr($columns = ['*'], ?Closure $callback = null) { @@ -384,7 +388,7 @@ public function firstOr($columns = ['*'], ?Closure $callback = null) * * @param mixed $id * @param array $columns - * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|null + * @return T|\Illuminate\Database\Eloquent\Collection|null */ public function find($id, $columns = ['*']) { @@ -402,7 +406,7 @@ public function find($id, $columns = ['*']) * * @param \Illuminate\Contracts\Support\Arrayable|array $ids * @param array $columns - * @return \Illuminate\Database\Eloquent\Collection + * @return \Illuminate\Database\Eloquent\Collection */ public function findMany($ids, $columns = ['*']) { @@ -422,9 +426,9 @@ public function findMany($ids, $columns = ['*']) * * @param mixed $id * @param array $columns - * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection + * @return T|\Illuminate\Database\Eloquent\Collection * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException */ public function findOrFail($id, $columns = ['*']) { @@ -449,7 +453,7 @@ public function findOrFail($id, $columns = ['*']) * @param mixed $id * @param \Closure|array $columns * @param \Closure|null $callback - * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|mixed + * @return T|\Illuminate\Database\Eloquent\Collection|mixed */ public function findOr($id, $columns = ['*'], ?Closure $callback = null) { @@ -490,7 +494,7 @@ public function getResults() * Execute the query as a "select" statement. * * @param array $columns - * @return \Illuminate\Database\Eloquent\Collection + * @return \Illuminate\Database\Eloquent\Collection */ public function get($columns = ['*']) { diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOne.php b/src/Illuminate/Database/Eloquent/Relations/HasOne.php index ed85f1e910ee..ade601a17d69 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOne.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOne.php @@ -11,6 +11,10 @@ use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; use Illuminate\Database\Query\JoinClause; +/** + * @template T + * @extends HasOneOrMany + */ class HasOne extends HasOneOrMany implements SupportsPartialRelations { use ComparesRelatedModels, CanBeOneOfMany, SupportsDefaultModels; diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index e1d295d86be4..f3259a626db1 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -8,6 +8,10 @@ use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; use Illuminate\Database\UniqueConstraintViolationException; +/** + * @template T of \Illuminate\Database\Eloquent\Model + * @extends Relation + */ abstract class HasOneOrMany extends Relation { use InteractsWithDictionary; @@ -47,7 +51,7 @@ public function __construct(Builder $query, Model $parent, $foreignKey, $localKe * Create and return an un-saved instance of the related model. * * @param array $attributes - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function make(array $attributes = []) { @@ -60,7 +64,7 @@ public function make(array $attributes = []) * Create and return an un-saved instance of the related models. * * @param iterable $records - * @return \Illuminate\Database\Eloquent\Collection + * @return \Illuminate\Database\Eloquent\Collection */ public function makeMany($records) { @@ -195,7 +199,7 @@ protected function buildDictionary(Collection $results) * * @param mixed $id * @param array $columns - * @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model + * @return \Illuminate\Support\Collection|T */ public function findOrNew($id, $columns = ['*']) { @@ -213,7 +217,7 @@ public function findOrNew($id, $columns = ['*']) * * @param array $attributes * @param array $values - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function firstOrNew(array $attributes = [], array $values = []) { @@ -231,7 +235,7 @@ public function firstOrNew(array $attributes = [], array $values = []) * * @param array $attributes * @param array $values - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function firstOrCreate(array $attributes = [], array $values = []) { @@ -247,7 +251,7 @@ public function firstOrCreate(array $attributes = [], array $values = []) * * @param array $attributes * @param array $values - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function createOrFirst(array $attributes = [], array $values = []) { @@ -263,7 +267,7 @@ public function createOrFirst(array $attributes = [], array $values = []) * * @param array $attributes * @param array $values - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function updateOrCreate(array $attributes, array $values = []) { @@ -278,7 +282,7 @@ public function updateOrCreate(array $attributes, array $values = []) * Attach a model instance to the parent model. * * @param \Illuminate\Database\Eloquent\Model $model - * @return \Illuminate\Database\Eloquent\Model|false + * @return T|false */ public function save(Model $model) { @@ -291,7 +295,7 @@ public function save(Model $model) * Attach a model instance without raising any events to the parent model. * * @param \Illuminate\Database\Eloquent\Model $model - * @return \Illuminate\Database\Eloquent\Model|false + * @return T|false */ public function saveQuietly(Model $model) { @@ -332,7 +336,7 @@ public function saveManyQuietly($models) * Create a new instance of the related model. * * @param array $attributes - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function create(array $attributes = []) { @@ -347,7 +351,7 @@ public function create(array $attributes = []) * Create a new instance of the related model without raising any events to the parent model. * * @param array $attributes - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function createQuietly(array $attributes = []) { @@ -358,7 +362,7 @@ public function createQuietly(array $attributes = []) * Create a new instance of the related model. Allow mass-assignment. * * @param array $attributes - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function forceCreate(array $attributes = []) { @@ -371,7 +375,7 @@ public function forceCreate(array $attributes = []) * Create a new instance of the related model with mass assignment without raising model events. * * @param array $attributes - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function forceCreateQuietly(array $attributes = []) { @@ -382,7 +386,7 @@ public function forceCreateQuietly(array $attributes = []) * Create a Collection of new instances of the related model. * * @param iterable $records - * @return \Illuminate\Database\Eloquent\Collection + * @return \Illuminate\Database\Eloquent\Collection */ public function createMany(iterable $records) { @@ -399,7 +403,7 @@ public function createMany(iterable $records) * Create a Collection of new instances of the related model without raising any events to the parent model. * * @param iterable $records - * @return \Illuminate\Database\Eloquent\Collection + * @return \Illuminate\Database\Eloquent\Collection */ public function createManyQuietly(iterable $records) { diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php index ed9c7baa4dc3..ae1651c70b02 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php @@ -7,6 +7,10 @@ use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; +/** + * @template T of \Illuminate\Database\Eloquent\Model + * @extends HasManyThrough + */ class HasOneThrough extends HasManyThrough { use InteractsWithDictionary, SupportsDefaultModels; diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphMany.php index 3636f25d06c2..dd70651b8d0e 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphMany.php @@ -5,12 +5,16 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; +/** + * @template T of \Illuminate\Database\Eloquent\Model + * @extends MorphOneOrMany + */ class MorphMany extends MorphOneOrMany { /** * Convert the relationship to a "morph one" relationship. * - * @return \Illuminate\Database\Eloquent\Relations\MorphOne + * @return \Illuminate\Database\Eloquent\Relations\MorphOne */ public function one() { @@ -68,7 +72,7 @@ public function match(array $models, Collection $results, $relation) * Create a new instance of the related model. Allow mass-assignment. * * @param array $attributes - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function forceCreate(array $attributes = []) { @@ -81,7 +85,7 @@ public function forceCreate(array $attributes = []) * Create a new instance of the related model with mass assignment without raising model events. * * @param array $attributes - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function forceCreateQuietly(array $attributes = []) { diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphOne.php b/src/Illuminate/Database/Eloquent/Relations/MorphOne.php index fc8f4dc8ca48..c1e7b3faeab4 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphOne.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphOne.php @@ -11,6 +11,10 @@ use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; use Illuminate\Database\Query\JoinClause; +/** + * @template T of \Illuminate\Database\Eloquent\Model + * @extends MorphOneOrMany + */ class MorphOne extends MorphOneOrMany implements SupportsPartialRelations { use CanBeOneOfMany, ComparesRelatedModels, SupportsDefaultModels; @@ -115,7 +119,7 @@ public function addOneOfManyJoinSubQueryConstraints(JoinClause $join) * Make a new related instance for the given model. * * @param \Illuminate\Database\Eloquent\Model $parent - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function newRelatedInstanceFor(Model $parent) { diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php index 3cfec895548d..c4ca4d7c7c4b 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php @@ -5,6 +5,10 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; +/** + * @template T of \Illuminate\Database\Eloquent\Model + * @extends MorphOneOrMany + */ abstract class MorphOneOrMany extends HasOneOrMany { /** @@ -71,7 +75,7 @@ public function addEagerConstraints(array $models) * Create a new instance of the related model. Allow mass-assignment. * * @param array $attributes - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function forceCreate(array $attributes = []) { diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index 570287ac0b48..77d00d5814f6 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -8,6 +8,10 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; +/** + * @template T of \Illuminate\Database\Eloquent\Model + * @extends BelongsTo + */ class MorphTo extends BelongsTo { use InteractsWithDictionary; @@ -128,7 +132,7 @@ public function getEager() * Get all of the relation results for a type. * * @param string $type - * @return \Illuminate\Database\Eloquent\Collection + * @return \Illuminate\Database\Eloquent\Collection */ protected function getResultsByType($type) { @@ -177,7 +181,7 @@ protected function gatherKeysByType($type, $keyType) * Create a new model instance by type. * * @param string $type - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function createModelByType($type) { @@ -227,7 +231,7 @@ protected function matchToMorphParents($type, Collection $results) * Associate the model instance to the given parent. * * @param \Illuminate\Database\Eloquent\Model|null $model - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function associate($model) { @@ -251,7 +255,7 @@ public function associate($model) /** * Dissociate previously associated model from the given parent. * - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function dissociate() { @@ -278,7 +282,7 @@ public function touch() * Make a new related instance for the given model. * * @param \Illuminate\Database\Eloquent\Model $parent - * @return \Illuminate\Database\Eloquent\Model + * @return T */ protected function newRelatedInstanceFor(Model $parent) { @@ -309,7 +313,7 @@ public function getDictionary() * Specify which relations to load for a given morph type. * * @param array $with - * @return \Illuminate\Database\Eloquent\Relations\MorphTo + * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function morphWith(array $with) { @@ -324,7 +328,7 @@ public function morphWith(array $with) * Specify which relationship counts to load for a given morph type. * * @param array $withCount - * @return \Illuminate\Database\Eloquent\Relations\MorphTo + * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function morphWithCount(array $withCount) { @@ -339,7 +343,7 @@ public function morphWithCount(array $withCount) * Specify constraints on the query for a given morph type. * * @param array $callbacks - * @return \Illuminate\Database\Eloquent\Relations\MorphTo + * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function constrain(array $callbacks) { diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php index 8cf113bd0f34..c5b383cb8a81 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php @@ -6,6 +6,10 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; +/** + * @template T of \Illuminate\Database\Eloquent\Model + * @extends BelongsToMany + */ class MorphToMany extends BelongsToMany { /** diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index b5e8864f0937..e7e0db209493 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -13,6 +13,9 @@ use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\Macroable; +/** + * @template T of \Illuminate\Database\Eloquent\Model + */ abstract class Relation implements BuilderContract { use ForwardsCalls, Macroable { @@ -157,7 +160,7 @@ abstract public function getResults(); /** * Get the relationship for eager loading. * - * @return \Illuminate\Database\Eloquent\Collection + * @return \Illuminate\Database\Eloquent\Collection */ public function getEager() { @@ -170,7 +173,7 @@ public function getEager() * Execute the query and get the first result if it's the sole matching record. * * @param array|string $columns - * @return \Illuminate\Database\Eloquent\Model + * @return T * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> * @throws \Illuminate\Database\MultipleRecordsFoundException @@ -196,7 +199,7 @@ public function sole($columns = ['*']) * Execute the query as a "select" statement. * * @param array $columns - * @return \Illuminate\Database\Eloquent\Collection + * @return \Illuminate\Database\Eloquent\Collection */ public function get($columns = ['*']) { @@ -349,7 +352,7 @@ public function getQualifiedParentKeyName() /** * Get the related model of the relation. * - * @return \Illuminate\Database\Eloquent\Model + * @return T */ public function getRelated() { From a4dfbdb5db48fb4a008c6a9da2a34e63dfad2ce1 Mon Sep 17 00:00:00 2001 From: Ruslan Asanov Date: Sun, 2 Jun 2024 10:12:02 +0500 Subject: [PATCH 2/4] fix code style --- src/Illuminate/Database/Eloquent/Relations/BelongsTo.php | 1 + src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php | 1 + src/Illuminate/Database/Eloquent/Relations/HasMany.php | 1 + src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php | 1 + src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php | 1 + src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php | 1 + src/Illuminate/Database/Eloquent/Relations/MorphMany.php | 1 + src/Illuminate/Database/Eloquent/Relations/MorphOne.php | 1 + src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php | 3 ++- src/Illuminate/Database/Eloquent/Relations/MorphTo.php | 1 + src/Illuminate/Database/Eloquent/Relations/MorphToMany.php | 1 + 11 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php index 790d1cf0120b..dc6dcd60b323 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php @@ -12,6 +12,7 @@ /** * @template T of \Illuminate\Database\Eloquent\Model + * * @extends Relation */ class BelongsTo extends Relation diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index dd3344519498..044d18b4e19a 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -18,6 +18,7 @@ /** * @template T of \Illuminate\Database\Eloquent\Model + * * @extends Relation */ class BelongsToMany extends Relation diff --git a/src/Illuminate/Database/Eloquent/Relations/HasMany.php b/src/Illuminate/Database/Eloquent/Relations/HasMany.php index f91f382f8840..f799860df69e 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasMany.php @@ -6,6 +6,7 @@ /** * @template T + * * @extends HasOneOrMany */ class HasMany extends HasOneOrMany diff --git a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php index 4b62a3a60213..8dbcea5ce5e6 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php @@ -15,6 +15,7 @@ /** * @template T of \Illuminate\Database\Eloquent\Model + * * @extends Relation */ class HasManyThrough extends Relation diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index f3259a626db1..bbc667c9e361 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -10,6 +10,7 @@ /** * @template T of \Illuminate\Database\Eloquent\Model + * * @extends Relation */ abstract class HasOneOrMany extends Relation diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php index ae1651c70b02..bf749a6354e0 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php @@ -9,6 +9,7 @@ /** * @template T of \Illuminate\Database\Eloquent\Model + * * @extends HasManyThrough */ class HasOneThrough extends HasManyThrough diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphMany.php index dd70651b8d0e..86eaf71b93f0 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphMany.php @@ -7,6 +7,7 @@ /** * @template T of \Illuminate\Database\Eloquent\Model + * * @extends MorphOneOrMany */ class MorphMany extends MorphOneOrMany diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphOne.php b/src/Illuminate/Database/Eloquent/Relations/MorphOne.php index c1e7b3faeab4..22e29e2d7a78 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphOne.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphOne.php @@ -13,6 +13,7 @@ /** * @template T of \Illuminate\Database\Eloquent\Model + * * @extends MorphOneOrMany */ class MorphOne extends MorphOneOrMany implements SupportsPartialRelations diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php index c4ca4d7c7c4b..7789ba8e238b 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php @@ -7,7 +7,8 @@ /** * @template T of \Illuminate\Database\Eloquent\Model - * @extends MorphOneOrMany + * + * @extends HasOneOrMany */ abstract class MorphOneOrMany extends HasOneOrMany { diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index 77d00d5814f6..eb0fe747f741 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -10,6 +10,7 @@ /** * @template T of \Illuminate\Database\Eloquent\Model + * * @extends BelongsTo */ class MorphTo extends BelongsTo diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php index c5b383cb8a81..782c5c180c17 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php @@ -8,6 +8,7 @@ /** * @template T of \Illuminate\Database\Eloquent\Model + * * @extends BelongsToMany */ class MorphToMany extends BelongsToMany From 98f35037ec6893b67c7a6121cb704aca3f4c1e88 Mon Sep 17 00:00:00 2001 From: Ruslan Asanov Date: Sun, 2 Jun 2024 10:13:46 +0500 Subject: [PATCH 3/4] fix code style --- src/Illuminate/Database/Eloquent/Relations/HasOne.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOne.php b/src/Illuminate/Database/Eloquent/Relations/HasOne.php index ade601a17d69..5701261aad2d 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOne.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOne.php @@ -13,6 +13,7 @@ /** * @template T + * * @extends HasOneOrMany */ class HasOne extends HasOneOrMany implements SupportsPartialRelations From d29f0f91099a6762639f7a01b227b70cfcb6f56c Mon Sep 17 00:00:00 2001 From: Ruslan Asanov Date: Mon, 3 Jun 2024 11:33:07 +0500 Subject: [PATCH 4/4] add compatibility with larastan --- .../Database/Eloquent/Relations/BelongsTo.php | 19 +++++++++-------- .../Database/Eloquent/Relations/MorphTo.php | 21 ++++++++++--------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php index dc6dcd60b323..153910fd011b 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php @@ -11,9 +11,10 @@ use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; /** - * @template T of \Illuminate\Database\Eloquent\Model + * @template TRelatedModel of \Illuminate\Database\Eloquent\Model + * @template TChildModel of \Illuminate\Database\Eloquent\Model * - * @extends Relation + * @extends Relation */ class BelongsTo extends Relation { @@ -24,7 +25,7 @@ class BelongsTo extends Relation /** * The child model instance of the relation. * - * @var T + * @var TChildModel */ protected $child; @@ -200,8 +201,8 @@ public function match(array $models, Collection $results, $relation) /** * Associate the model instance to the given parent. * - * @param T|int|string|null $model - * @return T + * @param TRelatedModel|int|string|null $model + * @return TChildModel */ public function associate($model) { @@ -221,7 +222,7 @@ public function associate($model) /** * Dissociate previously associated model from the given parent. * - * @return T + * @return TChildModel */ public function dissociate() { @@ -233,7 +234,7 @@ public function dissociate() /** * Alias of "dissociate" method. * - * @return T + * @return TChildModel */ public function disassociate() { @@ -295,7 +296,7 @@ protected function relationHasIncrementingId() * Make a new related instance for the given model. * * @param \Illuminate\Database\Eloquent\Model $parent - * @return T + * @return TRelatedModel */ protected function newRelatedInstanceFor(Model $parent) { @@ -305,7 +306,7 @@ protected function newRelatedInstanceFor(Model $parent) /** * Get the child of the relationship. * - * @return T + * @return TChildModel */ public function getChild() { diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index eb0fe747f741..4828f7be272e 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -9,9 +9,10 @@ use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; /** - * @template T of \Illuminate\Database\Eloquent\Model + * @template TRelatedModel of \Illuminate\Database\Eloquent\Model + * @template TChildModel of \Illuminate\Database\Eloquent\Model * - * @extends BelongsTo + * @extends BelongsTo */ class MorphTo extends BelongsTo { @@ -133,7 +134,7 @@ public function getEager() * Get all of the relation results for a type. * * @param string $type - * @return \Illuminate\Database\Eloquent\Collection + * @return \Illuminate\Database\Eloquent\Collection */ protected function getResultsByType($type) { @@ -182,7 +183,7 @@ protected function gatherKeysByType($type, $keyType) * Create a new model instance by type. * * @param string $type - * @return T + * @return TRelatedModel */ public function createModelByType($type) { @@ -232,7 +233,7 @@ protected function matchToMorphParents($type, Collection $results) * Associate the model instance to the given parent. * * @param \Illuminate\Database\Eloquent\Model|null $model - * @return T + * @return TRelatedModel */ public function associate($model) { @@ -256,7 +257,7 @@ public function associate($model) /** * Dissociate previously associated model from the given parent. * - * @return T + * @return TRelatedModel */ public function dissociate() { @@ -283,7 +284,7 @@ public function touch() * Make a new related instance for the given model. * * @param \Illuminate\Database\Eloquent\Model $parent - * @return T + * @return TRelatedModel */ protected function newRelatedInstanceFor(Model $parent) { @@ -314,7 +315,7 @@ public function getDictionary() * Specify which relations to load for a given morph type. * * @param array $with - * @return \Illuminate\Database\Eloquent\Relations\MorphTo + * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function morphWith(array $with) { @@ -329,7 +330,7 @@ public function morphWith(array $with) * Specify which relationship counts to load for a given morph type. * * @param array $withCount - * @return \Illuminate\Database\Eloquent\Relations\MorphTo + * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function morphWithCount(array $withCount) { @@ -344,7 +345,7 @@ public function morphWithCount(array $withCount) * Specify constraints on the query for a given morph type. * * @param array $callbacks - * @return \Illuminate\Database\Eloquent\Relations\MorphTo + * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function constrain(array $callbacks) {