| 1 | <?php |
||
| 14 | final class I18nResponseErrorFormatter |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | private $dictionary; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * I18nResponseErrorFormatter constructor. |
||
| 23 | * @param string $dictionary |
||
| 24 | */ |
||
| 25 | public function __construct(string $dictionary = 'hipanel') |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @inheritDoc |
||
| 32 | */ |
||
| 33 | public function __invoke(Exception $exception): string |
||
| 44 | } |
||
| 45 |
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 sub-classes 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 parent class: