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 |
||
10 | class UserIdentifiers extends Model |
||
11 | { |
||
12 | /** |
||
13 | * Get a flat array of all the user IDs. |
||
14 | * |
||
15 | * @param string $status (Default: 'ACTIVE'). |
||
16 | * |
||
17 | * @return string[] |
||
18 | */ |
||
19 | View Code Duplication | public function all($status = 'ACTIVE') |
|
30 | |||
31 | /** |
||
32 | * Get all active user identifiers of a given type, like 'BARCODE' or 'UNIV_ID'. |
||
33 | * |
||
34 | * @param string $value |
||
35 | * @param string $status |
||
36 | * |
||
37 | * @return array |
||
38 | */ |
||
39 | View Code Duplication | public function allOfType($value, $status = 'ACTIVE') |
|
50 | |||
51 | /** |
||
52 | * Get the first active user identifier of a given type, like 'BARCODE' or 'UNIV_ID'. |
||
53 | * |
||
54 | * @param string $value |
||
55 | * @param string $status |
||
56 | * |
||
57 | * @return null|string |
||
58 | */ |
||
59 | public function firstOfType($value, $status = 'ACTIVE') |
||
67 | |||
68 | /** |
||
69 | * Get the first active barcode. |
||
70 | * |
||
71 | * @return null|string |
||
72 | */ |
||
73 | public function getBarcode() |
||
77 | |||
78 | /** |
||
79 | * Get all active barcodes. |
||
80 | * |
||
81 | * @return string[] |
||
82 | */ |
||
83 | public function getBarcodes() |
||
87 | |||
88 | /** |
||
89 | * Get the first active university id. |
||
90 | * |
||
91 | * @return null|string |
||
92 | */ |
||
93 | public function getUniversityId() |
||
97 | |||
98 | /** |
||
99 | * Get all active university ids. |
||
100 | * |
||
101 | * @return string[] |
||
102 | */ |
||
103 | public function getUniversityIds() |
||
107 | } |
||
108 |
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.