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 |
||
43 | class CirclesController extends Controller { |
||
44 | |||
45 | /** @var string */ |
||
46 | private $userId; |
||
47 | /** @var IL10N */ |
||
48 | private $l10n; |
||
49 | /** @var ConfigService */ |
||
50 | private $configService; |
||
51 | /** @var CirclesService */ |
||
52 | private $circlesService; |
||
53 | /** @var MiscService */ |
||
54 | private $miscService; |
||
55 | |||
56 | View Code Duplication | public function __construct( |
|
73 | |||
74 | |||
75 | /** |
||
76 | * @NoAdminRequired |
||
77 | * @NoSubAdminRequired |
||
78 | * |
||
79 | * @param $type |
||
80 | * @param string $name |
||
81 | * |
||
82 | * @return DataResponse |
||
83 | */ |
||
84 | public function create($type, $name) { |
||
126 | |||
127 | |||
128 | /** |
||
129 | * @NoAdminRequired |
||
130 | * @NoSubAdminRequired |
||
131 | * |
||
132 | * @param $type |
||
133 | * @param string $name |
||
134 | * |
||
135 | * @return DataResponse |
||
136 | */ |
||
137 | View Code Duplication | public function list($type, $name = '') { |
|
161 | |||
162 | |||
163 | /** |
||
164 | * @NoAdminRequired |
||
165 | * @NoSubAdminRequired |
||
166 | * |
||
167 | * @param $id |
||
168 | * |
||
169 | * @return DataResponse |
||
170 | * @internal param string $name |
||
171 | * |
||
172 | */ |
||
173 | View Code Duplication | public function details($id) { |
|
197 | |||
198 | |||
199 | /** |
||
200 | * @NoAdminRequired |
||
201 | * @NoSubAdminRequired |
||
202 | * |
||
203 | * @param $id |
||
204 | * |
||
205 | * @return DataResponse |
||
206 | * @internal param string $name |
||
207 | * |
||
208 | */ |
||
209 | View Code Duplication | public function join($id) { |
|
233 | |||
234 | |||
235 | /** |
||
236 | * @NoAdminRequired |
||
237 | * @NoSubAdminRequired |
||
238 | * |
||
239 | * @param $id |
||
240 | * |
||
241 | * @return DataResponse |
||
242 | * @internal param string $name |
||
243 | * |
||
244 | */ |
||
245 | View Code Duplication | public function leave($id) { |
|
268 | |||
269 | |||
270 | |||
271 | |||
272 | |||
273 | |||
274 | |||
275 | |||
276 | /** |
||
277 | * @NoAdminRequired |
||
278 | * @NoSubAdminRequired |
||
279 | * |
||
280 | * @param int $id |
||
281 | * |
||
282 | * @return DataResponse |
||
283 | */ |
||
284 | // public function delete($id) { |
||
285 | // $affectedRows = $this->dbHandler->deleteTeam($id, $this->userId); |
||
286 | // |
||
287 | // if ($affectedRows === 1) { |
||
288 | // return new DataResponse( |
||
289 | // [], |
||
290 | // Http::STATUS_NO_CONTENT |
||
291 | // ); |
||
292 | // } |
||
293 | // |
||
294 | // return new DataResponse( |
||
295 | // [ |
||
296 | // 'message' => (string)$this->l10n->t('Unable to delete team.') |
||
297 | // ], |
||
298 | // Http::STATUS_INTERNAL_SERVER_ERROR |
||
299 | // ); |
||
300 | // } |
||
301 | |||
302 | |||
303 | } |
||
304 | |||
305 |
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.