1 | <?php |
||
14 | class GuardCoverageToolHandler implements CommandHandlerInterface |
||
15 | { |
||
16 | const CHECKING_MESSAGE = 'Checking your current coverage'; |
||
17 | /** |
||
18 | * @var OutputInterface |
||
19 | */ |
||
20 | private $output; |
||
21 | /** |
||
22 | * @var StrictCoverageProcessorInterface |
||
23 | */ |
||
24 | private $strictCoverageProcessor; |
||
25 | /** |
||
26 | * @var GuardCoverageFileReaderInterface |
||
27 | */ |
||
28 | private $guardReader; |
||
29 | /** |
||
30 | * @var GuardCoverageFileWriterInterface |
||
31 | */ |
||
32 | private $guardWriter; |
||
33 | /** |
||
34 | * @var float; |
||
35 | */ |
||
36 | private $currentCoverage; |
||
37 | /** |
||
38 | * @var float |
||
39 | */ |
||
40 | private $previousCoverage; |
||
41 | |||
42 | /** |
||
43 | * GuardCoverageTool constructor. |
||
44 | * |
||
45 | * @param OutputInterface $output |
||
46 | * @param StrictCoverageProcessorInterface $strictCoverageProcessor |
||
47 | * @param GuardCoverageFileReaderInterface $guardReader |
||
48 | * @param GuardCoverageFileWriterInterface $guardWriter |
||
49 | */ |
||
50 | 2 | public function __construct( |
|
61 | |||
62 | /** |
||
63 | * @param string $warningMessage |
||
64 | */ |
||
65 | 2 | private function run($warningMessage) |
|
66 | { |
||
67 | 2 | $outputMessage = new PreCommitOutputWriter(self::CHECKING_MESSAGE); |
|
68 | 2 | $this->output->write($outputMessage->getMessage()); |
|
69 | |||
70 | 2 | $this->currentCoverage = $this->strictCoverageProcessor->process(); |
|
71 | 2 | $this->previousCoverage = $this->guardReader->read(); |
|
72 | |||
73 | |||
74 | 2 | true === $this->isLowerCurrentCoverage() ? $this->output->writeln( |
|
75 | 1 | sprintf( |
|
76 | 1 | "\n<bg=yellow;options=bold>%s Previous coverage %s, current coverage %s.</>", |
|
77 | 1 | $warningMessage, |
|
78 | 1 | $this->previousCoverage, |
|
79 | 1 | $this->currentCoverage |
|
80 | ) |
||
81 | 1 | ) : $this->output->writeln( |
|
82 | 1 | $outputMessage->getSuccessfulMessage() . $this->printGuardCoverage() |
|
83 | ); |
||
84 | |||
85 | 2 | $this->guardWriter->write($this->currentCoverage); |
|
86 | 2 | } |
|
87 | |||
88 | /** |
||
89 | * @return bool |
||
90 | */ |
||
91 | 2 | private function isLowerCurrentCoverage() |
|
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | 1 | private function printGuardCoverage() |
|
107 | |||
108 | /** |
||
109 | * @param CommandInterface|GuardCoverage $command |
||
110 | */ |
||
111 | 2 | public function handle(CommandInterface $command) |
|
115 | } |
||
116 |
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: