Passed
Push — master ( 69774c...de7969 )
by Sebastian
02:46
created

CloverXML   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 16
c 0
b 0
f 0
dl 0
loc 55
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateXml() 0 4 3
A __construct() 0 8 2
A getCoverage() 0 13 3
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Hook\PHP\CoverageResolver;
13
14
use RuntimeException;
15
use CaptainHook\App\Hook\PHP\CoverageResolver;
16
use CaptainHook\App\Storage\File\Xml;
17
18
/**
19
 * Class CloverXML
20
 *
21
 * @package CaptainHook
22
 * @author  Sebastian Feldmann <[email protected]>
23
 * @link    https://github.com/captainhookphp/captainhook
24
 * @since   Class available since Release 1.2.0
25
 */
26
class CloverXML implements CoverageResolver
27
{
28
    /**
29
     * Clover XML
30
     *
31
     * @var \SimpleXMLElement
32
     */
33
    private $xml;
34
35
    /**
36
     * CloverXML constructor.
37
     *
38 6
     * @param string $pathToCloverXml
39
     */
40 6
    public function __construct($pathToCloverXml)
41 6
    {
42 1
        $cloverFile = new Xml($pathToCloverXml);
43
        if (!$cloverFile->exists()) {
44 5
            throw new RuntimeException('could not find clover xml file: ' . $cloverFile->getPath());
45 5
        }
46 4
        $this->xml = $cloverFile->read();
47
        $this->validateXml();
48
    }
49
50
    /**
51
     * Make sure you have a valid xml structure
52
     *
53
     * @return void
54 5
     * @throws \RuntimeException
55
     */
56 5
    private function validateXml(): void
57 1
    {
58
        if (!isset($this->xml->project) || !isset($this->xml->project->metrics)) {
59 4
            throw new RuntimeException('invalid clover xml file');
60
        }
61
    }
62
63
    /**
64
     * Return test coverage in percent.
65
     *
66 4
     * @return float
67
     */
68 4
    public function getCoverage(): float
69 4
    {
70
        $statements = (string) $this->xml->project->metrics->attributes()->statements;
71 4
        $covered    = (string) $this->xml->project->metrics->attributes()->coveredstatements;
72 1
73
        if ($statements < 1 || !is_numeric($covered)) {
74 1
            throw new RuntimeException(
75
                'could not read coverage data from clover xml file ' .
76
                '(statements: ' . $statements . ', coveredstatements: ' . $covered . ')'
77
            );
78 3
        }
79
80
        return $covered / ($statements * 0.01);
81
    }
82
}
83