1 | <?php |
||
16 | class ImageItemConverter implements ConverterInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var EntityWrapper |
||
20 | */ |
||
21 | private $entityWrapper; |
||
22 | |||
23 | /** |
||
24 | * @var EntityTypeManagerInterface |
||
25 | */ |
||
26 | private $entityManager; |
||
27 | |||
28 | /** |
||
29 | * @param EntityWrapper $entityWrapper |
||
30 | * @param EntityTypeManagerInterface $entityManager |
||
31 | */ |
||
32 | public function __construct(EntityWrapper $entityWrapper, |
||
38 | |||
39 | /** |
||
40 | * @param FieldItemInterface $value |
||
41 | */ |
||
42 | public function convert(FieldItemInterface $value) |
||
65 | |||
66 | /** |
||
67 | * Returns whether the FieldItemInterface can be converted or not. |
||
68 | * |
||
69 | * @param FieldItemInterface $value |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function canConvert(FieldItemInterface $value) : bool |
||
77 | } |
||
78 |
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: