Completed
Push — master ( b4c90e...febc05 )
by Alexander
03:46
created

Handler::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Flugg\Responder\Exceptions;
4
5
use Exception;
6
use Flugg\Responder\Traits\HandlesApiErrors;
7
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
8
9
class Handler extends ExceptionHandler
10
{
11
    use HandlesApiErrors;
12
13
    /**
14
     * Render an exception into an HTTP response.
15
     *
16
     * @param  \Illuminate\Http\Request $request
17
     * @param  Exception                $e
18
     * @return \Illuminate\Http\Response
19
     */
20
    public function render( $request, Exception $e )
21
    {
22
        if ( $e instanceof ApiException ) {
23
            return $this->renderApiErrors( $e );
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->renderApiErrors($e); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Flugg\Responder\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...
24
        }
25
26
        return parent::render( $request, $e );
27
    }
28
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
29