Completed
Push — master ( c8ea4b...c4213a )
by Sherif
02:51
created

Handler::render()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 45
rs 4.909
cc 9
eloc 24
nc 16
nop 2
1
<?php
2
3
namespace App\Exceptions;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\ModelNotFoundException;
7
use Symfony\Component\HttpKernel\Exception\HttpException;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
10
11
class Handler extends ExceptionHandler
12
{
13
    /**
14
     * A list of the exception types that should not be reported.
15
     *
16
     * @var array
17
     */
18
    protected $dontReport = [
19
        HttpException::class,
20
        ModelNotFoundException::class,
21
    ];
22
23
    /**
24
     * Report or log an exception.
25
     *
26
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
27
     *
28
     * @param  \Exception  $e
29
     * @return void
30
     */
31
    public function report(Exception $e)
32
    {
33
        return parent::report($e);
34
    }
35
36
    /**
37
     * Render an exception into an HTTP response.
38
     *
39
     * @param  \Illuminate\Http\Request  $request
40
     * @param  \Exception  $e
41
     * @return \Illuminate\Http\Response
42
     */
43
    public function render($request, Exception $e)
44
    {
45
        if ($e instanceof ModelNotFoundException) {
46
            $e = new NotFoundHttpException($e->getMessage(), $e);
47
        }
48
49
        if ($request->wantsJson())
50
        {
51
            if ($e instanceof \Illuminate\Database\QueryException) 
52
            {
53
                $error = \ErrorHandler::dbQueryError();
54
                return \Response::json($error['message'], $error['status']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Response::json($...e'], $error['status']); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Exceptions\Handler::render of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
55
            }
56
            else if ($e instanceof \predis\connection\connectionexception) 
57
            {
58
                $error = \ErrorHandler::redisNotRunning();
59
                return \Response::json($error['message'], $error['status']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Response::json($...e'], $error['status']); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Exceptions\Handler::render of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
60
            }
61
            else if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException) 
62
            {
63
                $error = \ErrorHandler::tokenExpired();
64
                return \Response::json($error['message'], $error['status']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Response::json($...e'], $error['status']); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Exceptions\Handler::render of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
65
            } 
66
            else if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException) 
67
            {
68
                $error = \ErrorHandler::noPermissions();
69
                return \Response::json($error['message'], $error['status']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Response::json($...e'], $error['status']); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Exceptions\Handler::render of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
70
            }
71
            else if ($e instanceof \Tymon\JWTAuth\Exceptions\JWTException) 
72
            {
73
                $error = \ErrorHandler::unAuthorized();
74
                return \Response::json($error['message'], $error['status']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Response::json($...e'], $error['status']); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Exceptions\Handler::render of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
75
            }
76
            else if ($e instanceof HttpException) 
77
            {
78
                return \Response::json($e->getMessage(), $e->getStatusCode());   
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Response::json($..., $e->getStatusCode()); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Exceptions\Handler::render of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
79
            }
80
            else
81
            {
82
                return parent::render($request, $e);
83
            }
84
        }
85
86
        return parent::render($request, $e);
87
    }
88
}
89