Passed
Push — develop ( 30cf64...589229 )
by Guillaume
06:18 queued 04:10
created

Clover::process()   F

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 228
Code Lines 146

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 28
eloc 146
nc 235938
nop 3
dl 0
loc 228
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of phpunit/php-code-coverage.
4
 *
5
 * (c) Sebastian Bergmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianBergmann\CodeCoverage\Report;
11
12
use SebastianBergmann\CodeCoverage\CodeCoverage;
13
use SebastianBergmann\CodeCoverage\Directory;
14
use SebastianBergmann\CodeCoverage\Node\File;
15
use SebastianBergmann\CodeCoverage\RuntimeException;
16
17
/**
18
 * Generates a Clover XML logfile from a code coverage object.
19
 */
20
final class Clover
21
{
22
    /**
23
     * @throws \RuntimeException
24
     */
25
    public function process(CodeCoverage $coverage, ?string $target = null, ?string $name = null): string
26
    {
27
        $time = (string) \time();
28
29
        $xmlDocument               = new \DOMDocument('1.0', 'UTF-8');
30
        $xmlDocument->formatOutput = true;
31
32
        $xmlCoverage = $xmlDocument->createElement('coverage');
33
        $xmlCoverage->setAttribute('generated', $time);
34
        $xmlDocument->appendChild($xmlCoverage);
35
36
        $xmlProject = $xmlDocument->createElement('project');
37
        $xmlProject->setAttribute('timestamp', $time);
38
39
        if (\is_string($name)) {
40
            $xmlProject->setAttribute('name', $name);
41
        }
42
43
        $xmlCoverage->appendChild($xmlProject);
44
45
        $packages = [];
46
        $report   = $coverage->getReport();
47
48
        foreach ($report as $item) {
49
            if (!$item instanceof File) {
50
                continue;
51
            }
52
53
            /* @var File $item */
54
55
            $xmlFile = $xmlDocument->createElement('file');
56
            $xmlFile->setAttribute('name', $item->getPath());
57
58
            $classes      = $item->getClassesAndTraits();
59
            $coverageData = $item->getCoverageData();
60
            $lines        = [];
61
            $namespace    = 'global';
62
63
            foreach ($classes as $className => $class) {
64
                $classStatements        = 0;
65
                $coveredClassStatements = 0;
66
                $coveredMethods         = 0;
67
                $classMethods           = 0;
68
69
                foreach ($class['methods'] as $methodName => $method) {
70
                    if ($method['executableLines'] == 0) {
71
                        continue;
72
                    }
73
74
                    $classMethods++;
75
                    $classStatements += $method['executableLines'];
76
                    $coveredClassStatements += $method['executedLines'];
77
78
                    if ($method['coverage'] == 100) {
79
                        $coveredMethods++;
80
                    }
81
82
                    $methodCount = 0;
83
84
                    foreach (\range($method['startLine'], $method['endLine']) as $line) {
85
                        if (isset($coverageData[$line]) && ($coverageData[$line] !== null)) {
86
                            $methodCount = \max($methodCount, \count($coverageData[$line]));
87
                        }
88
                    }
89
90
                    $lines[$method['startLine']] = [
91
                        'ccn'         => $method['ccn'],
92
                        'count'       => $methodCount,
93
                        'crap'        => $method['crap'],
94
                        'type'        => 'method',
95
                        'visibility'  => $method['visibility'],
96
                        'name'        => $methodName,
97
                    ];
98
                }
99
100
                if (!empty($class['package']['namespace'])) {
101
                    $namespace = $class['package']['namespace'];
102
                }
103
104
                $xmlClass = $xmlDocument->createElement('class');
105
                $xmlClass->setAttribute('name', $className);
106
                $xmlClass->setAttribute('namespace', $namespace);
107
108
                if (!empty($class['package']['fullPackage'])) {
109
                    $xmlClass->setAttribute(
110
                        'fullPackage',
111
                        $class['package']['fullPackage']
112
                    );
113
                }
114
115
                if (!empty($class['package']['category'])) {
116
                    $xmlClass->setAttribute(
117
                        'category',
118
                        $class['package']['category']
119
                    );
120
                }
121
122
                if (!empty($class['package']['package'])) {
123
                    $xmlClass->setAttribute(
124
                        'package',
125
                        $class['package']['package']
126
                    );
127
                }
128
129
                if (!empty($class['package']['subpackage'])) {
130
                    $xmlClass->setAttribute(
131
                        'subpackage',
132
                        $class['package']['subpackage']
133
                    );
134
                }
135
136
                $xmlFile->appendChild($xmlClass);
137
138
                $xmlMetrics = $xmlDocument->createElement('metrics');
139
                $xmlMetrics->setAttribute('complexity', (string) $class['ccn']);
140
                $xmlMetrics->setAttribute('methods', (string) $classMethods);
141
                $xmlMetrics->setAttribute('coveredmethods', (string) $coveredMethods);
142
                $xmlMetrics->setAttribute('conditionals', '0');
143
                $xmlMetrics->setAttribute('coveredconditionals', '0');
144
                $xmlMetrics->setAttribute('statements', (string) $classStatements);
145
                $xmlMetrics->setAttribute('coveredstatements', (string) $coveredClassStatements);
146
                $xmlMetrics->setAttribute('elements', (string) ($classMethods + $classStatements /* + conditionals */));
147
                $xmlMetrics->setAttribute('coveredelements', (string) ($coveredMethods + $coveredClassStatements /* + coveredconditionals */));
148
                $xmlClass->appendChild($xmlMetrics);
149
            }
150
151
            foreach ($coverageData as $line => $data) {
152
                if ($data === null || isset($lines[$line])) {
153
                    continue;
154
                }
155
156
                $lines[$line] = [
157
                    'count' => \count($data), 'type' => 'stmt',
158
                ];
159
            }
160
161
            \ksort($lines);
162
163
            foreach ($lines as $line => $data) {
164
                $xmlLine = $xmlDocument->createElement('line');
165
                $xmlLine->setAttribute('num', (string) $line);
166
                $xmlLine->setAttribute('type', $data['type']);
167
168
                if (isset($data['name'])) {
169
                    $xmlLine->setAttribute('name', $data['name']);
170
                }
171
172
                if (isset($data['visibility'])) {
173
                    $xmlLine->setAttribute('visibility', $data['visibility']);
174
                }
175
176
                if (isset($data['ccn'])) {
177
                    $xmlLine->setAttribute('complexity', (string) $data['ccn']);
178
                }
179
180
                if (isset($data['crap'])) {
181
                    $xmlLine->setAttribute('crap', (string) $data['crap']);
182
                }
183
184
                $xmlLine->setAttribute('count', (string) $data['count']);
185
                $xmlFile->appendChild($xmlLine);
186
            }
187
188
            $linesOfCode = $item->getLinesOfCode();
189
190
            $xmlMetrics = $xmlDocument->createElement('metrics');
191
            $xmlMetrics->setAttribute('loc', (string) $linesOfCode['loc']);
192
            $xmlMetrics->setAttribute('ncloc', (string) $linesOfCode['ncloc']);
193
            $xmlMetrics->setAttribute('classes', (string) $item->getNumClassesAndTraits());
194
            $xmlMetrics->setAttribute('methods', (string) $item->getNumMethods());
195
            $xmlMetrics->setAttribute('coveredmethods', (string) $item->getNumTestedMethods());
196
            $xmlMetrics->setAttribute('conditionals', '0');
197
            $xmlMetrics->setAttribute('coveredconditionals', '0');
198
            $xmlMetrics->setAttribute('statements', (string) $item->getNumExecutableLines());
199
            $xmlMetrics->setAttribute('coveredstatements', (string) $item->getNumExecutedLines());
200
            $xmlMetrics->setAttribute('elements', (string) ($item->getNumMethods() + $item->getNumExecutableLines() /* + conditionals */));
201
            $xmlMetrics->setAttribute('coveredelements', (string) ($item->getNumTestedMethods() + $item->getNumExecutedLines() /* + coveredconditionals */));
202
            $xmlFile->appendChild($xmlMetrics);
203
204
            if ($namespace === 'global') {
205
                $xmlProject->appendChild($xmlFile);
206
            } else {
207
                if (!isset($packages[$namespace])) {
208
                    $packages[$namespace] = $xmlDocument->createElement(
209
                        'package'
210
                    );
211
212
                    $packages[$namespace]->setAttribute('name', $namespace);
213
                    $xmlProject->appendChild($packages[$namespace]);
214
                }
215
216
                $packages[$namespace]->appendChild($xmlFile);
217
            }
218
        }
219
220
        $linesOfCode = $report->getLinesOfCode();
221
222
        $xmlMetrics = $xmlDocument->createElement('metrics');
223
        $xmlMetrics->setAttribute('files', (string) \count($report));
224
        $xmlMetrics->setAttribute('loc', (string) $linesOfCode['loc']);
225
        $xmlMetrics->setAttribute('ncloc', (string) $linesOfCode['ncloc']);
226
        $xmlMetrics->setAttribute('classes', (string) $report->getNumClassesAndTraits());
227
        $xmlMetrics->setAttribute('methods', (string) $report->getNumMethods());
228
        $xmlMetrics->setAttribute('coveredmethods', (string) $report->getNumTestedMethods());
229
        $xmlMetrics->setAttribute('conditionals', '0');
230
        $xmlMetrics->setAttribute('coveredconditionals', '0');
231
        $xmlMetrics->setAttribute('statements', (string) $report->getNumExecutableLines());
232
        $xmlMetrics->setAttribute('coveredstatements', (string) $report->getNumExecutedLines());
233
        $xmlMetrics->setAttribute('elements', (string) ($report->getNumMethods() + $report->getNumExecutableLines() /* + conditionals */));
234
        $xmlMetrics->setAttribute('coveredelements', (string) ($report->getNumTestedMethods() + $report->getNumExecutedLines() /* + coveredconditionals */));
235
        $xmlProject->appendChild($xmlMetrics);
236
237
        $buffer = $xmlDocument->saveXML();
238
239
        if ($target !== null) {
240
            Directory::create(\dirname($target));
241
242
            if (@\file_put_contents($target, $buffer) === false) {
243
                throw new RuntimeException(
244
                    \sprintf(
245
                        'Could not write to "%s',
246
                        $target
247
                    )
248
                );
249
            }
250
        }
251
252
        return $buffer;
253
    }
254
}
255