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 |
||
13 | class CliDebugView extends DebugView |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * Render HTML header for development views |
||
18 | * |
||
19 | * @param HTTPRequest $httpRequest |
||
20 | * @return string |
||
21 | */ |
||
22 | public function renderHeader($httpRequest = null) |
||
26 | |||
27 | /** |
||
28 | * Render HTML footer for development views |
||
29 | */ |
||
30 | public function renderFooter() |
||
33 | |||
34 | /** |
||
35 | * Write information about the error to the screen |
||
36 | * |
||
37 | * @param string $httpRequest |
||
38 | * @param int $errno |
||
39 | * @param string $errstr |
||
40 | * @param string $errfile |
||
41 | * @param int $errline |
||
42 | * @return string |
||
43 | */ |
||
44 | public function renderError($httpRequest, $errno, $errstr, $errfile, $errline) |
||
56 | |||
57 | /** |
||
58 | * Write a fragment of the a source file |
||
59 | * |
||
60 | * @param array $lines An array of file lines; the keys should be the original line numbers |
||
61 | * @param int $errline Index of the line in $lines which has the error |
||
62 | * @return string |
||
63 | */ |
||
64 | public function renderSourceFragment($lines, $errline) |
||
76 | |||
77 | /** |
||
78 | * Write a backtrace |
||
79 | * |
||
80 | * @param array $trace |
||
81 | * @return string |
||
82 | */ |
||
83 | public function renderTrace($trace = null) |
||
90 | |||
91 | public function renderParagraph($text) |
||
95 | |||
96 | /** |
||
97 | * Render the information header for the view |
||
98 | * |
||
99 | * @param string $title |
||
100 | * @param string $subtitle |
||
101 | * @param string $description |
||
102 | * @return string |
||
103 | */ |
||
104 | public function renderInfo($title, $subtitle, $description = null) |
||
113 | |||
114 | public function renderVariable($val, $caller) |
||
132 | } |
||
133 |
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.