1 | <?php |
||
10 | class ImgUrl extends AbstractHelper |
||
11 | { |
||
12 | /** |
||
13 | * @var CacheManagerInterface |
||
14 | */ |
||
15 | protected $cacheManager; |
||
16 | |||
17 | /** |
||
18 | * @var FilterManagerInterface |
||
19 | */ |
||
20 | protected $filterManager; |
||
21 | |||
22 | /** |
||
23 | * @var LoaderManagerInterface |
||
24 | */ |
||
25 | protected $loaderManager; |
||
26 | |||
27 | /** |
||
28 | * Constructor |
||
29 | * |
||
30 | * @param CacheManagerInterface $cacheManager |
||
31 | * @param FilterManagerInterface $filterManager |
||
32 | * @param LoaderManagerInterface $loaderManager |
||
33 | */ |
||
34 | 2 | public function __construct( |
|
44 | |||
45 | /** |
||
46 | * Gets url of image |
||
47 | * |
||
48 | * @param string $relativeName Relative Path |
||
49 | * @param string $filter Filter Service |
||
50 | * @return string |
||
51 | */ |
||
52 | 2 | public function __invoke($relativeName, $filter) |
|
71 | } |
||
72 |
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: