Completed
Push — master ( 96df1a...267f86 )
by Naveen
09:52
created

PHP_CodeCoverage_Report_Clover::process()   F

Complexity

Conditions 26
Paths > 20000

Size

Total Lines 260
Code Lines 169

Duplication

Lines 33
Ratio 12.69 %
Metric Value
dl 33
loc 260
rs 2
cc 26
eloc 169
nc 69402
nop 3

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
2
/*
3
 * This file is part of the PHP_CodeCoverage package.
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
11
/**
12
 * Generates a Clover XML logfile from an PHP_CodeCoverage object.
13
 *
14
 * @since Class available since Release 1.0.0
15
 */
16
class PHP_CodeCoverage_Report_Clover
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    /**
19
     * @param  PHP_CodeCoverage $coverage
20
     * @param  string           $target
21
     * @param  string           $name
22
     * @return string
23
     */
24
    public function process(PHP_CodeCoverage $coverage, $target = null, $name = null)
0 ignored issues
show
Coding Style introduced by
process uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
25
    {
26
        $xmlDocument               = new DOMDocument('1.0', 'UTF-8');
27
        $xmlDocument->formatOutput = true;
28
29
        $xmlCoverage = $xmlDocument->createElement('coverage');
30
        $xmlCoverage->setAttribute('generated', (int) $_SERVER['REQUEST_TIME']);
31
        $xmlDocument->appendChild($xmlCoverage);
32
33
        $xmlProject = $xmlDocument->createElement('project');
34
        $xmlProject->setAttribute('timestamp', (int) $_SERVER['REQUEST_TIME']);
35
36
        if (is_string($name)) {
37
            $xmlProject->setAttribute('name', $name);
38
        }
39
40
        $xmlCoverage->appendChild($xmlProject);
41
42
        $packages = array();
43
        $report   = $coverage->getReport();
44
        unset($coverage);
45
46
        foreach ($report as $item) {
47
            $namespace = 'global';
48
49
            if (!$item instanceof PHP_CodeCoverage_Report_Node_File) {
50
                continue;
51
            }
52
53
            $xmlFile = $xmlDocument->createElement('file');
54
            $xmlFile->setAttribute('name', $item->getPath());
55
56
            $classes  = $item->getClassesAndTraits();
57
            $coverage = $item->getCoverageData();
58
            $lines    = array();
59
60
            foreach ($classes as $className => $class) {
61
                $classStatements        = 0;
62
                $coveredClassStatements = 0;
63
                $coveredMethods         = 0;
64
                $classMethods           = 0;
65
66
                foreach ($class['methods'] as $methodName => $method) {
67
                    if ($method['executableLines']  == 0) {
68
                        continue;
69
                    }
70
71
                    $classMethods++;
72
                    $classStatements        += $method['executableLines'];
73
                    $coveredClassStatements += $method['executedLines'];
74
                    if ($method['coverage'] == 100) {
75
                        $coveredMethods++;
76
                    }
77
78
                    $methodCount = 0;
79
                    for ($i  = $method['startLine'];
80
                         $i <= $method['endLine'];
81
                         $i++) {
82
                        if (isset($coverage[$i]) && ($coverage[$i] !== null)) {
83
                            $methodCount = max($methodCount, count($coverage[$i]));
84
                        }
85
                    }
86
87
                    $lines[$method['startLine']] = array(
88
                        'count' => $methodCount,
89
                        'crap'  => $method['crap'],
90
                        'type'  => 'method',
91
                        'name'  => $methodName
92
                    );
93
                }
94
95
                if (!empty($class['package']['namespace'])) {
96
                    $namespace = $class['package']['namespace'];
97
                }
98
99
                $xmlClass = $xmlDocument->createElement('class');
100
                $xmlClass->setAttribute('name', $className);
101
                $xmlClass->setAttribute('namespace', $namespace);
102
103 View Code Duplication
                if (!empty($class['package']['fullPackage'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
                    $xmlClass->setAttribute(
105
                        'fullPackage',
106
                        $class['package']['fullPackage']
107
                    );
108
                }
109
110 View Code Duplication
                if (!empty($class['package']['category'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
                    $xmlClass->setAttribute(
112
                        'category',
113
                        $class['package']['category']
114
                    );
115
                }
116
117 View Code Duplication
                if (!empty($class['package']['package'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
                    $xmlClass->setAttribute(
119
                        'package',
120
                        $class['package']['package']
121
                    );
122
                }
123
124 View Code Duplication
                if (!empty($class['package']['subpackage'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
                    $xmlClass->setAttribute(
126
                        'subpackage',
127
                        $class['package']['subpackage']
128
                    );
129
                }
130
131
                $xmlFile->appendChild($xmlClass);
132
133
                $xmlMetrics = $xmlDocument->createElement('metrics');
134
                $xmlMetrics->setAttribute('methods', $classMethods);
135
                $xmlMetrics->setAttribute('coveredmethods', $coveredMethods);
136
                $xmlMetrics->setAttribute('conditionals', 0);
137
                $xmlMetrics->setAttribute('coveredconditionals', 0);
138
                $xmlMetrics->setAttribute('statements', $classStatements);
139
                $xmlMetrics->setAttribute(
140
                    'coveredstatements',
141
                    $coveredClassStatements
142
                );
143
                $xmlMetrics->setAttribute(
144
                    'elements',
145
                    $classMethods +
146
                    $classStatements
147
                    /* + conditionals */
148
                );
149
                $xmlMetrics->setAttribute(
150
                    'coveredelements',
151
                    $coveredMethods +
152
                    $coveredClassStatements
153
                    /* + coveredconditionals */
154
                );
155
                $xmlClass->appendChild($xmlMetrics);
156
            }
157
158
            foreach ($coverage as $line => $data) {
159
                if ($data === null || isset($lines[$line])) {
160
                    continue;
161
                }
162
163
                $lines[$line] = array(
164
                    'count' => count($data), 'type' => 'stmt'
165
                );
166
            }
167
168
            ksort($lines);
169
170
            foreach ($lines as $line => $data) {
171
                $xmlLine = $xmlDocument->createElement('line');
172
                $xmlLine->setAttribute('num', $line);
173
                $xmlLine->setAttribute('type', $data['type']);
174
175
                if (isset($data['name'])) {
176
                    $xmlLine->setAttribute('name', $data['name']);
177
                }
178
179
                if (isset($data['crap'])) {
180
                    $xmlLine->setAttribute('crap', $data['crap']);
181
                }
182
183
                $xmlLine->setAttribute('count', $data['count']);
184
                $xmlFile->appendChild($xmlLine);
185
            }
186
187
            $linesOfCode = $item->getLinesOfCode();
188
189
            $xmlMetrics = $xmlDocument->createElement('metrics');
190
            $xmlMetrics->setAttribute('loc', $linesOfCode['loc']);
191
            $xmlMetrics->setAttribute('ncloc', $linesOfCode['ncloc']);
192
            $xmlMetrics->setAttribute('classes', $item->getNumClassesAndTraits());
193
            $xmlMetrics->setAttribute('methods', $item->getNumMethods());
194
            $xmlMetrics->setAttribute(
195
                'coveredmethods',
196
                $item->getNumTestedMethods()
197
            );
198
            $xmlMetrics->setAttribute('conditionals', 0);
199
            $xmlMetrics->setAttribute('coveredconditionals', 0);
200
            $xmlMetrics->setAttribute(
201
                'statements',
202
                $item->getNumExecutableLines()
203
            );
204
            $xmlMetrics->setAttribute(
205
                'coveredstatements',
206
                $item->getNumExecutedLines()
207
            );
208
            $xmlMetrics->setAttribute(
209
                'elements',
210
                $item->getNumMethods() + $item->getNumExecutableLines()
211
                /* + conditionals */
212
            );
213
            $xmlMetrics->setAttribute(
214
                'coveredelements',
215
                $item->getNumTestedMethods() + $item->getNumExecutedLines()
216
                /* + coveredconditionals */
217
            );
218
            $xmlFile->appendChild($xmlMetrics);
219
220
            if ($namespace == 'global') {
221
                $xmlProject->appendChild($xmlFile);
222
            } else {
223
                if (!isset($packages[$namespace])) {
224
                    $packages[$namespace] = $xmlDocument->createElement(
225
                        'package'
226
                    );
227
228
                    $packages[$namespace]->setAttribute('name', $namespace);
229
                    $xmlProject->appendChild($packages[$namespace]);
230
                }
231
232
                $packages[$namespace]->appendChild($xmlFile);
233
            }
234
        }
235
236
        $linesOfCode = $report->getLinesOfCode();
237
238
        $xmlMetrics = $xmlDocument->createElement('metrics');
239
        $xmlMetrics->setAttribute('files', count($report));
240
        $xmlMetrics->setAttribute('loc', $linesOfCode['loc']);
241
        $xmlMetrics->setAttribute('ncloc', $linesOfCode['ncloc']);
242
        $xmlMetrics->setAttribute(
243
            'classes',
244
            $report->getNumClassesAndTraits()
245
        );
246
        $xmlMetrics->setAttribute('methods', $report->getNumMethods());
247
        $xmlMetrics->setAttribute(
248
            'coveredmethods',
249
            $report->getNumTestedMethods()
250
        );
251
        $xmlMetrics->setAttribute('conditionals', 0);
252
        $xmlMetrics->setAttribute('coveredconditionals', 0);
253
        $xmlMetrics->setAttribute(
254
            'statements',
255
            $report->getNumExecutableLines()
256
        );
257
        $xmlMetrics->setAttribute(
258
            'coveredstatements',
259
            $report->getNumExecutedLines()
260
        );
261
        $xmlMetrics->setAttribute(
262
            'elements',
263
            $report->getNumMethods() + $report->getNumExecutableLines()
264
            /* + conditionals */
265
        );
266
        $xmlMetrics->setAttribute(
267
            'coveredelements',
268
            $report->getNumTestedMethods() + $report->getNumExecutedLines()
269
            /* + coveredconditionals */
270
        );
271
272
        $xmlProject->appendChild($xmlMetrics);
273
274 View Code Duplication
        if ($target !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
275
            if (!is_dir(dirname($target))) {
276
                mkdir(dirname($target), 0777, true);
277
            }
278
279
            return $xmlDocument->save($target);
280
        } else {
281
            return $xmlDocument->saveXML();
282
        }
283
    }
284
}
285