|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\PhpUnit\Contract\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandHandlerInterface; |
|
6
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandInterface; |
|
7
|
|
|
use PhpGitHooks\Module\Configuration\Domain\MinimumStrictCoverage; |
|
8
|
|
|
use PhpGitHooks\Module\Git\Service\PreCommitOutputWriter; |
|
9
|
|
|
use PhpGitHooks\Module\PhpUnit\Service\StrictCoverageTool; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
|
|
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) |
|
30
|
|
|
{ |
|
31
|
2 |
|
$this->output = $output; |
|
32
|
2 |
|
$this->strictCoverageTool = $strictCoverageTool; |
|
33
|
2 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param MinimumStrictCoverage $minimumStrictCoverage |
|
37
|
|
|
* @param string $errorMessage |
|
38
|
|
|
*/ |
|
39
|
2 |
|
private function execute(MinimumStrictCoverage $minimumStrictCoverage, $errorMessage) |
|
40
|
|
|
{ |
|
41
|
2 |
|
$outputMessage = new PreCommitOutputWriter(self::EXECUTE_MESSAGE); |
|
42
|
2 |
|
$this->output->write($outputMessage->getMessage()); |
|
43
|
2 |
|
$currentCoverage = $this->strictCoverageTool->run($minimumStrictCoverage, $errorMessage); |
|
44
|
1 |
|
$this->output->writeln($outputMessage->getSuccessfulMessage() . $this->printCurrentCoverage($currentCoverage)); |
|
45
|
1 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $currentCoverage |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
1 |
|
private function printCurrentCoverage($currentCoverage) |
|
52
|
|
|
{ |
|
53
|
1 |
|
return ' <comment>[' . round($currentCoverage, 0) . '%]</comment>'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param CommandInterface|StrictCoverage $command |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function handle(CommandInterface $command) |
|
60
|
|
|
{ |
|
61
|
2 |
|
$this->execute( |
|
62
|
2 |
|
new MinimumStrictCoverage($command->getMinimumCoverage()), |
|
|
|
|
|
|
63
|
2 |
|
$command->getErrorMessage() |
|
|
|
|
|
|
64
|
|
|
); |
|
65
|
1 |
|
} |
|
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: