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 |
||
37 | View Code Duplication | class EeBunchSubject extends BunchSubject |
|
|
|||
38 | { |
||
39 | |||
40 | /** |
||
41 | * The row ID of the product that has been created recently. |
||
42 | * |
||
43 | * @var integer |
||
44 | */ |
||
45 | protected $lastRowId; |
||
46 | |||
47 | /** |
||
48 | * The mapping for the paths to the created row IDs. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $pathRowIdMapping = array(); |
||
53 | |||
54 | /** |
||
55 | * Intializes the previously loaded global data for exactly one bunch. |
||
56 | * |
||
57 | * @param string $serial The serial of the actual import |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | public function setUp($serial) |
||
80 | |||
81 | /** |
||
82 | * Clean up the global data after importing the variants. |
||
83 | * |
||
84 | * @param string $serial The serial of the actual import |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | public function tearDown($serial) |
||
105 | |||
106 | /** |
||
107 | * Set's the row ID of the product that has been created recently. |
||
108 | * |
||
109 | * @param string $lastRowId The row ID |
||
110 | * |
||
111 | * @return void |
||
112 | */ |
||
113 | 1 | public function setLastRowId($lastRowId) |
|
117 | |||
118 | /** |
||
119 | * Return's the row ID of the product that has been created recently. |
||
120 | * |
||
121 | * @return string The row Id |
||
122 | */ |
||
123 | 1 | public function getLastRowId() |
|
127 | |||
128 | /** |
||
129 | * Add the passed path => row ID mapping. |
||
130 | * |
||
131 | * @param string $path The path |
||
132 | * |
||
133 | * @return void |
||
134 | */ |
||
135 | public function addPathRowIdMapping($path) |
||
139 | |||
140 | /** |
||
141 | * Removes the mapping, e. g. when a category has been deleted. |
||
142 | * |
||
143 | * @param string $path The path to delete the mapping for |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | public function removePathRowIdMapping($path) |
||
151 | |||
152 | /** |
||
153 | * Return's the entity ID for the passed path. |
||
154 | * |
||
155 | * @param string $path The path to return the entity ID for |
||
156 | * |
||
157 | * @return integer The mapped entity ID |
||
158 | * @throws \Exception Is thrown, if the path can not be mapped |
||
159 | */ |
||
160 | public function mapPathRowId($path) |
||
175 | |||
176 | /** |
||
177 | * Return's the PK column name to create the product => attribute relation. |
||
178 | * |
||
179 | * @return string The PK column name |
||
180 | */ |
||
181 | protected function getPrimaryKeyMemberName() |
||
185 | |||
186 | /** |
||
187 | * Return's TRUE, if the passed URL key varchar value IS related with the actual PK. |
||
188 | * |
||
189 | * @param array $categoryVarcharAttribute The varchar value to check |
||
190 | * |
||
191 | * @return boolean TRUE if the URL key is related, else FALSE |
||
192 | */ |
||
193 | public function isUrlKeyOf(array $categoryVarcharAttribute) |
||
198 | } |
||
199 |
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.