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 given table is invalid |
||
| 40 | */ |
||
| 41 | 54 | public function __construct(array $table) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Creates a table from a given list. |
||
| 80 | * |
||
| 81 | * @param array $list One-dimensional array |
||
| 82 | * |
||
| 83 | * @return TableNode |
||
| 84 | * |
||
| 85 | * @throws NodeException If the given list is not a one-dimensional array |
||
| 86 | */ |
||
| 87 | 2 | public static function fromList(array $list) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Returns node type. |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | public function getNodeType() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns table hash, formed by columns (ColumnsHash). |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | 4 | public function getHash() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Returns table hash, formed by columns. |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | 19 | public function getColumnsHash() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Returns table hash, formed by rows. |
||
| 139 | * |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | 2 | public function getRowsHash() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Returns numerated table lines. |
||
| 155 | * Line numbers are keys, lines are values. |
||
| 156 | * |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | 4 | public function getTable() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Returns table rows. |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | 54 | public function getRows() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Returns table definition lines. |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | 5 | public function getLines() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Returns specific row in a table. |
||
| 186 | * |
||
| 187 | * @param integer $index Row number |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | * |
||
| 191 | * @throws NodeException If row with specified index does not exist |
||
| 192 | */ |
||
| 193 | 8 | View Code Duplication | public function getRow($index) |
| 203 | |||
| 204 | /** |
||
| 205 | * Returns specific column in a table. |
||
| 206 | * |
||
| 207 | * @param integer $index Column number |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | * |
||
| 211 | * @throws NodeException If column with specified index does not exist |
||
| 212 | */ |
||
| 213 | 1 | public function getColumn($index) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Returns line number at which specific row was defined. |
||
| 231 | * |
||
| 232 | * @param integer $index |
||
| 233 | * |
||
| 234 | * @return integer |
||
| 235 | * |
||
| 236 | * @throws NodeException If row with specified index does not exist |
||
| 237 | */ |
||
| 238 | 4 | View Code Duplication | public function getRowLine($index) |
| 248 | |||
| 249 | /** |
||
| 250 | * Converts row into delimited string. |
||
| 251 | * |
||
| 252 | * @param integer $rowNum Row number |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | 5 | public function getRowAsString($rowNum) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Converts row into delimited string. |
||
| 268 | * |
||
| 269 | * @param integer $rowNum Row number |
||
| 270 | * @param callable $wrapper Wrapper function |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function getRowAsStringWithWrappedValues($rowNum, $wrapper) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Converts entire table into string |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | 2 | public function getTableAsString() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Returns line number at which table was started. |
||
| 303 | * |
||
| 304 | * @return integer |
||
| 305 | */ |
||
| 306 | public function getLine() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Converts table into string |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public function __toString() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Retrieves a hash iterator. |
||
| 323 | * |
||
| 324 | * @return Iterator |
||
| 325 | */ |
||
| 326 | 1 | public function getIterator() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Pads string right. |
||
| 333 | * |
||
| 334 | * @param string $text Text to pad |
||
| 335 | * @param integer $length Length |
||
| 336 | * |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | 5 | protected function padRight($text, $length) |
|
| 347 | } |
||
| 348 |