Passed
Push — master ( 52f2d2...c6d4b5 )
by Pierre
02:38 queued 21s
created

Parser   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 94.87%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 34
c 2
b 1
f 0
dl 0
loc 136
ccs 37
cts 39
cp 0.9487
rs 10
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getParser() 0 3 1
A getResults() 0 3 1
A setContent() 0 8 2
A exists() 0 4 1
A init() 0 4 1
A getArgs() 0 3 1
A parse() 0 20 5
A getContent() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PierInfor\Undercover;
6
7
use PierInfor\Undercover\Interfaces\IParser;
8
use PierInfor\Undercover\Interfaces\IArgs;
9
use PierInfor\Undercover\Args;
10
use PierInfor\Undercover\Interfaces\IProcessor;
11
use PierInfor\Undercover\Processor;
12
13
/**
14
 * Parser is a clover coverage parser
15
 *
16
 * @author Pierre Fromager <info@pier_infor.fr>
17
 * @version 1.0
18
 * @package PierInfor\Undercover
19
 */
20
class Parser implements IParser
21
{
22
23
    /**
24
     * $args
25
     *
26
     * @var IArgs
27
     */
28
    protected $args;
29
30
    /**
31
     * $content
32
     *
33
     * @var string
34
     */
35
    protected $content;
36
37
    /**
38
     * processor
39
     *
40
     * @var IProcessor
41
     */
42
    protected $processor;
43
44
    /**
45
     * constructor
46
     */
47 8
    public function __construct()
48
    {
49 8
        $this->args = new Args();
50 8
        $this->processor = new Processor();
51 8
        $this->init();
52
    }
53
54
    /**
55
     * initializer
56
     *
57
     * @return IParser
58
     */
59 1
    protected function init(): IParser
60
    {
61 1
        $this->setContent();
62 1
        return $this;
63
    }
64
65
    /**
66
     * parse xml clover file and set coverage results
67
     *
68
     * @return IParser
69
     */
70 1
    public function parse(): IParser
71
    {
72 1
        if (!empty($this->getContent())) {
73 1
            $xml = new \SimpleXMLElement(
74 1
                $this->getContent()
75
            );
76 1
            $metrics = $xml->project->metrics;
77 1
            $classes = $xml->xpath(self::XPATH_SEARCH);
78 1
            $coveredClasses = 0;
79 1
            foreach ($classes as $class) {
80 1
                $methods = (int) $class->metrics[Args::_METHODS];
81 1
                $areMethodsCovered = ($methods > 0
82 1
                    && $methods === (int) $class->metrics[IProcessor::COVERED_METHODS]);
83 1
                if ($areMethodsCovered) {
84 1
                    $coveredClasses++;
85
                }
86
            }
87 1
            $this->processor->process($coveredClasses, $metrics);
88
        }
89 1
        return $this;
90
    }
91
92
    /**
93
     * return parser instance
94
     *
95
     * @return IParser
96
     */
97 1
    public function getParser(): IParser
98
    {
99 1
        return $this;
100
    }
101
102
    /**
103
     * return Args object
104
     *
105
     * @return IArgs
106
     */
107 1
    public function getArgs(): IArgs
108
    {
109 1
        return $this->args;
110
    }
111
112
    /**
113
     * returns result metrics coverage ratios as array
114
     *
115
     * @return array
116
     */
117 1
    public function getResults(): array
118
    {
119 1
        return $this->processor->getResults();
120
    }
121
122
    /**
123
     * returns coverage file content
124
     *
125
     * @return string
126
     */
127 1
    protected function getContent(): string
128
    {
129 1
        return $this->content;
130
    }
131
132
    /**
133
     * set content from file
134
     *
135
     * @return IParser
136
     */
137 1
    protected function setContent(): IParser
138
    {
139 1
        $this->content = ($this->exists())
140
            ? file_get_contents(
141
                $this->args->getFilename()
142
            )
143 1
            : '';
144 1
        return $this;
145
    }
146
147
    /**
148
     * returns true if file exists
149
     *
150
     * @return boolean
151
     */
152 1
    protected function exists(): bool
153
    {
154 1
        return file_exists(
155 1
            $this->args->getFilename()
156
        );
157
    }
158
}
159