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 | /** @var string $userClass */ |
||
| 29 | private $userClass; |
||
| 30 | |||
| 31 | 25 | public function __construct(Container $c) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * @param array $data |
||
| 40 | * @return User |
||
| 41 | */ |
||
| 42 | 17 | public function createFromArray(array $data) |
|
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | 1 | public function toArray(User $user) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @param User $user |
||
| 79 | * @return User |
||
| 80 | */ |
||
| 81 | 18 | public function saveUser(User $user) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @param int $id |
||
| 88 | * @return User|null |
||
| 89 | */ |
||
| 90 | 1 | View Code Duplication | public function findUserById($id) |
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $email |
||
| 100 | * @return User|null |
||
| 101 | */ |
||
| 102 | 2 | View Code Duplication | public function findUserByEmail($email) |
| 109 | |||
| 110 | /** |
||
| 111 | * @return UserRepository |
||
| 112 | */ |
||
| 113 | 20 | private function getUserRepository() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return \Del\Repository\EmailLink |
||
| 120 | */ |
||
| 121 | 5 | private function getEmailLinkRepository() |
|
| 125 | |||
| 126 | 4 | public function registerUser(array $data) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param User $user |
||
| 163 | * @param $password |
||
| 164 | * @return User |
||
| 165 | */ |
||
| 166 | 7 | public function changePassword(User $user, $password) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param User $user |
||
| 180 | * @param int $expiry_days |
||
| 181 | * @return EmailLink |
||
| 182 | */ |
||
| 183 | 4 | public function generateEmailLink(User $user, $expiry_days = 7) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param EmailLink $link |
||
| 197 | */ |
||
| 198 | 4 | public function deleteEmailLink(EmailLink $link) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * @param User $user |
||
| 207 | */ |
||
| 208 | 18 | public function deleteUser(User $user, $deletePerson = false) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @param $email |
||
| 215 | * @param $token |
||
| 216 | * @return EmailLink |
||
| 217 | * @throws EmailLinkException |
||
| 218 | */ |
||
| 219 | 4 | public function findEmailLink($email, $token) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $email |
||
| 236 | * @param string $password |
||
| 237 | * @return int |
||
| 238 | * @throws UserException |
||
| 239 | */ |
||
| 240 | 6 | public function authenticate($email, $password) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @param UserCriteria $criteria |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | 1 | public function findByCriteria(UserCriteria $criteria) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param UserCriteria $criteria |
||
| 286 | * @return User|null |
||
| 287 | */ |
||
| 288 | 3 | public function findOneByCriteria(UserCriteria $criteria) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @param User $user |
||
| 296 | * @param $password |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | 1 | public function checkPassword(User $user, $password) |
|
| 306 | |||
| 307 | 25 | public function setUserClass($fullyQualifiedClassName) |
|
| 311 | } |
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.