1 | <?php |
||
23 | class FormatterMediaExtension extends AbstractExtension implements ExtensionInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var ExtensionInterface |
||
27 | */ |
||
28 | protected $twigExtension; |
||
29 | |||
30 | public function __construct(ExtensionInterface $twigExtension) |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | public function getAllowedTags(): array |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function getAllowedMethods(): array |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | public function getTokenParsers(): array |
||
70 | |||
71 | /** |
||
72 | * @return ExtensionInterface |
||
73 | */ |
||
74 | public function getTwigExtension(): ExtensionInterface |
||
78 | |||
79 | /** |
||
80 | * @param int $media |
||
81 | * @param string $format |
||
82 | * @param array $options |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | public function media($media, $format, $options = []) |
||
90 | |||
91 | /** |
||
92 | * @param int $media |
||
93 | * @param string $format |
||
94 | * @param array $options |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | public function thumbnail($media, $format, $options = []) |
||
102 | |||
103 | /** |
||
104 | * @param int $media |
||
105 | * @param string $format |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | public function path($media, $format) |
||
113 | |||
114 | public function getNodeVisitors() |
||
118 | |||
119 | public function getFilters() |
||
123 | |||
124 | public function getTests() |
||
128 | |||
129 | public function getFunctions() |
||
133 | |||
134 | public function getOperators() |
||
138 | |||
139 | public function getAllowedFilters() |
||
143 | |||
144 | public function getAllowedFunctions() |
||
148 | |||
149 | public function getAllowedProperties() |
||
153 | } |
||
154 |
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 implementation 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 interface: