1 | <?php |
||
13 | class PhpLintToolHandler implements CommandHandlerInterface |
||
14 | { |
||
15 | const RUNNING_PHPLINT = 'Running PHPLINT'; |
||
16 | /** |
||
17 | * @var PhpLintToolProcessorInterface |
||
18 | */ |
||
19 | private $phpLintTool; |
||
20 | /** |
||
21 | * @var OutputInterface |
||
22 | */ |
||
23 | private $output; |
||
24 | |||
25 | /** |
||
26 | * PhpLintTool constructor. |
||
27 | * |
||
28 | * @param PhpLintToolProcessorInterface $phpLintTool |
||
29 | * @param OutputInterface $output |
||
30 | */ |
||
31 | 2 | public function __construct(PhpLintToolProcessorInterface $phpLintTool, OutputInterface $output) |
|
36 | |||
37 | /** |
||
38 | * @param array $files |
||
39 | * @param string $errorMessage |
||
40 | * |
||
41 | * @throws PhpLintViolationsException |
||
42 | */ |
||
43 | 2 | private function execute(array $files, $errorMessage) |
|
65 | |||
66 | /** |
||
67 | * @param CommandInterface|PhpLintTool $command |
||
68 | */ |
||
69 | 2 | public function handle(CommandInterface $command) |
|
73 | } |
||
74 |
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: