Completed
Push — master ( 6ad044...8a4f85 )
by Sherif
03:22
created

Handler::report()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Exceptions;
4
5
use Exception;
6
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
7
8
class Handler extends ExceptionHandler
9
{
10
    /**
11
     * A list of the exception types that are not reported.
12
     *
13
     * @var array
14
     */
15
    protected $dontReport = [
16
        \League\OAuth2\Server\Exception\OAuthServerException::class,
17
    ];
18
19
    /**
20
     * A list of the inputs that are never flashed for validation exceptions.
21
     *
22
     * @var array
23
     */
24
    protected $dontFlash = [
25
        'password',
26
        'password_confirmation',
27
    ];
28
29
    /**
30
     * Report or log an exception.
31
     *
32
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
33
     *
34
     * @param  \Exception  $exception
35
     * @return void
36
     */
37
    public function report(Exception $exception)
38
    {
39
        parent::report($exception);
40
    }
41
42
    /**
43
     * Render an exception into an HTTP response.
44
     *
45
     * @param  \Illuminate\Http\Request  $request
46
     * @param  \Exception  $exception
47
     * @return \Illuminate\Http\Response
48
     */
49
    public function render($request, Exception $exception)
50
    {
51
        if ($request->wantsJson())
52
        {
53
            if ($exception instanceof \Illuminate\Auth\AuthenticationException) 
54
            {
55
                \ErrorHandler::unAuthorized();
56
            }
57
            if ($exception instanceof \Illuminate\Database\QueryException) 
58
            {
59
                \ErrorHandler::dbQueryError();
60
            }
61
            else if ($exception instanceof \predis\connection\connectionexception) 
62
            {
63
                \ErrorHandler::redisNotRunning();
64
            }
65
            else if ($exception instanceof \GuzzleHttp\Exception\ClientException) 
66
            {
67
                \ErrorHandler::connectionError();
68
            }
69
            else if ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException) 
70
            {
71
                return \Response::json($exception->getMessage(), $exception->getStatusCode());   
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Response::json($...tion->getStatusCode()); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Exceptions\Handler::render of type Illuminate\Http\Response|null.

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...
72
            }
73
            else if ($exception instanceof \Illuminate\Validation\ValidationException) 
74
            {
75
                return \Response::json($exception->errors(), 422);   
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Response::json($...eption->errors(), 422); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Exceptions\Handler::render of type Illuminate\Http\Response|null.

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...
76
            }
77
            else if ( ! $exception instanceof \Symfony\Component\Debug\Exception\FatalErrorException)
78
            {
79
                return parent::render($request, $exception);
80
            }
81
        }
82
        else
83
        {
84
            return parent::render($request, $exception);
85
        }
86
    }
87
}
88