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 |
||
| 9 | class TableRow extends AbstractTableElement |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var Table |
||
| 13 | */ |
||
| 14 | protected $table; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $cells = array(); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @return Table |
||
| 23 | */ |
||
| 24 | public function getTable() |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param Table|null $table |
||
| 31 | * |
||
| 32 | * @return $this |
||
| 33 | */ |
||
| 34 | public function setTable(Table $table = null) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | public function getCells() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param TableCell $cell |
||
| 55 | * |
||
| 56 | * @return $this |
||
| 57 | */ |
||
| 58 | public function addCell(TableCell $cell) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param TableCell $cell |
||
| 71 | */ |
||
| 72 | public function removeCell(TableCell $cell) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param int $index |
||
| 86 | * |
||
| 87 | * @return TableCell|null |
||
| 88 | */ |
||
| 89 | public function getCell($index) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param array $cells |
||
| 96 | * @param null|int $position |
||
| 97 | */ |
||
| 98 | View Code Duplication | public function insertCells($cells, $position = null) |
|
| 106 | } |
||
| 107 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: