These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace PHPMD\Renderer; |
||
4 | |||
5 | use PHPMD\PHPMD; |
||
6 | use PHPMD\Report; |
||
7 | use PHPMD\Renderer\XMLRenderer; |
||
8 | |||
9 | /** |
||
10 | * This class will render a Java-checkstyle compatible xml-report. |
||
11 | * for use with cs2pr and others |
||
12 | */ |
||
13 | class CheckStyleRenderer extends XMLRenderer |
||
14 | { |
||
15 | /** |
||
16 | * Temporary property that holds the name of the last rendered file, it is |
||
17 | * used to detect the next processed file. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | private $fileName; |
||
22 | |||
23 | /** |
||
24 | * Get a violation severity level according to the priority |
||
25 | * of the rule that's being broken |
||
26 | * @see https://checkstyle.sourceforge.io/version/4.4/property_types.html#severity |
||
27 | * - priority 1 maps to error level severity |
||
28 | * - priority 2 maps to warning level severity |
||
29 | * - priority > 2 maps to info level severity |
||
30 | * |
||
31 | * @param integer $priority priority of the broken rule |
||
32 | * @return string either error, warning or info |
||
33 | */ |
||
34 | protected function mapPriorityToSeverity($priority) |
||
35 | { |
||
36 | if ($priority > 2) { |
||
37 | return 'info'; |
||
38 | } |
||
39 | |||
40 | return (int)$priority === 2 ? 'warning' : 'error'; |
||
41 | } |
||
42 | /** |
||
43 | * This method will be called when the engine has finished the source analysis |
||
44 | * phase. |
||
45 | * |
||
46 | * @param \PHPMD\Report $report |
||
47 | */ |
||
48 | public function renderReport(Report $report) |
||
49 | { |
||
50 | $writer = $this->getWriter(); |
||
51 | $writer->write('<checkstyle>'); |
||
52 | $writer->write(\PHP_EOL); |
||
53 | |||
54 | foreach ($report->getRuleViolations() as $violation) { |
||
55 | $fileName = $violation->getFileName(); |
||
56 | |||
57 | View Code Duplication | if ($this->fileName !== $fileName) { |
|
0 ignored issues
–
show
|
|||
58 | // Not first file |
||
59 | if (null !== $this->fileName) { |
||
60 | $writer->write(' </file>' . \PHP_EOL); |
||
61 | } |
||
62 | // Store current file name |
||
63 | $this->fileName = $fileName; |
||
64 | |||
65 | $writer->write(' <file name="' . $fileName . '">' . \PHP_EOL); |
||
66 | } |
||
67 | |||
68 | $rule = $violation->getRule(); |
||
69 | |||
70 | $writer->write(' <error'); |
||
71 | $writer->write(' line="' . $violation->getBeginLine() . '"'); |
||
72 | $writer->write(' endline="' . $violation->getEndLine() . '"'); |
||
73 | $writer->write(\sprintf(' severity="%s"', $this->mapPriorityToSeverity($rule->getPriority()))); |
||
74 | $writer->write(\sprintf( |
||
75 | ' message="%s (%s, %s) "', |
||
76 | \htmlspecialchars($violation->getDescription()), |
||
77 | $rule->getName(), |
||
78 | $rule->getRuleSetName() |
||
79 | )); |
||
80 | |||
81 | $this->maybeAdd('package', $violation->getNamespaceName()); |
||
82 | $this->maybeAdd('externalInfoUrl', $rule->getExternalInfoUrl()); |
||
83 | $this->maybeAdd('function', $violation->getFunctionName()); |
||
84 | $this->maybeAdd('class', $violation->getClassName()); |
||
85 | $this->maybeAdd('method', $violation->getMethodName()); |
||
86 | //$this->_maybeAdd('variable', $violation->getVariableName()); |
||
87 | |||
88 | $writer->write(' />' . \PHP_EOL); |
||
89 | } |
||
90 | |||
91 | // Last file and at least one violation |
||
92 | if (null !== $this->fileName) { |
||
93 | $writer->write(' </file>' . \PHP_EOL); |
||
94 | } |
||
95 | |||
96 | foreach ($report->getErrors() as $error) { |
||
97 | $writer->write(' <file name="' . $error->getFile() . '">'); |
||
98 | $writer->write($error->getFile()); |
||
99 | $writer->write('<error msg="'); |
||
100 | $writer->write(\htmlspecialchars($error->getMessage())); |
||
101 | $writer->write(' severity="error" />' . \PHP_EOL); |
||
102 | } |
||
103 | |||
104 | $writer->write('</checkstyle>' . \PHP_EOL); |
||
105 | } |
||
106 | } |
||
107 |
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.