From 788a18f1603265e7826dd673e78c0f97d8888c4f Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 17 Feb 2022 14:56:24 +0000 Subject: [PATCH] Makes `ModelNotFoundException` generic (#41082) Co-Authored-By: Benedikt Franke Co-authored-by: Benedikt Franke --- src/Illuminate/Database/Eloquent/Builder.php | 8 ++++---- .../Database/Eloquent/ModelNotFoundException.php | 15 +++++++++------ .../Database/Eloquent/Relations/BelongsToMany.php | 4 ++-- .../Eloquent/Relations/HasManyThrough.php | 4 ++-- .../Database/Eloquent/Relations/Relation.php | 2 +- src/Illuminate/Routing/ImplicitRouteBinding.php | 2 +- src/Illuminate/Routing/RouteBinding.php | 2 +- src/Illuminate/Routing/Router.php | 6 +++--- .../Database/Eloquent/ModelNotFoundException.php | 15 +++++++++++++++ 9 files changed, 38 insertions(+), 20 deletions(-) create mode 100644 types/Database/Eloquent/ModelNotFoundException.php diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 134c16002565..48967a5f7c9b 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -426,7 +426,7 @@ public function findMany($ids, $columns = ['*']) * @param array $columns * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static|static[] * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function findOrFail($id, $columns = ['*']) { @@ -523,7 +523,7 @@ public function updateOrCreate(array $attributes, array $values = []) * @param array $columns * @return \Illuminate\Database\Eloquent\Model|static * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function firstOrFail($columns = ['*']) { @@ -562,7 +562,7 @@ public function firstOr($columns = ['*'], Closure $callback = null) * @param array|string $columns * @return \Illuminate\Database\Eloquent\Model * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> * @throws \Illuminate\Database\MultipleRecordsFoundException */ public function sole($columns = ['*']) @@ -593,7 +593,7 @@ public function value($column) * @param string|\Illuminate\Database\Query\Expression $column * @return mixed * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function valueOrFail($column) { diff --git a/src/Illuminate/Database/Eloquent/ModelNotFoundException.php b/src/Illuminate/Database/Eloquent/ModelNotFoundException.php index c35598bdbf46..79ae8a39de9e 100755 --- a/src/Illuminate/Database/Eloquent/ModelNotFoundException.php +++ b/src/Illuminate/Database/Eloquent/ModelNotFoundException.php @@ -5,27 +5,30 @@ use Illuminate\Database\RecordsNotFoundException; use Illuminate\Support\Arr; +/** + * @template TModel of \Illuminate\Database\Eloquent\Model + */ class ModelNotFoundException extends RecordsNotFoundException { /** * Name of the affected Eloquent model. * - * @var string + * @var class-string */ protected $model; /** * The affected model IDs. * - * @var int|array + * @var array */ protected $ids; /** * Set the affected Eloquent model and instance ids. * - * @param string $model - * @param int|array $ids + * @param class-string $model + * @param array|int|string $ids * @return $this */ public function setModel($model, $ids = []) @@ -47,7 +50,7 @@ public function setModel($model, $ids = []) /** * Get the affected Eloquent model. * - * @return string + * @return class-string */ public function getModel() { @@ -57,7 +60,7 @@ public function getModel() /** * Get the affected Eloquent model IDs. * - * @return int|array + * @return array */ public function getIds() { diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index ee443c48155f..da4d1a8aa6bf 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -692,7 +692,7 @@ public function findMany($ids, $columns = ['*']) * @param array $columns * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function findOrFail($id, $columns = ['*']) { @@ -744,7 +744,7 @@ public function first($columns = ['*']) * @param array $columns * @return \Illuminate\Database\Eloquent\Model|static * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function firstOrFail($columns = ['*']) { diff --git a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php index 7af84b7b90f4..14baa48c03a6 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php @@ -291,7 +291,7 @@ public function first($columns = ['*']) * @param array $columns * @return \Illuminate\Database\Eloquent\Model|static * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function firstOrFail($columns = ['*']) { @@ -369,7 +369,7 @@ public function findMany($ids, $columns = ['*']) * @param array $columns * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function findOrFail($id, $columns = ['*']) { diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index 90356abaf862..bd2be0a451cc 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -164,7 +164,7 @@ public function getEager() * @param array|string $columns * @return \Illuminate\Database\Eloquent\Model * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> * @throws \Illuminate\Database\MultipleRecordsFoundException */ public function sole($columns = ['*']) diff --git a/src/Illuminate/Routing/ImplicitRouteBinding.php b/src/Illuminate/Routing/ImplicitRouteBinding.php index 2a643fcd28e4..b0baab15bf5f 100644 --- a/src/Illuminate/Routing/ImplicitRouteBinding.php +++ b/src/Illuminate/Routing/ImplicitRouteBinding.php @@ -18,7 +18,7 @@ class ImplicitRouteBinding * @param \Illuminate\Routing\Route $route * @return void * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> * @throws \Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException */ public static function resolveForRoute($container, $route) diff --git a/src/Illuminate/Routing/RouteBinding.php b/src/Illuminate/Routing/RouteBinding.php index 133a84a40b07..36fdc4f282f9 100644 --- a/src/Illuminate/Routing/RouteBinding.php +++ b/src/Illuminate/Routing/RouteBinding.php @@ -53,7 +53,7 @@ protected static function createClassBinding($container, $binding) * @param \Closure|null $callback * @return \Closure * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public static function forModel($container, $class, $callback = null) { diff --git a/src/Illuminate/Routing/Router.php b/src/Illuminate/Routing/Router.php index 22c5e00ad23c..772efa87aa5e 100644 --- a/src/Illuminate/Routing/Router.php +++ b/src/Illuminate/Routing/Router.php @@ -846,7 +846,7 @@ public static function toResponse($request, $response) * @param \Illuminate\Routing\Route $route * @return \Illuminate\Routing\Route * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> * @throws \Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException */ public function substituteBindings($route) @@ -866,7 +866,7 @@ public function substituteBindings($route) * @param \Illuminate\Routing\Route $route * @return void * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> * @throws \Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException */ public function substituteImplicitBindings($route) @@ -882,7 +882,7 @@ public function substituteImplicitBindings($route) * @param \Illuminate\Routing\Route $route * @return mixed * - * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ protected function performBinding($key, $value, $route) { diff --git a/types/Database/Eloquent/ModelNotFoundException.php b/types/Database/Eloquent/ModelNotFoundException.php new file mode 100644 index 000000000000..c9feffc13b1f --- /dev/null +++ b/types/Database/Eloquent/ModelNotFoundException.php @@ -0,0 +1,15 @@ + $exception */ +$exception = new ModelNotFoundException(); + + assertType('array', $exception->getIds()); + assertType('class-string', $exception->getModel()); + + $exception->setModel(User::class, 1); + $exception->setModel(User::class, [1]); + $exception->setModel(User::class, '1'); + $exception->setModel(User::class, ['1']);