1 | <?php |
||
10 | class GuardCoverageToolCommandHandler implements CommandHandlerInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var GuardCoverageTool |
||
14 | */ |
||
15 | private $guardCoverageTool; |
||
16 | |||
17 | /** |
||
18 | * GuardCoverageToolCommandHandler constructor. |
||
19 | * |
||
20 | * @param GuardCoverageTool $guardCoverageTool |
||
21 | */ |
||
22 | 2 | public function __construct(GuardCoverageTool $guardCoverageTool) |
|
26 | |||
27 | /** |
||
28 | * @param CommandInterface|GuardCoverageCommand $command |
||
29 | */ |
||
30 | 2 | public function handle(CommandInterface $command) |
|
34 | } |
||
35 |
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: