1 | <?php |
||
12 | class TimedTwigDecorator extends \Twig_Environment{ |
||
13 | /** |
||
14 | * @var \Twig_Environment |
||
15 | */ |
||
16 | private $environment; |
||
17 | /** |
||
18 | * @var Stopwatch |
||
19 | */ |
||
20 | private $stopwatch; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $serverName; |
||
26 | |||
27 | /** |
||
28 | * @param \Twig_Environment $environment |
||
29 | * @param Stopwatch $stopwatch |
||
30 | * @param string $serverName |
||
31 | */ |
||
32 | public function __construct(\Twig_Environment $environment, Stopwatch $stopwatch, $serverName = 'localhost') |
||
38 | |||
39 | /** |
||
40 | * Renders a template. |
||
41 | * |
||
42 | * @param string $name The template name |
||
43 | * @param array $context An array of parameters to pass to the template |
||
44 | * |
||
45 | * @return string The rendered template |
||
46 | * |
||
47 | * @throws \Twig_Error_Loader When the template cannot be found |
||
48 | * @throws \Twig_Error_Syntax When an error occurred during compilation |
||
49 | * @throws \Twig_Error_Runtime When an error occurred during rendering |
||
50 | */ |
||
51 | public function render($name, array $context = array()) |
||
66 | |||
67 | /** |
||
68 | * @return \Twig_Environment |
||
69 | */ |
||
70 | public function getEnvironment() |
||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | public function getLoader() |
||
82 | |||
83 | |||
84 | /** |
||
85 | * is triggered when invoking inaccessible methods in an object context. |
||
86 | * |
||
87 | * @param $name string |
||
88 | * @param $arguments array |
||
89 | * @return mixed |
||
90 | * @link http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods |
||
91 | */ |
||
92 | function __call($name, $arguments) |
||
96 | |||
97 | |||
98 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: