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 |
||
5 | class TableRow extends AbstractTableElement |
||
6 | { |
||
7 | /** |
||
8 | * @var Table |
||
9 | */ |
||
10 | protected $table; |
||
11 | |||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $cells = array(); |
||
16 | |||
17 | public function getTable() |
||
21 | |||
22 | public function setTable(Table $table = null) |
||
32 | |||
33 | public function getCells() |
||
37 | |||
38 | public function addCell(TableCell $cell) |
||
48 | |||
49 | public function removeCell(TableCell $cell) |
||
60 | |||
61 | /** |
||
62 | * @param $index |
||
63 | * |
||
64 | * @return TableCell|null |
||
65 | */ |
||
66 | public function getCell($index) |
||
70 | |||
71 | /** |
||
72 | * @param array $cells |
||
73 | * @param null $position |
||
74 | */ |
||
75 | View Code Duplication | public function insertCells($cells, $position = null) |
|
83 | } |
||
84 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: