Passed
Push — master ( c6d4b5...1256de )
by Pierre
01:47
created

Parser::getFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 9
    public function __construct()
48
    {
49 9
        $this->args = new Args();
50 9
        $this->processor = new Processor();
51 9
        $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($this->getContent());
74 1
            $metrics = $xml->project->metrics;
75 1
            $classes = $xml->xpath(self::XPATH_SEARCH);
76 1
            $coveredClasses = 0;
77 1
            foreach ($classes as $class) {
78 1
                $methods = (int) $class->metrics[Args::_METHODS];
79 1
                $coveredMethods = (int) $class->metrics[IProcessor::COVERED_METHODS];
80 1
                $areMethodsCovered = ($methods > 0 && $methods === $coveredMethods);
81 1
                if ($areMethodsCovered) {
82 1
                    $coveredClasses++;
83
                }
84
            }
85 1
            $this->processor->process($coveredClasses, $metrics);
86
        }
87 1
        return $this;
88
    }
89
90
    /**
91
     * return parser instance
92
     *
93
     * @return IParser
94
     */
95 1
    public function getParser(): IParser
96
    {
97 1
        return $this;
98
    }
99
100
    /**
101
     * return Args object
102
     *
103
     * @return IArgs
104
     */
105 1
    public function getArgs(): IArgs
106
    {
107 1
        return $this->args;
108
    }
109
110
    /**
111
     * returns result metrics coverage ratios as array
112
     *
113
     * @return array
114
     */
115 1
    public function getResults(): array
116
    {
117 1
        return $this->processor->getResults();
118
    }
119
120
    /**
121
     * returns coverage file content
122
     *
123
     * @return string
124
     */
125 1
    protected function getContent(): string
126
    {
127 1
        return $this->content;
128
    }
129
130
    /**
131
     * set content from file
132
     *
133
     * @return IParser
134
     */
135 1
    protected function setContent(): IParser
136
    {
137 1
        $this->content = ($this->exists())
138 1
            ? file_get_contents($this->getFilename())
139 1
            : '';
140 1
        return $this;
141
    }
142
143
    /**
144
     * returns true if file exists
145
     *
146
     * @return boolean
147
     */
148 1
    protected function exists(): bool
149
    {
150 1
        return file_exists($this->getFilename());
151
    }
152
153
    /**
154
     * return filename from args
155
     *
156
     * @return string
157
     */
158 1
    protected function getFilename(): string
159
    {
160 1
        return $this->args->getFilename();
161
    }
162
}
163