Passed
Pull Request — development (#671)
by Nick
06:47
created

CodeCoverageCommand::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 2
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
/***************************************************************************
3
 * for license information see LICENSE.md
4
 ***************************************************************************/
5
6
namespace Oc\Command;
7
8
use InvalidArgumentException;
9
use SimpleXMLElement;
10
use Symfony\Component\Console\Exception\InvalidArgumentException as ConsoleInvalidArgumentException;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Class CodeCoverageCommand
16
 *
17
 * @package Oc\Command
18
 */
19
class CodeCoverageCommand extends AbstractCommand
20
{
21
    const COMMAND_NAME = 'build:code-coverage';
22
23
    /**
24
     * Configures the command.
25
     *
26
     * @return void
27
     *
28
     * @throws ConsoleInvalidArgumentException
29
     */
30
    protected function configure()
31
    {
32
        parent::configure();
33
34
        $this
35
            ->setName(self::COMMAND_NAME)
36
            ->setDescription('display the code coverage from the last phpunit run');
37
    }
38
39
    /**
40
     * Executes the command.
41
     *
42
     * @param InputInterface $input
43
     * @param OutputInterface $output
44
     *
45
     * @return int|null
46
     *
47
     * @throws InvalidArgumentException
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $inputFile = __DIR__ . '/../../../../build/logs/clover.xml';
52
53
        if (!file_exists($inputFile)) {
54
            throw new InvalidArgumentException('Invalid input file provided');
55
        }
56
57
        $xml = new SimpleXMLElement(file_get_contents($inputFile));
58
        $metrics = $xml->xpath('//metrics');
59
        $totalElements = 0;
60
        $checkedElements = 0;
61
62
        foreach ($metrics as $metric) {
63
            $totalElements += (int)$metric['elements'];
64
            $checkedElements += (int)$metric['coveredelements'];
65
        }
66
67
        $coverage = ($checkedElements / $totalElements) * 100;
68
69
        $output->writeln('Code coverage is ' . $coverage . '%');
70
71
        return self::CODE_SUCCESS;
72
    }
73
}
74