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 |
||
20 | class UserApi extends AbstractApi |
||
21 | { |
||
22 | /** |
||
23 | * @var UserFactory |
||
24 | */ |
||
25 | private $userFactory; |
||
26 | |||
27 | /** |
||
28 | * @var Serializer |
||
29 | */ |
||
30 | private $serializer; |
||
31 | |||
32 | public function __construct(ClientInterface $httpClient, LoggerInterface $logger, UserFactory $userFactory, Serializer $serializer) |
||
38 | |||
39 | public function isAuthDataValid(string $login, string $password): bool |
||
55 | |||
56 | public function authenticate(string $login, string $password): Auth |
||
78 | |||
79 | public function logout(Auth $auth): bool |
||
97 | |||
98 | /** |
||
99 | * Get user subscribers by user login |
||
100 | * |
||
101 | * @param string $login |
||
102 | * |
||
103 | * @return User[] |
||
104 | * |
||
105 | * @throws ApiException |
||
106 | * @throws InvalidResponseException |
||
107 | * @throws UserNotFoundException |
||
108 | */ |
||
109 | View Code Duplication | public function getUserSubscribersByLogin(string $login): array |
|
125 | |||
126 | /** |
||
127 | * Get user subscribers by user id |
||
128 | * |
||
129 | * @param int $id |
||
130 | * |
||
131 | * @return User[] |
||
132 | * |
||
133 | * @throws ApiException |
||
134 | * @throws InvalidResponseException |
||
135 | * @throws UserNotFoundException |
||
136 | */ |
||
137 | View Code Duplication | public function getUserSubscribersById(int $id): array |
|
153 | |||
154 | /** |
||
155 | * Get user subscriptions by user login |
||
156 | * |
||
157 | * @param string $login |
||
158 | * |
||
159 | * @return User[] |
||
160 | * |
||
161 | * @throws ApiException |
||
162 | * @throws InvalidResponseException |
||
163 | * @throws UserNotFoundException |
||
164 | */ |
||
165 | View Code Duplication | public function getUserSubscriptionsByLogin(string $login): array |
|
181 | |||
182 | /** |
||
183 | * Get user subscriptions by user id |
||
184 | * |
||
185 | * @param int $id |
||
186 | * |
||
187 | * @return User[] |
||
188 | * |
||
189 | * @throws ApiException |
||
190 | * @throws InvalidResponseException |
||
191 | * @throws UserNotFoundException |
||
192 | */ |
||
193 | View Code Duplication | public function getUserSubscriptionsById(int $id): array |
|
209 | |||
210 | /** |
||
211 | * Get single user by login |
||
212 | * |
||
213 | * @param string $login |
||
214 | * |
||
215 | * @return User |
||
216 | * |
||
217 | * @throws UserNotFoundException |
||
218 | * @throws RequestException |
||
219 | */ |
||
220 | View Code Duplication | public function getUserByLogin(string $login): User |
|
236 | |||
237 | /** |
||
238 | * Get single user by id |
||
239 | * |
||
240 | * @param int $id |
||
241 | * |
||
242 | * @return User |
||
243 | * |
||
244 | * @throws UserNotFoundException |
||
245 | * @throws RequestException |
||
246 | */ |
||
247 | View Code Duplication | public function getUserById(int $id): User |
|
263 | |||
264 | /** |
||
265 | * Finds and updates or create new user from API response data |
||
266 | * |
||
267 | * @param array $userInfo |
||
268 | * |
||
269 | * @return User |
||
270 | * |
||
271 | * @throws ApiException |
||
272 | * @throws InvalidResponseException |
||
273 | */ |
||
274 | private function getUserFromUserInfo(array $userInfo): User |
||
280 | |||
281 | /** |
||
282 | * Get array of User objects from API response containing user list |
||
283 | * |
||
284 | * @param array $users |
||
285 | * |
||
286 | * @return User[] |
||
287 | * |
||
288 | * @throws ApiException |
||
289 | * @throws InvalidResponseException |
||
290 | */ |
||
291 | private function getUsersFromList(array $users = []): array |
||
304 | } |
||
305 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.