1 | <?php |
||
12 | class StrictCoverageToolHandler implements CommandHandlerInterface |
||
13 | { |
||
14 | const EXECUTE_MESSAGE = 'Checking minimum coverage'; |
||
15 | /** |
||
16 | * @var OutputInterface |
||
17 | */ |
||
18 | private $output; |
||
19 | /** |
||
20 | * @var StrictCoverageTool |
||
21 | */ |
||
22 | private $strictCoverageTool; |
||
23 | |||
24 | /** |
||
25 | * StrictCoverageToolExecutor constructor. |
||
26 | * @param OutputInterface $output |
||
27 | * @param StrictCoverageTool $strictCoverageTool |
||
28 | */ |
||
29 | 2 | public function __construct(OutputInterface $output, StrictCoverageTool $strictCoverageTool) |
|
34 | |||
35 | /** |
||
36 | * @param MinimumStrictCoverage $minimumStrictCoverage |
||
37 | * @param string $errorMessage |
||
38 | */ |
||
39 | 2 | private function execute(MinimumStrictCoverage $minimumStrictCoverage, $errorMessage) |
|
46 | |||
47 | /** |
||
48 | * @param string $currentCoverage |
||
49 | * @return string |
||
50 | */ |
||
51 | 1 | private function printCurrentCoverage($currentCoverage) |
|
55 | |||
56 | /** |
||
57 | * @param CommandInterface|StrictCoverage $command |
||
58 | */ |
||
59 | 2 | public function handle(CommandInterface $command) |
|
66 | } |
||
67 |
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: