TwigExtension   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 7
dl 0
loc 88
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A favicon() 0 4 2
A __construct() 0 6 1
A getFilters() 0 6 1
A getFunctions() 0 6 1
A widgets() 0 13 2
A getName() 0 4 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
namespace AnimeDb\Bundle\AppBundle\Service;
10
11
use Symfony\Bundle\FrameworkBundle\Routing\Router;
12
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
13
use Symfony\Component\HttpKernel\Controller\ControllerReference;
14
15
class TwigExtension extends \Twig_Extension
16
{
17
    /**
18
     * @var Router
19
     */
20
    private $router;
21
22
    /**
23
     * @var FragmentHandler
24
     */
25
    private $handler;
26
27
    /**
28
     * @var WidgetsContainer
29
     */
30
    private $widgets;
31
32
    /**
33
     * @param Router $router
34
     * @param FragmentHandler $handler
35
     * @param WidgetsContainer $widgets
36
     */
37
    public function __construct(Router $router, FragmentHandler $handler, WidgetsContainer $widgets)
38
    {
39
        $this->router = $router;
40
        $this->handler = $handler;
41
        $this->widgets = $widgets;
42
    }
43
44
    /**
45
     * @return array
46
     */
47
    public function getFilters()
48
    {
49
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('favicon' =...ay($this, 'favicon'))); (array<string,Twig_SimpleFilter>) is incompatible with the return type declared by the interface Twig_ExtensionInterface::getFilters of type Twig_SimpleFilter[].

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...
50
            'favicon' => new \Twig_SimpleFilter('favicon', [$this, 'favicon']),
51
        ];
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function getFunctions()
58
    {
59
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('widgets' =...e' => array('html')))); (array<string,Twig_SimpleFunction>) is incompatible with the return type declared by the interface Twig_ExtensionInterface::getFunctions of type Twig_SimpleFunction[].

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
            'widgets' => new \Twig_SimpleFunction('widgets', [$this, 'widgets'], ['is_safe' => ['html']]),
61
        ];
62
    }
63
64
    /**
65
     * @param string $url
66
     *
67
     * @return string|false
68
     */
69
    public function favicon($url)
70
    {
71
        return $url ? $this->router->generate('media_favicon', ['host' => parse_url($url, PHP_URL_HOST)]) : false;
72
    }
73
74
    /**
75
     * @param string $place
76
     * @param array $attributes
77
     * @param array $options
78
     *
79
     * @return string
80
     */
81
    public function widgets($place, array $attributes = [], array $options = [])
82
    {
83
        $result = '';
84
        foreach ($this->widgets->getWidgetsForPlace($place) as $controller) {
85
            $result .= $this->handler->render(
86
                new ControllerReference($controller, $attributes, []),
87
                'hinclude',
88
                $options
89
            );
90
        }
91
92
        return $result;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getName()
99
    {
100
        return 'anime_db_app_extension';
101
    }
102
}
103