1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Paraunit\Configuration; |
6
|
|
|
|
7
|
|
|
use Paraunit\Configuration\DependencyInjection\CoverageContainerDefinition; |
8
|
|
|
use Paraunit\Coverage\CoverageResult; |
9
|
|
|
use Paraunit\Coverage\Processor\Clover; |
10
|
|
|
use Paraunit\Coverage\Processor\CoverageProcessorInterface; |
11
|
|
|
use Paraunit\Coverage\Processor\Crap4j; |
12
|
|
|
use Paraunit\Coverage\Processor\Html; |
13
|
|
|
use Paraunit\Coverage\Processor\Php; |
14
|
|
|
use Paraunit\Coverage\Processor\Text; |
15
|
|
|
use Paraunit\Coverage\Processor\TextToConsole; |
16
|
|
|
use Paraunit\Coverage\Processor\Xml; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
20
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
21
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class CoverageConfiguration |
25
|
|
|
* @package Paraunit\Configuration |
26
|
|
|
*/ |
27
|
|
|
class CoverageConfiguration extends ParallelConfiguration |
28
|
|
|
{ |
29
|
11 |
|
public function __construct(bool $createPublicServiceAliases = false) |
30
|
|
|
{ |
31
|
11 |
|
parent::__construct($createPublicServiceAliases); |
32
|
11 |
|
$this->containerDefinition = new CoverageContainerDefinition(); |
33
|
|
|
} |
34
|
|
|
|
35
|
11 |
|
protected function loadCommandLineOptions(ContainerBuilder $container, InputInterface $input) |
36
|
|
|
{ |
37
|
11 |
|
parent::loadCommandLineOptions($container, $input); |
38
|
|
|
|
39
|
11 |
|
$coverageResult = $container->getDefinition(CoverageResult::class); |
40
|
|
|
|
41
|
11 |
|
$this->addPathProcessor($coverageResult, $input, Xml::class); |
42
|
11 |
|
$this->addPathProcessor($coverageResult, $input, Html::class); |
43
|
|
|
|
44
|
11 |
|
$this->addFileProcessor($coverageResult, $input, Clover::class); |
45
|
11 |
|
$this->addFileProcessor($coverageResult, $input, Text::class); |
46
|
11 |
|
$this->addFileProcessor($coverageResult, $input, Crap4j::class); |
47
|
11 |
|
$this->addFileProcessor($coverageResult, $input, Php::class); |
48
|
|
|
|
49
|
11 |
|
if ($input->getOption('text-to-console')) { |
50
|
2 |
|
$this->addProcessor($coverageResult, TextToConsole::class, [ |
51
|
2 |
|
new Reference(OutputInterface::class), |
52
|
2 |
|
(bool) $input->getOption('ansi'), |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
8 |
|
private function addProcessor(Definition $coverageResult, string $processorClass, array $dependencies) |
58
|
|
|
{ |
59
|
8 |
|
$coverageResult->addMethodCall('addCoverageProcessor', [new Definition($processorClass, $dependencies)]); |
60
|
|
|
} |
61
|
|
|
|
62
|
11 |
View Code Duplication |
private function addFileProcessor( |
|
|
|
|
63
|
|
|
Definition $coverageResult, |
64
|
|
|
InputInterface $input, |
65
|
|
|
string $processorClass |
66
|
|
|
) { |
67
|
11 |
|
$optionName = $this->getOptionName($processorClass); |
68
|
|
|
|
69
|
11 |
|
if ($input->getOption($optionName)) { |
70
|
4 |
|
$this->addProcessor($coverageResult, $processorClass, [ |
71
|
4 |
|
$this->createOutputFileDefinition($input, $optionName), |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
11 |
View Code Duplication |
private function addPathProcessor( |
|
|
|
|
77
|
|
|
Definition $coverageResult, |
78
|
|
|
InputInterface $input, |
79
|
|
|
string $processorClass |
80
|
|
|
) { |
81
|
11 |
|
$optionName = $this->getOptionName($processorClass); |
82
|
|
|
|
83
|
11 |
|
if ($input->getOption($optionName)) { |
84
|
2 |
|
$this->addProcessor($coverageResult, $processorClass, [ |
85
|
2 |
|
$this->createOutputPathDefinition($input, $optionName), |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
4 |
|
private function createOutputFileDefinition(InputInterface $input, string $optionName): Definition |
91
|
|
|
{ |
92
|
4 |
|
return new Definition(OutputFile::class, [$input->getOption($optionName)]); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
private function createOutputPathDefinition(InputInterface $input, string $optionName): Definition |
96
|
|
|
{ |
97
|
2 |
|
return new Definition(OutputPath::class, [$input->getOption($optionName)]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $processorClass |
102
|
|
|
* @return string |
103
|
|
|
* @throws \InvalidArgumentException |
104
|
|
|
*/ |
105
|
11 |
|
private function getOptionName(string $processorClass): string |
106
|
|
|
{ |
107
|
11 |
|
if (! \in_array(CoverageProcessorInterface::class, class_implements($processorClass), true)) { |
108
|
|
|
throw new \InvalidArgumentException('Expecting FQCN of class implementing ' . CoverageProcessorInterface::class . ', got ' . $processorClass); |
109
|
|
|
} |
110
|
|
|
|
111
|
11 |
|
return $processorClass::getConsoleOptionName(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.