Skip to content

Commit

Permalink
Makes ModelNotFoundException generic (#41082)
Browse files Browse the repository at this point in the history
Co-Authored-By: Benedikt Franke <[email protected]>

Co-authored-by: Benedikt Franke <[email protected]>
  • Loading branch information
nunomaduro and spawnia authored Feb 17, 2022
1 parent 7d37bd1 commit 788a18f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ['*'])
{
Expand Down Expand Up @@ -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 = ['*'])
{
Expand Down Expand Up @@ -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 = ['*'])
Expand Down Expand Up @@ -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)
{
Expand Down
15 changes: 9 additions & 6 deletions src/Illuminate/Database/Eloquent/ModelNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<TModel>
*/
protected $model;

/**
* The affected model IDs.
*
* @var int|array
* @var array<int, int|string>
*/
protected $ids;

/**
* Set the affected Eloquent model and instance ids.
*
* @param string $model
* @param int|array $ids
* @param class-string<TModel> $model
* @param array<int, int|string>|int|string $ids
* @return $this
*/
public function setModel($model, $ids = [])
Expand All @@ -47,7 +50,7 @@ public function setModel($model, $ids = [])
/**
* Get the affected Eloquent model.
*
* @return string
* @return class-string<TModel>
*/
public function getModel()
{
Expand All @@ -57,7 +60,7 @@ public function getModel()
/**
* Get the affected Eloquent model IDs.
*
* @return int|array
* @return array<int, int|string>
*/
public function getIds()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ['*'])
{
Expand Down Expand Up @@ -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 = ['*'])
{
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ['*'])
{
Expand Down Expand Up @@ -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 = ['*'])
{
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ['*'])
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/ImplicitRouteBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/RouteBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
{
Expand Down
15 changes: 15 additions & 0 deletions types/Database/Eloquent/ModelNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use Illuminate\Database\Eloquent\ModelNotFoundException;
use function PHPStan\Testing\assertType;

/** @var ModelNotFoundException<User> $exception */
$exception = new ModelNotFoundException();

assertType('array<int, int|string>', $exception->getIds());
assertType('class-string<User>', $exception->getModel());

$exception->setModel(User::class, 1);
$exception->setModel(User::class, [1]);
$exception->setModel(User::class, '1');
$exception->setModel(User::class, ['1']);

0 comments on commit 788a18f

Please sign in to comment.