Handler::report()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Storgman\Core\Exceptions;
29
30
use Angelov\Storgman\Core\Exceptions\ResourceNotFoundException;
31
use Exception;
32
use Illuminate\Auth\Access\AuthorizationException;
33
use Illuminate\Contracts\Validation\ValidationException;
34
use Illuminate\Database\Eloquent\ModelNotFoundException;
35
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
36
use Illuminate\Http\JsonResponse;
37
use Symfony\Component\HttpKernel\Exception\HttpException;
38
39
class Handler extends ExceptionHandler
40
{
41
    /**
42
     * A list of the exception types that should not be reported.
43
     *
44
     * @var array
45
     */
46
    protected $dontReport = [
47
        AuthorizationException::class,
48
        HttpException::class,
49
        ModelNotFoundException::class,
50
        ValidationException::class,
51
    ];
52
53
    /**
54
     * Report or log an exception.
55
     *
56
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
57
     *
58
     * @param  \Exception  $e
59
     * @return void
60
     */
61
    public function report(Exception $e)
62
    {
63
        return ExceptionHandler::report($e);
64
    }
65
66
    /**
67
     * Render an exception into an HTTP response.
68
     *
69
     * @param  \Illuminate\Http\Request  $request
70
     * @param  \Exception  $e
71
     * @return \Illuminate\Http\Response
72
     */
73
    public function render($request, Exception $e)
74
    {
75
        if ($this->isHttpException($e)) {
76
            return $this->renderHttpException($e);
0 ignored issues
show
Compatibility introduced by
$e of type object<Exception> is not a sub-type of object<Symfony\Component...xception\HttpException>. It seems like you assume a child class of the class Exception to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
77
        }
78
79
        if ($e instanceof ResourceNotFoundException) {
80
            $data = [];
81
            $data['status'] = 'warning';
82
            $data['message'] = 'There was something wrong with your request.';
83
84
            return new JsonResponse($data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Illuminate\Http\JsonResponse($data); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Angelov\Storgman\Core\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...
85
        }
86
87
        return ExceptionHandler::render($request, $e);
88
    }
89
}
90