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 |
||
23 | class Districts extends Api |
||
24 | { |
||
25 | /** |
||
26 | * The response array. |
||
27 | * |
||
28 | * @var array|null |
||
29 | */ |
||
30 | protected $response = null; |
||
31 | |||
32 | /** |
||
33 | * The cache directory. |
||
34 | * |
||
35 | * @var string|null |
||
36 | */ |
||
37 | protected $cacheDir = __DIR__ . '/cache/'; |
||
38 | |||
39 | /** |
||
40 | * The cache file name. |
||
41 | * |
||
42 | * @var string|null |
||
43 | */ |
||
44 | protected $file = __DIR__ . '/cache/' . 'districts_'; |
||
45 | |||
46 | /** |
||
47 | * Returns a list of all the districts in a city. |
||
48 | * |
||
49 | * @param int $cityId |
||
50 | * @param string $lang |
||
51 | * @return Districts |
||
52 | */ |
||
53 | public function all($cityId = 1, $lang = 'A') |
||
77 | |||
78 | /** |
||
79 | * Returns a the response. |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | public function get() |
||
89 | |||
90 | /** |
||
91 | * Returns a specific district from a city by ID. |
||
92 | * |
||
93 | * @param int $districtId |
||
94 | * @return array |
||
95 | */ |
||
96 | View Code Duplication | public function getId(int $districtId) |
|
104 | |||
105 | /** |
||
106 | * Returns a specific district from a city by ID. |
||
107 | * |
||
108 | * @param int $districtId |
||
109 | * @return array |
||
110 | */ |
||
111 | public function byId(int $districtId) |
||
115 | |||
116 | /** |
||
117 | * Returns a specific district from a city by ID. |
||
118 | * |
||
119 | * @param int $districtId |
||
120 | * @return array |
||
121 | */ |
||
122 | public function id(int $districtId) |
||
126 | |||
127 | /** |
||
128 | * Returns a certian district from a city by name. |
||
129 | * |
||
130 | * @param string $districtName |
||
131 | * @return array |
||
132 | */ |
||
133 | View Code Duplication | public function getName($districtName) |
|
141 | |||
142 | /** |
||
143 | * Returns a certian district from a city by name. |
||
144 | * |
||
145 | * @param string $districtName |
||
146 | * @return array |
||
147 | */ |
||
148 | public function byName($districtName) |
||
152 | |||
153 | /** |
||
154 | * Returns a certian district from a city by name. |
||
155 | * |
||
156 | * @param string $districtName |
||
157 | * @return array |
||
158 | */ |
||
159 | public function named($districtName) |
||
163 | |||
164 | /** |
||
165 | * Returns a certian district from a city by name. |
||
166 | * |
||
167 | * @param string $districtName |
||
168 | * @return array |
||
169 | */ |
||
170 | public function districtName($districtName) |
||
174 | |||
175 | /** |
||
176 | * Check if all() method was called first. |
||
177 | * |
||
178 | * @return void |
||
179 | * @throws \BadMethodCallException |
||
180 | */ |
||
181 | protected function check() |
||
187 | } |
||
188 |
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.