| 1 | <?php |
||
| 15 | class EntityReferenceConverter implements ConverterInterface |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var EntityWrapper |
||
| 19 | */ |
||
| 20 | private $entityWrapper; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var EntityTypeManagerInterface |
||
| 24 | */ |
||
| 25 | private $entityManager; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param EntityWrapper $entityWrapper |
||
| 29 | * @param EntityTypeManagerInterface $entityManager |
||
| 30 | */ |
||
| 31 | public function __construct(EntityWrapper $entityWrapper, |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param FieldItemInterface $value |
||
| 40 | */ |
||
| 41 | public function convert(FieldItemInterface $value) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Returns whether the FieldItemInterface can be converted or not. |
||
| 51 | * |
||
| 52 | * @param FieldItemInterface $value |
||
| 53 | * |
||
| 54 | * @return bool |
||
| 55 | */ |
||
| 56 | public function canConvert(FieldItemInterface $value) : bool |
||
| 60 | } |
||
| 61 |
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: