Passed
Push — master ( 7d6f7a...eb4fb1 )
by Pierre
02:15
created

Parser::setContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 4
cts 6
cp 0.6667
crap 2.1481
rs 10
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
        $this->results = [];
0 ignored issues
show
Bug Best Practice introduced by
The property results does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
63 1
        $this->error = false;
0 ignored issues
show
Bug Best Practice introduced by
The property error does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
64 1
        return $this;
65
    }
66
67
    /**
68
     * parse xml clover file and set coverage results
69
     *
70
     * @return IParser
71
     */
72 1
    public function parse(): IParser
73
    {
74 1
        if (!empty($this->getContent())) {
75 1
            $xml = new \SimpleXMLElement(
76 1
                $this->getContent()
77
            );
78 1
            $metrics = $xml->project->metrics;
79 1
            $classes = $xml->xpath(self::XPATH_SEARCH);
80 1
            $coveredClasses = 0;
81 1
            foreach ($classes as $class) {
82 1
                $methods = (int) $class->metrics[Args::_METHODS];
83 1
                $areMethodsCovered = ($methods > 0
84 1
                    && $methods === (int) $class->metrics[IProcessor::COVERED_METHODS]);
85 1
                if ($areMethodsCovered) {
86 1
                    $coveredClasses++;
87
                }
88
            }
89 1
            $this->processor->process($coveredClasses, $metrics);
90
        }
91 1
        return $this;
92
    }
93
94
    /**
95
     * return parser instance
96
     *
97
     * @return IParser
98
     */
99 1
    public function getParser(): IParser
100
    {
101 1
        return $this;
102
    }
103
104
    /**
105
     * return Args object
106
     *
107
     * @return IArgs
108
     */
109 1
    public function getArgs(): IArgs
110
    {
111 1
        return $this->args;
112
    }
113
114
    /**
115
     * returns result metrics coverage ratios as array
116
     *
117
     * @return array
118
     */
119 1
    public function getResults(): array
120
    {
121 1
        return $this->processor->getResults();
122
    }
123
124
    /**
125
     * returns coverage file content
126
     *
127
     * @return string
128
     */
129 1
    protected function getContent(): string
130
    {
131 1
        return $this->content;
132
    }
133
134
    /**
135
     * set content from file
136
     *
137
     * @return IParser
138
     */
139 1
    protected function setContent(): IParser
140
    {
141 1
        $this->content = ($this->exists())
142
            ? file_get_contents(
143
                $this->args->getFilename()
144
            )
145 1
            : '';
146 1
        return $this;
147
    }
148
149
    /**
150
     * returns true if file exists
151
     *
152
     * @return boolean
153
     */
154 1
    protected function exists(): bool
155
    {
156 1
        return file_exists(
157 1
            $this->args->getFilename()
158
        );
159
    }
160
}
161