]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHP/CodeCoverage/Report/Clover.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHP / CodeCoverage / Report / Clover.php
1 <?php
2 /**
3  * PHP_CodeCoverage
4  *
5  * Copyright (c) 2009-2011, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @category   PHP
38  * @package    CodeCoverage
39  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40  * @copyright  2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42  * @link       http://github.com/sebastianbergmann/php-code-coverage
43  * @since      File available since Release 1.0.0
44  */
45
46 require_once 'PHP/CodeCoverage.php';
47 require_once 'PHP/Token/Stream/CachingFactory.php';
48
49 /**
50  * Generates a Clover XML logfile from an PHP_CodeCoverage object.
51  *
52  * @category   PHP
53  * @package    CodeCoverage
54  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
55  * @copyright  2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
56  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
57  * @version    Release: 1.0.4
58  * @link       http://github.com/sebastianbergmann/php-code-coverage
59  * @since      Class available since Release 1.0.0
60  */
61 class PHP_CodeCoverage_Report_Clover
62 {
63     /**
64      * @param  PHP_CodeCoverage $coverage
65      * @param  string           $target
66      * @param  string           $name
67      * @return string
68      */
69     public function process(PHP_CodeCoverage $coverage, $target = NULL, $name = NULL)
70     {
71         $document = new DOMDocument('1.0', 'UTF-8');
72         $document->formatOutput = TRUE;
73
74         $root = $document->createElement('coverage');
75         $root->setAttribute('generated', (int)$_SERVER['REQUEST_TIME']);
76         $document->appendChild($root);
77
78         $project = $document->createElement('project');
79         $project->setAttribute('timestamp', (int)$_SERVER['REQUEST_TIME']);
80
81         if (is_string($name)) {
82             $project->setAttribute('name', $name);
83         }
84
85         $root->appendChild($project);
86
87         $files    = $coverage->getSummary();
88         $packages = array();
89
90         $projectStatistics = array(
91           'files'               => 0,
92           'loc'                 => 0,
93           'ncloc'               => 0,
94           'classes'             => 0,
95           'methods'             => 0,
96           'coveredMethods'      => 0,
97           'conditionals'        => 0,
98           'coveredConditionals' => 0,
99           'statements'          => 0,
100           'coveredStatements'   => 0
101         );
102
103         foreach ($files as $filename => $data) {
104             $namespace = 'global';
105
106             if (file_exists($filename)) {
107                 $fileStatistics = array(
108                   'classes'             => 0,
109                   'methods'             => 0,
110                   'coveredMethods'      => 0,
111                   'conditionals'        => 0,
112                   'coveredConditionals' => 0,
113                   'statements'          => 0,
114                   'coveredStatements'   => 0
115                 );
116
117                 $file = $document->createElement('file');
118                 $file->setAttribute('name', $filename);
119
120                 $tokens        = PHP_Token_Stream_CachingFactory::get($filename);
121                 $classesInFile = $tokens->getClasses();
122                 $linesOfCode   = $tokens->getLinesOfCode();
123                 unset($tokens);
124
125                 $ignoredLines = PHP_CodeCoverage_Util::getLinesToBeIgnored(
126                   $filename
127                 );
128
129                 $lines = array();
130
131                 foreach ($classesInFile as $className => $_class) {
132                     $classStatistics = array(
133                       'methods'             => 0,
134                       'coveredMethods'      => 0,
135                       'conditionals'        => 0,
136                       'coveredConditionals' => 0,
137                       'statements'          => 0,
138                       'coveredStatements'   => 0
139                     );
140
141                     foreach ($_class['methods'] as $methodName => $method) {
142                         $classStatistics['methods']++;
143
144                         $methodCount        = 0;
145                         $methodLines        = 0;
146                         $methodLinesCovered = 0;
147
148                         for ($i  = $method['startLine'];
149                              $i <= $method['endLine'];
150                              $i++) {
151                             if (isset($ignoredLines[$i])) {
152                                 continue;
153                             }
154
155                             $add   = TRUE;
156                             $count = 0;
157
158                             if (isset($files[$filename][$i])) {
159                                 if ($files[$filename][$i] != -2) {
160                                     $classStatistics['statements']++;
161                                     $methodLines++;
162                                 }
163
164                                 if (is_array($files[$filename][$i])) {
165                                     $classStatistics['coveredStatements']++;
166                                     $methodLinesCovered++;
167                                     $count = count($files[$filename][$i]);
168                                 }
169
170                                 else if ($files[$filename][$i] == -2) {
171                                     $add = FALSE;
172                                 }
173                             } else {
174                                 $add = FALSE;
175                             }
176
177                             $methodCount = max($methodCount, $count);
178
179                             if ($add) {
180                                 $lines[$i] = array(
181                                   'count' => $count,
182                                   'type'  => 'stmt'
183                                 );
184                             }
185                         }
186
187                         if ($methodCount > 0) {
188                             $classStatistics['coveredMethods']++;
189                         }
190
191                         $lines[$method['startLine']] = array(
192                           'count' => $methodCount,
193                           'crap'  => PHP_CodeCoverage_Util::crap(
194                                        $method['ccn'],
195                                        PHP_CodeCoverage_Util::percent(
196                                          $methodLinesCovered,
197                                          $methodLines
198                                        )
199                                      ),
200                           'type'  => 'method',
201                           'name'  => $methodName
202                         );
203                     }
204
205                     $package = PHP_CodeCoverage_Util::getPackageInformation(
206                       $className, $_class['docblock']
207                     );
208
209                     if (!empty($package['namespace'])) {
210                         $namespace = $package['namespace'];
211                     }
212
213                     $class = $document->createElement('class');
214                     $class->setAttribute('name', $className);
215                     $class->setAttribute('namespace', $namespace);
216
217                     if (!empty($package['fullPackage'])) {
218                         $class->setAttribute(
219                           'fullPackage', $package['fullPackage']
220                         );
221                     }
222
223                     if (!empty($package['category'])) {
224                         $class->setAttribute(
225                           'category', $package['category']
226                         );
227                     }
228
229                     if (!empty($package['package'])) {
230                         $class->setAttribute(
231                           'package', $package['package']
232                         );
233                     }
234
235                     if (!empty($package['subpackage'])) {
236                         $class->setAttribute(
237                           'subpackage', $package['subpackage']
238                         );
239                     }
240
241                     $file->appendChild($class);
242
243                     $metrics = $document->createElement('metrics');
244
245                     $metrics->setAttribute(
246                       'methods', $classStatistics['methods']
247                     );
248
249                     $metrics->setAttribute(
250                       'coveredmethods', $classStatistics['coveredMethods']
251                     );
252
253                     $metrics->setAttribute(
254                       'conditionals', $classStatistics['conditionals']
255                     );
256
257                     $metrics->setAttribute(
258                       'coveredconditionals',
259                       $classStatistics['coveredConditionals']
260                     );
261
262                     $metrics->setAttribute(
263                       'statements', $classStatistics['statements']
264                     );
265
266                     $metrics->setAttribute(
267                       'coveredstatements',
268                       $classStatistics['coveredStatements']
269                     );
270
271                     $metrics->setAttribute(
272                       'elements',
273                       $classStatistics['conditionals'] +
274                       $classStatistics['statements']   +
275                       $classStatistics['methods']
276                     );
277
278                     $metrics->setAttribute(
279                       'coveredelements',
280                       $classStatistics['coveredConditionals'] +
281                       $classStatistics['coveredStatements']   +
282                       $classStatistics['coveredMethods']
283                     );
284
285                     $class->appendChild($metrics);
286
287                     $fileStatistics['methods']             += $classStatistics['methods'];
288                     $fileStatistics['coveredMethods']      += $classStatistics['coveredMethods'];
289                     $fileStatistics['conditionals']        += $classStatistics['conditionals'];
290                     $fileStatistics['coveredConditionals'] += $classStatistics['coveredConditionals'];
291                     $fileStatistics['statements']          += $classStatistics['statements'];
292                     $fileStatistics['coveredStatements']   += $classStatistics['coveredStatements'];
293                     $fileStatistics['classes']++;
294                 }
295
296                 foreach ($data as $_line => $_data) {
297                     if (isset($lines[$_line]) || isset($ignoredLines[$_line])) {
298                         continue;
299                     }
300
301                     if ($_data != -2) {
302                         $fileStatistics['statements']++;
303
304                         if (is_array($_data)) {
305                             $count = count($_data);
306                             $fileStatistics['coveredStatements']++;
307                         } else {
308                             $count = 0;
309                         }
310
311                         $lines[$_line] = array(
312                           'count' => $count,
313                           'type' => 'stmt'
314                         );
315                     }
316                 }
317
318                 ksort($lines);
319
320                 foreach ($lines as $_line => $_data) {
321                     if (isset($ignoredLines[$_line])) {
322                         continue;
323                     }
324
325                     $line = $document->createElement('line');
326                     $line->setAttribute('num', $_line);
327                     $line->setAttribute('type', $_data['type']);
328
329                     if (isset($_data['name'])) {
330                         $line->setAttribute('name', $_data['name']);
331                     }
332
333                     if (isset($_data['crap'])) {
334                         $line->setAttribute('crap', $_data['crap']);
335                     }
336
337                     $line->setAttribute('count', $_data['count']);
338
339                     $file->appendChild($line);
340                 }
341
342                 $metrics = $document->createElement('metrics');
343
344                 $metrics->setAttribute('loc', $linesOfCode['loc']);
345                 $metrics->setAttribute('ncloc', $linesOfCode['ncloc']);
346                 $metrics->setAttribute('classes', $fileStatistics['classes']);
347                 $metrics->setAttribute('methods', $fileStatistics['methods']);
348
349                 $metrics->setAttribute(
350                   'coveredmethods', $fileStatistics['coveredMethods']
351                 );
352
353                 $metrics->setAttribute(
354                   'conditionals', $fileStatistics['conditionals']
355                 );
356
357                 $metrics->setAttribute(
358                   'coveredconditionals', $fileStatistics['coveredConditionals']
359                 );
360
361                 $metrics->setAttribute(
362                   'statements', $fileStatistics['statements']
363                 );
364
365                 $metrics->setAttribute(
366                   'coveredstatements', $fileStatistics['coveredStatements']
367                 );
368
369                 $metrics->setAttribute(
370                   'elements',
371                   $fileStatistics['conditionals'] +
372                   $fileStatistics['statements']   +
373                   $fileStatistics['methods']
374                 );
375
376                 $metrics->setAttribute(
377                   'coveredelements',
378                   $fileStatistics['coveredConditionals'] +
379                   $fileStatistics['coveredStatements']   +
380                   $fileStatistics['coveredMethods']
381                 );
382
383                 $file->appendChild($metrics);
384
385                 if ($namespace == 'global') {
386                     $project->appendChild($file);
387                 } else {
388                     if (!isset($packages[$namespace])) {
389                         $packages[$namespace] = $document->createElement(
390                           'package'
391                         );
392
393                         $packages[$namespace]->setAttribute('name', $namespace);
394                         $project->appendChild($packages[$namespace]);
395                     }
396
397                     $packages[$namespace]->appendChild($file);
398                 }
399
400                 $projectStatistics['loc']                 += $linesOfCode['loc'];
401                 $projectStatistics['ncloc']               += $linesOfCode['ncloc'];
402                 $projectStatistics['classes']             += $fileStatistics['classes'];
403                 $projectStatistics['methods']             += $fileStatistics['methods'];
404                 $projectStatistics['coveredMethods']      += $fileStatistics['coveredMethods'];
405                 $projectStatistics['conditionals']        += $fileStatistics['conditionals'];
406                 $projectStatistics['coveredConditionals'] += $fileStatistics['coveredConditionals'];
407                 $projectStatistics['statements']          += $fileStatistics['statements'];
408                 $projectStatistics['coveredStatements']   += $fileStatistics['coveredStatements'];
409                 $projectStatistics['files']++;
410             }
411         }
412
413         $metrics = $document->createElement('metrics');
414
415         $metrics->setAttribute('files', $projectStatistics['files']);
416         $metrics->setAttribute('loc', $projectStatistics['loc']);
417         $metrics->setAttribute('ncloc', $projectStatistics['ncloc']);
418         $metrics->setAttribute('classes', $projectStatistics['classes']);
419         $metrics->setAttribute('methods', $projectStatistics['methods']);
420
421         $metrics->setAttribute(
422           'coveredmethods', $projectStatistics['coveredMethods']
423         );
424
425         $metrics->setAttribute(
426           'conditionals', $projectStatistics['conditionals']
427         );
428
429         $metrics->setAttribute(
430           'coveredconditionals', $projectStatistics['coveredConditionals']
431         );
432
433         $metrics->setAttribute(
434           'statements', $projectStatistics['statements']
435         );
436
437         $metrics->setAttribute(
438           'coveredstatements', $projectStatistics['coveredStatements']
439         );
440
441         $metrics->setAttribute(
442           'elements',
443           $projectStatistics['conditionals'] +
444           $projectStatistics['statements']   +
445           $projectStatistics['methods']
446         );
447
448         $metrics->setAttribute(
449           'coveredelements',
450           $projectStatistics['coveredConditionals'] +
451           $projectStatistics['coveredStatements']   +
452           $projectStatistics['coveredMethods']
453         );
454
455         $project->appendChild($metrics);
456
457         if ($target !== NULL) {
458             if (!is_dir(dirname($target))) {
459               mkdir(dirname($target), 0777, TRUE);
460             }
461
462             return $document->save($target);
463         } else {
464             return $document->saveXML();
465         }
466     }
467 }