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:
Complex classes like UserService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UserService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class UserService |
||
21 | { |
||
22 | /** @var EntityManager $em */ |
||
23 | protected $em; |
||
24 | |||
25 | /** @var PersonService */ |
||
26 | private $personSvc; |
||
27 | |||
28 | 22 | public function __construct(Container $c) |
|
33 | |||
34 | /** |
||
35 | * @param array $data |
||
36 | * @return User |
||
37 | */ |
||
38 | 16 | public function createFromArray(array $data) |
|
51 | |||
52 | |||
53 | |||
54 | |||
55 | /** |
||
56 | * @return array |
||
57 | */ |
||
58 | 1 | public function toArray(User $user) |
|
71 | |||
72 | /** |
||
73 | * @param User $user |
||
74 | * @return User |
||
75 | */ |
||
76 | 16 | public function saveUser(User $user) |
|
81 | |||
82 | /** |
||
83 | * @param int $id |
||
84 | * @return User|null |
||
85 | */ |
||
86 | 1 | View Code Duplication | public function findUserById($id) |
93 | |||
94 | /** |
||
95 | * @param string $email |
||
96 | * @return User|null |
||
97 | */ |
||
98 | 1 | View Code Duplication | public function findUserByEmail($email) |
105 | |||
106 | /** |
||
107 | * @return UserRepository |
||
108 | */ |
||
109 | 17 | private function getUserRepository() |
|
113 | |||
114 | /** |
||
115 | * @return \Del\Repository\EmailLink |
||
116 | */ |
||
117 | 5 | private function getEmailLinkRepository() |
|
121 | |||
122 | 4 | public function registerUser(array $data) |
|
155 | |||
156 | /** |
||
157 | * @param User $user |
||
158 | * @param $password |
||
159 | * @return User |
||
160 | */ |
||
161 | 6 | public function changePassword(User $user, $password) |
|
172 | |||
173 | /** |
||
174 | * @param User $user |
||
175 | * @param int $expiry_days |
||
176 | * @return EmailLink |
||
177 | */ |
||
178 | 4 | public function generateEmailLink(User $user, $expiry_days = 7) |
|
189 | |||
190 | /** |
||
191 | * @param EmailLink $link |
||
192 | */ |
||
193 | 4 | public function deleteEmailLink(EmailLink $link) |
|
199 | |||
200 | /** |
||
201 | * @param User $user |
||
202 | */ |
||
203 | 16 | public function deleteUser(User $user, $deletePerson = false) |
|
207 | |||
208 | /** |
||
209 | * @param $email |
||
210 | * @param $token |
||
211 | * @return EmailLink |
||
212 | * @throws EmailLinkException |
||
213 | */ |
||
214 | 4 | public function findEmailLink($email, $token) |
|
228 | |||
229 | /** |
||
230 | * @param string $email |
||
231 | * @param string $password |
||
232 | * @return int |
||
233 | * @throws UserException |
||
234 | */ |
||
235 | 6 | function authenticate($email, $password) |
|
273 | |||
274 | /** |
||
275 | * @param UserCriteria $criteria |
||
276 | * @return array |
||
277 | */ |
||
278 | 1 | public function findByCriteria(UserCriteria $criteria) |
|
282 | |||
283 | /** |
||
284 | * @param User $user |
||
285 | * @param $password |
||
286 | * @return bool |
||
287 | */ |
||
288 | 1 | public function checkPassword(User $user, $password) |
|
295 | |||
296 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.