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 |
||
8 | class Matrix |
||
9 | { |
||
10 | /** |
||
11 | * @var Row[] |
||
12 | */ |
||
13 | private $rows = []; |
||
14 | |||
15 | /** |
||
16 | * @var Column[] |
||
17 | */ |
||
18 | private $columns = []; |
||
19 | |||
20 | /** |
||
21 | * @param Row[] $rowVectors |
||
22 | */ |
||
23 | 20 | public function __construct(array $rowVectors = []) |
|
29 | |||
30 | /** |
||
31 | * @param Row $row |
||
32 | */ |
||
33 | 6 | View Code Duplication | public function addRow(Row $row) |
42 | |||
43 | /** |
||
44 | * @param int $offset |
||
45 | * @param Row $row |
||
46 | */ |
||
47 | 6 | public function setRow($offset, Row $row) |
|
53 | |||
54 | /** |
||
55 | * @param int $offset |
||
56 | * |
||
57 | * @return Row |
||
58 | */ |
||
59 | 10 | View Code Duplication | public function getRow($offset) |
70 | |||
71 | /** |
||
72 | * @return Row[] |
||
73 | */ |
||
74 | 10 | public function getRows() |
|
78 | |||
79 | /** |
||
80 | * @param int $offset |
||
81 | * |
||
82 | * @return bool |
||
83 | */ |
||
84 | 8 | public function hasRow($offset) |
|
88 | |||
89 | /** |
||
90 | * @param Column $column |
||
91 | */ |
||
92 | 6 | View Code Duplication | public function addColumn(Column $column) |
101 | |||
102 | /** |
||
103 | * @param int $offset |
||
104 | * @param Column $column |
||
105 | */ |
||
106 | 6 | public function setColumn($offset, Column $column) |
|
112 | |||
113 | /** |
||
114 | * @param int $offset |
||
115 | * |
||
116 | * @return Column |
||
117 | */ |
||
118 | 10 | View Code Duplication | public function getColumn($offset) |
129 | |||
130 | /** |
||
131 | * @return Column[] |
||
132 | */ |
||
133 | 10 | public function getColumns() |
|
137 | |||
138 | /** |
||
139 | * @param int $offset |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | 8 | public function hasColumn($offset) |
|
147 | |||
148 | /** |
||
149 | * @param array $rows |
||
150 | * |
||
151 | * @return Matrix |
||
152 | */ |
||
153 | 2 | public static function createFromArray(array $rows) |
|
163 | } |
||
164 |
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.