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 |
||
16 | trait ListTrait |
||
17 | { |
||
18 | /** |
||
19 | * @param string $id |
||
20 | * @param array $parts |
||
21 | * @param array $otherParameters |
||
22 | * @return array |
||
23 | */ |
||
24 | View Code Duplication | public function listById($id, array $parts = ['snippet'], array $otherParameters = []) |
|
33 | |||
34 | /** |
||
35 | * Get automatically only the video data array |
||
36 | * |
||
37 | * @param string $id |
||
38 | * @param array $parts |
||
39 | * @param array $otherParameters |
||
40 | * @return array |
||
41 | * @throws NotFoundItemException |
||
42 | */ |
||
43 | public function getById($id, array $parts = ['snippet'], array $otherParameters = []) |
||
52 | |||
53 | /** |
||
54 | * @param array $filters |
||
55 | * @param array $parts |
||
56 | * @param array $otherParameters |
||
57 | * @return array |
||
58 | */ |
||
59 | View Code Duplication | public function listBy(array $filters, array $parts = ['snippet'], array $otherParameters = []) |
|
69 | } |
||
70 |
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.