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 |
||
7 | final class UpdateUser |
||
8 | { |
||
9 | /** |
||
10 | * @var User |
||
11 | */ |
||
12 | private $user; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $username; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $password; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $displayName; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $email; |
||
33 | |||
34 | /** |
||
35 | * UpdateUser constructor. |
||
36 | * |
||
37 | * @param UserInterface $user |
||
38 | * @param $username |
||
39 | * @param $password |
||
40 | * @param $displayName |
||
41 | */ |
||
42 | View Code Duplication | public function __construct(UserInterface $user, $username, $password, $displayName, $email) |
|
50 | |||
51 | /** |
||
52 | * Get the User. |
||
53 | * |
||
54 | * @return UserInterface |
||
55 | */ |
||
56 | public function getUser() |
||
60 | |||
61 | /** |
||
62 | * Get the username. |
||
63 | * |
||
64 | * @return string |
||
65 | */ |
||
66 | public function getUsername() |
||
70 | |||
71 | /** |
||
72 | * Get the password. |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | public function getPassword() |
||
80 | |||
81 | /** |
||
82 | * Get the displayName. |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | public function getDisplayName() |
||
90 | |||
91 | /** |
||
92 | * Get the email. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getEmail() |
||
100 | } |
||
101 |
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.