Passed
Push — master ( 1a0ddb...73af58 )
by Kyle
47s queued 10s
created

src/main/php/PHPMD/Renderer/XMLRenderer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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('tool="phpmd" ');
61 2
        $writer->write('timestamp="' . date('c') . '">');
62
        $writer->write(PHP_EOL);
63 2
64 1
        foreach ($report->getRuleViolations() as $violation) {
65
            $fileName = $violation->getFileName();
66 1
67 View Code Duplication
            if ($this->fileName !== $fileName) {
0 ignored issues
show
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...
68 1
                // Not first file
69 1
                if ($this->fileName !== null) {
70
                    $writer->write('  </file>' . PHP_EOL);
71
                }
72 1
                // Store current file name
73
                $this->fileName = $fileName;
74 1
75
                $writer->write('  <file name="' . $fileName . '">' . PHP_EOL);
76
            }
77 1
78
            $rule = $violation->getRule();
79 1
80 1
            $writer->write('    <violation');
81 1
            $writer->write(' beginline="' . $violation->getBeginLine() . '"');
82 1
            $writer->write(' endline="' . $violation->getEndLine() . '"');
83 1
            $writer->write(' rule="' . $rule->getName() . '"');
84
            $writer->write(' ruleset="' . $rule->getRuleSetName() . '"');
85 1
86 1
            $this->maybeAdd('package', $violation->getNamespaceName());
87 1
            $this->maybeAdd('externalInfoUrl', $rule->getExternalInfoUrl());
88 1
            $this->maybeAdd('function', $violation->getFunctionName());
89 1
            $this->maybeAdd('class', $violation->getClassName());
90
            $this->maybeAdd('method', $violation->getMethodName());
91
            //$this->_maybeAdd('variable', $violation->getVariableName());
92 1
93 1
            $writer->write(' priority="' . $rule->getPriority() . '"');
94 1
            $writer->write('>' . PHP_EOL);
95 1
            $writer->write('      ' . htmlspecialchars($violation->getDescription()) . PHP_EOL);
96
            $writer->write('    </violation>' . PHP_EOL);
97
        }
98
99 2
        // Last file and at least one violation
100 1
        if ($this->fileName !== null) {
101
            $writer->write('  </file>' . PHP_EOL);
102
        }
103 2
104 1 View Code Duplication
        foreach ($report->getErrors() as $error) {
105 1
            $writer->write('  <error filename="');
106 1
            $writer->write($error->getFile());
107 1
            $writer->write('" msg="');
108 1
            $writer->write(htmlspecialchars($error->getMessage()));
109
            $writer->write('" />' . PHP_EOL);
110
        }
111 2
112 2
        $writer->write('</pmd>' . PHP_EOL);
113
    }
114
115
    /**
116
     * This method will write a xml attribute named <b>$attr</b> to the output
117
     * when the given <b>$value</b> is not an empty string and is not <b>null</b>.
118
     *
119
     * @param string $attr The xml attribute name.
120
     * @param string $value The attribute value.
121
     * @return void
122 1
     */
123
    protected function maybeAdd($attr, $value)
124 1
    {
125 1
        if ($value === null || trim($value) === '') {
126
            return;
127 1
        }
128 1
        $this->getWriter()->write(' ' . $attr . '="' . $value . '"');
129
    }
130
}
131