XMLRenderer::renderReport()   B
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 57

Duplication

Lines 7
Ratio 12.28 %

Code Coverage

Tests 37
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 16
nop 1
dl 7
loc 57
rs 8.3158
c 0
b 0
f 0
ccs 37
cts 37
cp 1
crap 6

How to fix   Long Method   

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 PHP Mess Detector.
4
 *
5
 * Copyright (c) Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Licensed under BSD License
9
 * For full copyright and license information, please see the LICENSE file.
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @author Manuel Pichler <[email protected]>
13
 * @copyright Manuel Pichler. All rights reserved.
14
 * @license https://opensource.org/licenses/bsd-license.php BSD License
15
 * @link http://phpmd.org/
16
 */
17
18
namespace PHPMD\Renderer;
19
20
use PHPMD\AbstractRenderer;
21
use PHPMD\PHPMD;
22
use PHPMD\Report;
23
24
/**
25
 * This class will render a Java-PMD compatible xml-report.
26
 */
27
class XMLRenderer extends AbstractRenderer
28
{
29
    /**
30
     * Temporary property that holds the name of the last rendered file, it is
31
     * used to detect the next processed file.
32
     *
33
     * @var string
34
     */
35
    private $fileName = null;
36
37
    /**
38
     * This method will be called on all renderers before the engine starts the
39
     * real report processing.
40
     *
41
     * @return void
42
     */
43 2
    public function start()
44
    {
45 2
        $this->getWriter()->write('<?xml version="1.0" encoding="UTF-8" ?>');
46 2
        $this->getWriter()->write(PHP_EOL);
47 2
    }
48
49
    /**
50
     * This method will be called when the engine has finished the source analysis
51
     * phase.
52
     *
53
     * @param \PHPMD\Report $report
54
     * @return void
55
     */
56 2
    public function renderReport(Report $report)
57
    {
58 2
        $writer = $this->getWriter();
59 2
        $writer->write('<pmd version="' . PHPMD::VERSION . '" ');
60 2
        $writer->write('timestamp="' . date('c') . '">');
61 2
        $writer->write(PHP_EOL);
62
63 2
        foreach ($report->getRuleViolations() as $violation) {
64 1
            $fileName = $violation->getFileName();
65
66 1
            if ($this->fileName !== $fileName) {
67
                // Not first file
68 1
                if ($this->fileName !== null) {
69 1
                    $writer->write('  </file>' . PHP_EOL);
70
                }
71
                // Store current file name
72 1
                $this->fileName = $fileName;
73
74 1
                $writer->write('  <file name="' . $fileName . '">' . PHP_EOL);
75
            }
76
77 1
            $rule = $violation->getRule();
78
79 1
            $writer->write('    <violation');
80 1
            $writer->write(' beginline="' . $violation->getBeginLine() . '"');
81 1
            $writer->write(' endline="' . $violation->getEndLine() . '"');
82 1
            $writer->write(' rule="' . $rule->getName() . '"');
83 1
            $writer->write(' ruleset="' . $rule->getRuleSetName() . '"');
84
85 1
            $this->maybeAdd('package', $violation->getNamespaceName());
86 1
            $this->maybeAdd('externalInfoUrl', $rule->getExternalInfoUrl());
87 1
            $this->maybeAdd('function', $violation->getFunctionName());
88 1
            $this->maybeAdd('class', $violation->getClassName());
89 1
            $this->maybeAdd('method', $violation->getMethodName());
90
            //$this->_maybeAdd('variable', $violation->getVariableName());
91
92 1
            $writer->write(' priority="' . $rule->getPriority() . '"');
93 1
            $writer->write('>' . PHP_EOL);
94 1
            $writer->write('      ' . $violation->getDescription() . PHP_EOL);
95 1
            $writer->write('    </violation>' . PHP_EOL);
96
        }
97
98
        // Last file and at least one violation
99 2
        if ($this->fileName !== null) {
100 1
            $writer->write('  </file>' . PHP_EOL);
101
        }
102
103 2 View Code Duplication
        foreach ($report->getErrors() as $error) {
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 1
            $writer->write('  <error filename="');
105 1
            $writer->write($error->getFile());
106 1
            $writer->write('" msg="');
107 1
            $writer->write(htmlspecialchars($error->getMessage()));
108 1
            $writer->write('" />' . PHP_EOL);
109
        }
110
111 2
        $writer->write('</pmd>' . PHP_EOL);
112 2
    }
113
114
    /**
115
     * This method will write a xml attribute named <b>$attr</b> to the output
116
     * when the given <b>$value</b> is not an empty string and is not <b>null</b>.
117
     *
118
     * @param string $attr  The xml attribute name.
119
     * @param string $value The attribute value.
120
     * @return void
121
     */
122 1
    private function maybeAdd($attr, $value)
123
    {
124 1
        if ($value === null || trim($value) === '') {
125 1
            return;
126
        }
127 1
        $this->getWriter()->write(' ' . $attr . '="' . $value . '"');
128 1
    }
129
}
130