Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
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() |
|
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) { |
|
|
|||
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) |
||
130 | } |
||
131 |
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.