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 |
||
13 | final class UserManagement extends HttpApi |
||
14 | { |
||
15 | /** |
||
16 | * @param string $user |
||
17 | * |
||
18 | * @throws Exception |
||
19 | * |
||
20 | * @return User|ResponseInterface |
||
21 | */ |
||
22 | public function show($user) |
||
23 | { |
||
24 | Assert::stringNotEmpty($user); |
||
25 | |||
26 | $response = $this->httpGet('/api/user/info', ['user' => $user]); |
||
27 | |||
28 | return $this->hydrateResponse($response, User::class); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @param string $user |
||
33 | * @param array $param Valid keys are email, name, gender, birthday, country |
||
34 | * |
||
35 | * @throws Exception |
||
36 | * |
||
37 | * @return User|ResponseInterface |
||
38 | */ |
||
39 | View Code Duplication | public function update($user, $param) |
|
48 | /** |
||
49 | * @param string $user the user ID we should merge personality data to. |
||
50 | * @param string $from the user ID we should merge personality data from. |
||
51 | * |
||
52 | * @throws Exception |
||
53 | * |
||
54 | * @return User|ResponseInterface |
||
55 | */ |
||
56 | public function merge($user, $from) |
||
67 | } |
||
68 |
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.