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 |
||
22 | abstract class Render |
||
23 | { |
||
24 | /** |
||
25 | * Renders the given table into a string. Output depends on the subclass |
||
26 | * |
||
27 | * @param Table $table |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | public function renderTable(Table $table) |
||
42 | |||
43 | /** |
||
44 | * Renders the main body rows for the table |
||
45 | * |
||
46 | * @param Table $table |
||
47 | * |
||
48 | * @return array |
||
49 | */ |
||
50 | View Code Duplication | protected function buildRows(Table $table) |
|
70 | |||
71 | /** |
||
72 | * Renders the header rows for the table |
||
73 | * |
||
74 | * @param Table $table |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | View Code Duplication | protected function buildHeaders(Table $table) |
|
98 | |||
99 | /** |
||
100 | * Renders the footer rows for the table |
||
101 | * |
||
102 | * @param Table $table |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | View Code Duplication | protected function buildFooters(Table $table) |
|
126 | |||
127 | /** |
||
128 | * Should generate the container tag, eg: <table> |
||
129 | * |
||
130 | * @param Table $table |
||
131 | * @param array $rows |
||
132 | * @param array $headerRows |
||
133 | * @param array $footerRows |
||
134 | * |
||
135 | * @return mixed Should Ideally be a string that can be printed later |
||
136 | */ |
||
137 | protected abstract function container( |
||
143 | |||
144 | /** |
||
145 | * Renders a normal Row |
||
146 | * |
||
147 | * @param Row $row |
||
148 | * @param array $cells |
||
149 | * |
||
150 | * @return mixed Should ideally be a string that can be printed by container() |
||
151 | */ |
||
152 | protected abstract function row(Row $row, array $cells); |
||
153 | |||
154 | /** |
||
155 | * Renders a header Row |
||
156 | * |
||
157 | * @param Row $row |
||
158 | * @param array $cells |
||
159 | * |
||
160 | * @return mixed Should ideally be a string that can be printed by container() |
||
161 | */ |
||
162 | protected abstract function headerRow(Row $row, array $cells); |
||
163 | |||
164 | /** |
||
165 | * Renders a footer Row |
||
166 | * |
||
167 | * @param Row $row |
||
168 | * @param array $cells |
||
169 | * |
||
170 | * @return mixed Should ideally be a string that can be printed by |
||
171 | * container() |
||
172 | */ |
||
173 | protected abstract function footerRow(Row $row, array $cells); |
||
174 | |||
175 | /** |
||
176 | * Renders a normal cell |
||
177 | * |
||
178 | * @param Cell |
||
179 | */ |
||
180 | protected abstract function cell(Cell $cell); |
||
181 | |||
182 | /** |
||
183 | * Renders a header cell |
||
184 | * |
||
185 | * @param Cell |
||
186 | */ |
||
187 | protected abstract function headerCell(Cell $cell); |
||
188 | |||
189 | /** |
||
190 | * Renders a footer cell |
||
191 | * |
||
192 | * @param Cell |
||
193 | */ |
||
194 | protected abstract function footerCell(Cell $cell); |
||
195 | } |
||
196 |
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.