1 | <?php |
||
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) |
||
41 | |||
42 | /** |
||
43 | * @return array |
||
44 | */ |
||
45 | public function getFunctions() |
||
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) |
||
75 | |||
76 | /** |
||
77 | * @param \Twig_Environment $twig |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function renderHead(\Twig_Environment $twig) |
||
85 | |||
86 | /** |
||
87 | * @param \Twig_Environment $twig |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | public function renderBody(\Twig_Environment $twig) |
||
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | public function getName() |
||
103 | |||
104 | /** |
||
105 | * @param $area |
||
106 | * @return string |
||
107 | */ |
||
108 | private function getTemplate($area) |
||
120 | |||
121 | /** |
||
122 | * @param \Twig_Environment $twig |
||
123 | * @param $area |
||
124 | * @return string |
||
125 | */ |
||
126 | private function getRenderedTemplate(\Twig_Environment $twig, $area) |
||
139 | } |
||
140 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.