Completed
Push — master ( aab589...178fab )
by Alessandro
12s
created

CoverageConfiguration::optionIsEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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\TextSummary;
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 12
    public function __construct(bool $createPublicServiceAliases = false)
30
    {
31 12
        parent::__construct($createPublicServiceAliases);
32 12
        $this->containerDefinition = new CoverageContainerDefinition();
33
    }
34
35 12
    protected function loadCommandLineOptions(ContainerBuilder $container, InputInterface $input)
36
    {
37 12
        parent::loadCommandLineOptions($container, $input);
38
39 12
        $coverageResult = $container->getDefinition(CoverageResult::class);
40
41 12
        $this->addPathProcessor($coverageResult, $input, Xml::class);
42 12
        $this->addPathProcessor($coverageResult, $input, Html::class);
43
44 12
        $this->addFileProcessor($coverageResult, $input, Clover::class);
45 12
        $this->addFileOrOutputProcessor($coverageResult, $input, Text::class);
46 12
        $this->addFileOrOutputProcessor($coverageResult, $input, TextSummary::class);
47 12
        $this->addFileProcessor($coverageResult, $input, Crap4j::class);
48 12
        $this->addFileProcessor($coverageResult, $input, Php::class);
49
    }
50
51 9
    private function addProcessor(Definition $coverageResult, string $processorClass, array $dependencies)
52
    {
53 9
        $coverageResult->addMethodCall('addCoverageProcessor', [new Definition($processorClass, $dependencies)]);
54
    }
55
56 12 View Code Duplication
    private function addFileProcessor(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
57
        Definition $coverageResult,
58
        InputInterface $input,
59
        string $processorClass
60
    ) {
61 12
        $optionName = $this->getOptionName($processorClass);
62
63 12
        if ($input->getOption($optionName)) {
64 3
            $this->addProcessor($coverageResult, $processorClass, [
65 3
                $this->createOutputFileDefinition($input, $optionName),
66 3
                (bool) $input->getOption('ansi'),
67
            ]);
68
        }
69
    }
70
71 12 View Code Duplication
    private function addFileOrOutputProcessor(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
72
        Definition $coverageResult,
73
        InputInterface $input,
74
        string $processorClass
75
    ) {
76 12
        $optionName = $this->getOptionName($processorClass);
77
78 12
        if ($this->optionIsEnabled($input, $optionName)) {
79 4
            $this->addProcessor($coverageResult, $processorClass, [
80 4
                new Reference(OutputInterface::class),
81 4
                (bool) $input->getOption('ansi'),
82 4
                $this->createOutputFileDefinition($input, $optionName),
83
            ]);
84
        }
85
    }
86
87 12 View Code Duplication
    private function addPathProcessor(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
88
        Definition $coverageResult,
89
        InputInterface $input,
90
        string $processorClass
91
    ) {
92 12
        $optionName = $this->getOptionName($processorClass);
93
94 12
        if ($this->optionIsEnabled($input, $optionName)) {
95 2
            $this->addProcessor($coverageResult, $processorClass, [
96 2
                $this->createOutputPathDefinition($input, $optionName),
97
            ]);
98
        }
99
    }
100
101
    /**
102
     * @param InputInterface $input
103
     * @param string $optionName
104
     * @return null|Definition
105
     */
106 7
    private function createOutputFileDefinition(InputInterface $input, string $optionName)
107
    {
108 7
        if ($this->optionIsEnabled($input, $optionName)) {
109 7
            return new Definition(OutputFile::class, [$input->getOption($optionName)]);
110
        }
111
112
        return null;
113
    }
114
115 2
    private function createOutputPathDefinition(InputInterface $input, string $optionName): Definition
116
    {
117 2
        return new Definition(OutputPath::class, [$input->getOption($optionName)]);
118
    }
119
120
    /**
121
     * @param string $processorClass
122
     * @return string
123
     * @throws \InvalidArgumentException
124
     */
125 12
    private function getOptionName(string $processorClass): string
126
    {
127 12
        if (! \in_array(CoverageProcessorInterface::class, class_implements($processorClass), true)) {
128
            throw new \InvalidArgumentException('Expecting FQCN of class implementing ' . CoverageProcessorInterface::class . ', got ' . $processorClass);
129
        }
130
131 12
        return $processorClass::getConsoleOptionName();
132
    }
133
134 12
    private function optionIsEnabled(InputInterface $input, string $optionName): bool
135
    {
136 12
        return $input->hasParameterOption('--' . $optionName);
137
    }
138
}
139