GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( acbf0a...123e6e )
by Philipp
01:59
created

GoogleTagManagerExtension::getRenderedTemplate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 2
1
<?php
2
/*
3
 * This file is part of the GoogleTagManagerBundle project
4
 *
5
 * (c) Philipp Braeutigam <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Xynnn\GoogleTagManagerBundle\Twig;
12
13
use Symfony\Component\Templating\Helper\HelperInterface;
14
use Twig_Extension;
15
use Xynnn\GoogleTagManagerBundle\Helper\GoogleTagManagerHelper;
16
use Xynnn\GoogleTagManagerBundle\Helper\GoogleTagManagerHelperInterface;
17
18
/**
19
 * Class GoogleTagManagerExtension
20
 *
21
 * @package Xynnn\GoogleTagManagerBundle\Extension
22
 */
23
class GoogleTagManagerExtension extends Twig_Extension
24
{
25
    const AREA_FULL = 'full';
26
    const AREA_HEAD = 'head';
27
    const AREA_BODY = 'body';
28
29
    /**
30
     * @var GoogleTagManagerHelperInterface
31
     */
32
    private $helper;
33
34
    /**
35
     * @param GoogleTagManagerHelperInterface $helper
36
     */
37
    public function __construct(GoogleTagManagerHelperInterface $helper)
38
    {
39
        $this->helper = $helper;
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function getFunctions()
46
    {
47
        return array(
48
            new \Twig_SimpleFunction('google_tag_manager', array($this, 'render'), array(
49
                'is_safe' => array('html'),
50
                'needs_environment' => true,
51
                'deprecated' => true,
52
            )),
53
            new \Twig_SimpleFunction('google_tag_manager_body', array($this, 'renderBody'), array(
54
                'is_safe' => array('html'),
55
                'needs_environment' => true,
56
            )),
57
            new \Twig_SimpleFunction('google_tag_manager_head', array($this, 'renderHead'), array(
58
                'is_safe' => array('html'),
59
                'needs_environment' => true,
60
            )),
61
        );
62
    }
63
64
    /**
65
     * @param \Twig_Environment $twig
66
     *
67
     * @deprecated Use `renderHead` and `renderBody`
68
     *
69
     * @return string
70
     */
71
    public function render(\Twig_Environment $twig)
72
    {
73
        return $this->getRenderedTemplate($twig, self::AREA_FULL);
74
    }
75
76
    /**
77
     * @param \Twig_Environment $twig
78
     *
79
     * @return string
80
     */
81
    public function renderHead(\Twig_Environment $twig)
82
    {
83
        return $this->getRenderedTemplate($twig, self::AREA_HEAD);
84
    }
85
86
    /**
87
     * @param \Twig_Environment $twig
88
     *
89
     * @return string
90
     */
91
    public function renderBody(\Twig_Environment $twig)
92
    {
93
        return $this->getRenderedTemplate($twig, self::AREA_BODY);
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getName()
100
    {
101
        return 'google_tag_manager';
102
    }
103
104
    /**
105
     * @param $area
106
     * @return string
107
     */
108
    private function getTemplate($area)
109
    {
110
        switch ($area) {
111
            case self::AREA_HEAD:
112
                return 'tagmanager_head';
113
            case self::AREA_BODY:
114
                return 'tagmanager_body';
115
            case self::AREA_FULL:
116
            default:
117
                return 'tagmanager';
118
        }
119
    }
120
121
    /**
122
     * @param \Twig_Environment $twig
123
     * @param $area
124
     * @return string
125
     */
126
    private function getRenderedTemplate(\Twig_Environment $twig, $area)
127
    {
128
        if (!$this->helper->isEnabled()) {
129
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Xynnn\GoogleTagManagerBu...on::getRenderedTemplate of type string.

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...
130
        }
131
132
        return $twig->render(
133
            'GoogleTagManagerBundle::' . $this->getTemplate($area) . '.html.twig', array(
134
                'id' => $this->helper->getId(),
135
                'data' => $this->helper->hasData() ? $this->helper->getData() : null
136
            )
137
        );
138
    }
139
}
140