Completed
Push — master ( 759a3c...0a5dc9 )
by Mohamed
04:06
created

ExceptionController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 50%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 0
cbo 2
dl 0
loc 25
ccs 3
cts 6
cp 0.5
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A findTemplate() 0 12 3
1
<?php
2
3
/*
4
 * This file is part of the Moo\FlashCardBundle package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Moo\FlashCardBundle\Controller;
13
14
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
17
18
/**
19
 * ExceptionController is the exception controller to provides specific error page for the bundle pages.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 */
23
class ExceptionController extends BaseExceptionController
24
{
25
    /**
26
     * Override to provide a custom 404 error page for the current bundle
27
     *
28
     * @param Request $request
29
     * @param string  $format
30
     * @param int     $code    An HTTP response status code
31
     * @param Boolean $debug
32
     *
33
     * @return TemplateReference
34
     */
35 1
    protected function findTemplate(Request $request, $format, $code, $debug)
36
    {
37
        // bundle specific error page for production env.
38 1
        if (!$debug) {
39
            $template = new TemplateReference('MooFlashCardBundle', 'Exception', 'error', $format, 'twig');
40
            if ($this->templateExists($template)) {
41
                return $template;
42
            }
43
        }
44
45 1
        return parent::findTemplate($request, $format, $code, $debug);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::findTempl...format, $code, $debug); (string) is incompatible with the return type documented by Moo\FlashCardBundle\Cont...ontroller::findTemplate of type Symfony\Bundle\Framework...ating\TemplateReference.

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...
46
    }
47
}
48