| 1 | <?php |
||
| 12 | class EasyEntityAdapter implements \ArrayAccess |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var EntityInterface |
||
| 16 | */ |
||
| 17 | private $entity; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var EntityWrapper |
||
| 21 | */ |
||
| 22 | private $entityWrapper; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param EntityInterface $entity |
||
| 26 | * @param EntityWrapper $entityWrapper |
||
| 27 | */ |
||
| 28 | public function __construct(EntityInterface $entity, EntityWrapper $entityWrapper) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param $offset |
||
| 36 | */ |
||
| 37 | public function offsetExists($offset) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param $offset |
||
| 44 | */ |
||
| 45 | public function offsetGet($offset) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param $offset |
||
| 69 | * @param $value |
||
| 70 | */ |
||
| 71 | public function offsetSet($offset, $value) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param $offset |
||
| 78 | */ |
||
| 79 | public function offsetUnset($offset) |
||
| 83 | } |
||
| 84 |
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: