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 SimpleTable extends Render |
||
28 | { |
||
29 | /** |
||
30 | * Generates a "correct" table tag to contain the rendered rows |
||
31 | * |
||
32 | * @param Table $table |
||
33 | * @param array $rows |
||
34 | * @param array $headers |
||
35 | * @param array $footers |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | protected function container(Table $table, array $rows, array $headers, array $footers) |
||
53 | |||
54 | /** |
||
55 | * Generates a tr with the given rendered cells |
||
56 | * |
||
57 | * @param Row $row |
||
58 | * @param array $cells |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | protected function row(Row $row, array $cells) |
||
70 | |||
71 | /** |
||
72 | * Creates a td tag using the given Cell |
||
73 | * |
||
74 | * @param Cell $cell |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | View Code Duplication | protected function cell(Cell $cell) |
|
86 | |||
87 | /** |
||
88 | * Helper function to convert an array into a list of attributes and append |
||
89 | * them to a string |
||
90 | * |
||
91 | * @param string $html |
||
92 | * @param array $attributes |
||
93 | */ |
||
94 | protected function addAttributes(&$html, array $attributes) |
||
103 | |||
104 | protected function footerCell(Table\Cell $cell) |
||
108 | |||
109 | protected function footerRow(Table\Row $row, array $cells) |
||
113 | |||
114 | View Code Duplication | protected function headerCell(Table\Cell $cell) |
|
122 | |||
123 | protected function headerRow(Table\Row $row, array $cells) |
||
127 | |||
128 | } |
||
129 |
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.