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 |
||
23 | class TableNode implements ArgumentInterface, IteratorAggregate |
||
24 | { |
||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $table; |
||
29 | /** |
||
30 | * @var integer |
||
31 | */ |
||
32 | private $maxLineLength = array(); |
||
33 | |||
34 | /** |
||
35 | * Initializes table. |
||
36 | * |
||
37 | * @param array $table Table in form of [$rowLineNumber => [$val1, $val2, $val3]] |
||
38 | * |
||
39 | * @throws NodeException If the number of columns is not the same in each row |
||
40 | */ |
||
41 | 52 | public function __construct(array $table) |
|
64 | |||
65 | /** |
||
66 | * Creates a table from a given list. |
||
67 | * |
||
68 | * @param array $list One-dimensional array |
||
69 | * |
||
70 | * @return TableNode |
||
71 | * |
||
72 | * @throws NodeException If the given list is not a one-dimensional array |
||
73 | */ |
||
74 | 2 | public static function fromList(array $list) |
|
85 | |||
86 | /** |
||
87 | * Returns node type. |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | public function getNodeType() |
||
95 | |||
96 | /** |
||
97 | * Returns table hash, formed by columns (ColumnsHash). |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | 4 | public function getHash() |
|
105 | |||
106 | /** |
||
107 | * Returns table hash, formed by columns. |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | 19 | public function getColumnsHash() |
|
123 | |||
124 | /** |
||
125 | * Returns table hash, formed by rows. |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | 2 | public function getRowsHash() |
|
139 | |||
140 | /** |
||
141 | * Returns numerated table lines. |
||
142 | * Line numbers are keys, lines are values. |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | 4 | public function getTable() |
|
150 | |||
151 | /** |
||
152 | * Returns table rows. |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | 52 | public function getRows() |
|
160 | |||
161 | /** |
||
162 | * Returns table definition lines. |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | 5 | public function getLines() |
|
170 | |||
171 | /** |
||
172 | * Returns specific row in a table. |
||
173 | * |
||
174 | * @param integer $index Row number |
||
175 | * |
||
176 | * @return array |
||
177 | * |
||
178 | * @throws NodeException If row with specified index does not exist |
||
179 | */ |
||
180 | 8 | View Code Duplication | public function getRow($index) |
190 | |||
191 | /** |
||
192 | * Returns specific column in a table. |
||
193 | * |
||
194 | * @param integer $index Column number |
||
195 | * |
||
196 | * @return array |
||
197 | * |
||
198 | * @throws NodeException If column with specified index does not exist |
||
199 | */ |
||
200 | 1 | public function getColumn($index) |
|
215 | |||
216 | /** |
||
217 | * Returns line number at which specific row was defined. |
||
218 | * |
||
219 | * @param integer $index |
||
220 | * |
||
221 | * @return integer |
||
222 | * |
||
223 | * @throws NodeException If row with specified index does not exist |
||
224 | */ |
||
225 | 4 | View Code Duplication | public function getRowLine($index) |
235 | |||
236 | /** |
||
237 | * Converts row into delimited string. |
||
238 | * |
||
239 | * @param integer $rowNum Row number |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | 5 | public function getRowAsString($rowNum) |
|
252 | |||
253 | /** |
||
254 | * Converts row into delimited string. |
||
255 | * |
||
256 | * @param integer $rowNum Row number |
||
257 | * @param callable $wrapper Wrapper function |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | public function getRowAsStringWithWrappedValues($rowNum, $wrapper) |
||
272 | |||
273 | /** |
||
274 | * Converts entire table into string |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | 2 | public function getTableAsString() |
|
287 | |||
288 | /** |
||
289 | * Returns line number at which table was started. |
||
290 | * |
||
291 | * @return integer |
||
292 | */ |
||
293 | public function getLine() |
||
297 | |||
298 | /** |
||
299 | * Converts table into string |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | public function __toString() |
||
307 | |||
308 | /** |
||
309 | * Retrieves a hash iterator. |
||
310 | * |
||
311 | * @return Iterator |
||
312 | */ |
||
313 | 1 | public function getIterator() |
|
317 | |||
318 | /** |
||
319 | * Pads string right. |
||
320 | * |
||
321 | * @param string $text Text to pad |
||
322 | * @param integer $length Length |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | 5 | protected function padRight($text, $length) |
|
334 | } |
||
335 |
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.