| 1 | <?php |
||
| 13 | class RequestPasswordResetHandler implements Handler |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var UserRepository |
||
| 17 | */ |
||
| 18 | private $userRepository; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param UserRepository $userRepository |
||
| 22 | */ |
||
| 23 | public function __construct(UserRepository $userRepository) |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param Command|RequestPasswordReset $command |
||
| 30 | */ |
||
| 31 | public function handle(Command $command) |
||
| 37 | } |
||
| 38 |
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: