ValidateCommand::execute()   B
last analyzed

Complexity

Conditions 11
Paths 82

Size

Total Lines 68
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 39
c 2
b 0
f 0
nc 82
nop 2
dl 0
loc 68
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace OckCyp\CoversValidator\Command;
4
5
use OckCyp\CoversValidator\Handler\InputHandler;
6
use OckCyp\CoversValidator\Loader\TestSuiteLoader;
7
use OckCyp\CoversValidator\Validator\Validator;
8
use PHPUnit\Framework\TestCase;
9
use PHPUnit\Framework\WarningTestCase;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class ValidateCommand extends Command
16
{
17
    /**
18
     * @var bool
19
     */
20
    protected $firstValidityWrite = true;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function configure()
26
    {
27
        $this->setName('validate')
28
            ->addOption(
29
                'configuration',
30
                'c',
31
                InputOption::VALUE_REQUIRED,
32
                'Read PHPUnit configuration from XML file.'
33
            )
34
            ->addUsage('-c app')
35
            ->addUsage('-c tests/configuration.xml')
36
            ->addOption(
37
                'bootstrap',
38
                null,
39
                InputOption::VALUE_REQUIRED,
40
                'A "bootstrap" PHP file that is run before the validation.'
41
            )
42
            ->addUsage('--bootstrap=tests/bootstrap.php');
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $output->writeln($this->getApplication()->getLongVersion());
51
52
        $configurationHolder = InputHandler::handleInput($input);
53
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
54
            $output->writeln(PHP_EOL.sprintf(
55
                'Configuration file loaded: %s',
56
                $configurationHolder->getFilename()
57
            ));
58
        }
59
60
        $testCollection = TestSuiteLoader::loadSuite($configurationHolder);
61
        if ($testCollection->isEmpty()) {
62
            $output->writeln(PHP_EOL.'No tests found to validate.');
63
64
            return 0;
65
        }
66
67
        $failedCount = 0;
68
        /** @var TestCase $suite */
69
        foreach ($testCollection as $suite) {
70
            if ($suite instanceof WarningTestCase) {
71
                continue;
72
            }
73
74
            $testClass = get_class($suite);
75
            if (method_exists($suite, 'getName')) {
76
                // PHPUnit < 10
77
                $testMethod = $suite->getName(false);
78
            } elseif (method_exists($suite, 'name')) {
79
                // PHPUnit >= 10.0
80
                $testMethod = $suite->name();
81
            } else {
82
                $testMethod = $suite->toString();
83
            }
84
            $testSignature = $testClass.'::'.$testMethod;
85
86
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
87
                $this->writeValidity($output, 'Validating '.$testSignature.'...');
88
            }
89
90
            $isValid = Validator::isValidMethod(
91
                $testClass,
92
                $testMethod
93
            );
94
95
            if (!$isValid) {
96
                ++$failedCount;
97
                $this->writeValidity($output, $testSignature, false);
98
            } elseif ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
99
                $this->writeValidity($output, $testSignature, true);
100
            }
101
        }
102
103
        $output->writeln('');
104
105
        if ($failedCount > 0) {
106
            $output->writeln(
107
                "There were {$failedCount} test(s) with invalid @covers tags."
108
            );
109
110
            return 1;
111
        }
112
113
        $output->writeln('Validation complete. All @covers tags are valid.');
114
115
        return 0;
116
    }
117
118
    /**
119
     * Write validity message for tests
120
     * The purpose of this method is to write a new line for the first
121
     * validity related message
122
     *
123
     * @param OutputInterface $output
124
     * @param string $message
125
     * @param bool|null $isValid
126
     */
127
    protected function writeValidity($output, $message, $isValid = null)
128
    {
129
        if ($this->firstValidityWrite) {
130
            $output->writeln('');
131
            $this->firstValidityWrite = false;
132
        }
133
134
        if (is_bool($isValid)) {
135
            $message = sprintf(
136
                '%s - %s',
137
                $isValid ? '<fg=green>Valid</>' : '<fg=red>Invalid</>',
138
                $message
139
            );
140
        }
141
142
        $output->writeln($message);
143
    }
144
}
145