Conditions | 5 |
Paths | 5 |
Total Lines | 21 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
45 | public function offsetGet($offset) |
||
46 | { |
||
47 | if ($this->offsetExists($offset)) { |
||
48 | if (isset($this->entity->$offset)) { |
||
49 | $values = $this->entity->$offset; |
||
50 | $cardinality = $values->getDataDefinition()->getFieldStorageDefinition()->getCardinality(); |
||
51 | if (1 === $cardinality) { |
||
52 | if ($values[0] === null) { |
||
53 | return null; |
||
54 | } |
||
55 | |||
56 | return $this->entityWrapper->convert($values[0]); |
||
57 | } else { |
||
58 | return array_map([$this->entityWrapper, 'convert'], iterator_to_array($values)); |
||
59 | } |
||
60 | } |
||
61 | } else { |
||
62 | return null; |
||
63 | } |
||
64 | throw new \Exception('The index "'.$offset.'" doesn\'t exist'); |
||
65 | } |
||
66 | |||
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: