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) |
|
| 64 | { |
||
| 65 | return array |
||
| 66 | ( |
||
| 67 | 1 | 'id' => $user->getID(), |
|
| 68 | 1 | 'email' => $user->getEmail(), |
|
| 69 | 1 | 'person' => $user->getPerson(), |
|
| 70 | 1 | 'password' => $user->getPassword(), |
|
| 71 | 1 | 'state' => $user->getState()->getValue(), |
|
| 72 | 1 | 'registrationDate' => is_null($user->getRegistrationDate()) ? null : $user->getRegistrationDate()->format('Y-m-d H:i:s'), |
|
| 73 | 1 | 'lastLoginDate' => is_null($user->getLastLoginDate()) ? null : $user->getLastLoginDate()->format('Y-m-d H:i:s'), |
|
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param User $user |
||
| 79 | * @return User |
||
| 80 | */ |
||
| 81 | 16 | public function saveUser(User $user) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @param int $id |
||
| 88 | * @return User|null |
||
| 89 | */ |
||
| 90 | View Code Duplication | public function findUserById($id) |
|
|
|
|||
| 91 | { |
||
| 92 | $criteria = new UserCriteria(); |
||
| 93 | $criteria->setId($id); |
||
| 94 | $results = $this->getUserRepository()->findByCriteria($criteria); |
||
| 95 | return (count($results)) ? $results[0] : null; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $email |
||
| 100 | * @return User|null |
||
| 101 | */ |
||
| 102 | View Code Duplication | public function findUserByEmail($email) |
|
| 103 | { |
||
| 104 | $criteria = new UserCriteria(); |
||
| 105 | $criteria->setEmail($email); |
||
| 106 | $result = $this->getUserRepository()->findByCriteria($criteria); |
||
| 107 | return count($result) ? $result[0] : null; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return UserRepository |
||
| 112 | */ |
||
| 113 | 20 | private function getUserRepository() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return \Del\Repository\EmailLink |
||
| 120 | */ |
||
| 121 | 1 | private function getEmailLinkRepository() |
|
| 125 | |||
| 126 | 4 | public function registerUser(array $data) |
|
| 127 | { |
||
| 128 | 4 | if (!isset($data['email']) || !isset($data['password']) || !isset($data['confirm'])) { |
|
| 129 | 1 | throw new InvalidArgumentException(); |
|
| 130 | } |
||
| 131 | 3 | if ($data['password'] !== $data['confirm']) { |
|
| 132 | 1 | throw new UserException(UserException::WRONG_PASSWORD); |
|
| 133 | } |
||
| 134 | |||
| 135 | 2 | $criteria = new UserCriteria(); |
|
| 136 | 2 | $criteria->setEmail($data['email']); |
|
| 137 | 2 | $user = $this->getUserRepository()->findByCriteria($criteria); |
|
| 138 | if(!empty($user)) { |
||
| 139 | throw new UserException(UserException::USER_EXISTS); |
||
| 140 | } |
||
| 141 | |||
| 142 | $person = new Person(); |
||
| 143 | /** @var User $user */ |
||
| 144 | $user = new $this->userClass(); |
||
| 145 | $state = new State(State::STATE_UNACTIVATED); |
||
| 146 | $user->setPerson($person) |
||
| 147 | ->setEmail($data['email']) |
||
| 148 | ->setRegistrationDate(new DateTime()) |
||
| 149 | ->setState($state); |
||
| 150 | |||
| 151 | $bcrypt = new Bcrypt(); |
||
| 152 | $bcrypt->setCost(14); |
||
| 153 | |||
| 154 | $encryptedPassword = $bcrypt->create($data['password']); |
||
| 155 | $user->setPassword($encryptedPassword); |
||
| 156 | |||
| 157 | $this->saveUser($user); |
||
| 158 | return $user; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param User $user |
||
| 163 | * @param $password |
||
| 164 | * @return User |
||
| 165 | */ |
||
| 166 | 6 | public function changePassword(User $user, $password) |
|
| 167 | { |
||
| 168 | 6 | $bcrypt = new Bcrypt(); |
|
| 169 | 6 | $bcrypt->setCost(14); |
|
| 170 | |||
| 171 | 6 | $encryptedPassword = $bcrypt->create($password); |
|
| 172 | 6 | $user->setPassword($encryptedPassword); |
|
| 173 | |||
| 174 | 6 | $this->saveUser($user); |
|
| 175 | return $user; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param User $user |
||
| 180 | * @param int $expiry_days |
||
| 181 | * @return EmailLink |
||
| 182 | */ |
||
| 183 | public function generateEmailLink(User $user, $expiry_days = 7) |
||
| 184 | { |
||
| 185 | $date = new DateTime(); |
||
| 186 | $date->modify('+'.$expiry_days.' days'); |
||
| 187 | $token = md5(uniqid(rand(), true)); |
||
| 188 | $link = new EmailLink(); |
||
| 189 | $link->setUser($user); |
||
| 190 | $link->setToken($token); |
||
| 191 | $link->setExpiryDate($date); |
||
| 192 | return $this->getEmailLinkRepository()->save($link); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param EmailLink $link |
||
| 197 | */ |
||
| 198 | public function deleteEmailLink(EmailLink $link) |
||
| 199 | { |
||
| 200 | /** @var EmailLink $link */ |
||
| 201 | $link = $this->em->merge($link); |
||
| 202 | $this->getEmailLinkRepository()->delete($link); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param User $user |
||
| 207 | */ |
||
| 208 | 1 | public function deleteUser(User $user, $deletePerson = false) |
|
| 209 | { |
||
| 210 | 1 | $this->getUserRepository()->delete($user,$deletePerson); |
|
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param $email |
||
| 215 | * @param $token |
||
| 216 | * @return EmailLink |
||
| 217 | * @throws EmailLinkException |
||
| 218 | */ |
||
| 219 | 1 | public function findEmailLink($email, $token) |
|
| 220 | { |
||
| 221 | 1 | $link = $this->getEmailLinkRepository()->findByToken($token); |
|
| 222 | if(!$link) { |
||
| 223 | throw new EmailLinkException(EmailLinkException::LINK_NOT_FOUND); |
||
| 224 | } |
||
| 225 | if($link->getUser()->getEmail() != $email) { |
||
| 226 | throw new EmailLinkException(EmailLinkException::LINK_NO_MATCH); |
||
| 227 | } |
||
| 228 | if($link->getExpiryDate() < new DateTime()) { |
||
| 229 | throw new EmailLinkException(EmailLinkException::LINK_EXPIRED); |
||
| 230 | } |
||
| 231 | return $link; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $email |
||
| 236 | * @param string $password |
||
| 237 | * @return int |
||
| 238 | * @throws UserException |
||
| 239 | */ |
||
| 240 | 1 | public function authenticate($email, $password) |
|
| 241 | { |
||
| 242 | 1 | $criteria = new UserCriteria(); |
|
| 243 | 1 | $criteria->setEmail($email); |
|
| 244 | |||
| 245 | 1 | $user = $this->getUserRepository()->findByCriteria($criteria); |
|
| 246 | |||
| 247 | if(empty($user)) { |
||
| 248 | throw new UserException(UserException::USER_NOT_FOUND); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** @var User $user */ |
||
| 252 | $user = $user[0]; |
||
| 253 | |||
| 254 | switch($user->getState()->getValue()) { |
||
| 255 | case State::STATE_UNACTIVATED: |
||
| 256 | throw new UserException(UserException::USER_UNACTIVATED); |
||
| 257 | case State::STATE_DISABLED: |
||
| 258 | case State::STATE_SUSPENDED: |
||
| 259 | throw new UserException(UserException::USER_DISABLED); |
||
| 260 | case State::STATE_BANNED: |
||
| 261 | throw new UserException(UserException::USER_BANNED); |
||
| 262 | } |
||
| 263 | |||
| 264 | $bcrypt = new Bcrypt(); |
||
| 265 | $bcrypt->setCost(14); |
||
| 266 | |||
| 267 | if(!$bcrypt->verify($password, $user->getPassword())) |
||
| 268 | { |
||
| 269 | throw new UserException(UserException::WRONG_PASSWORD); |
||
| 270 | } |
||
| 271 | |||
| 272 | return $user->getID(); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param UserCriteria $criteria |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | public function findByCriteria(UserCriteria $criteria) |
||
| 280 | { |
||
| 281 | return $this->getUserRepository()->findByCriteria($criteria); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param UserCriteria $criteria |
||
| 286 | * @return User|null |
||
| 287 | */ |
||
| 288 | 1 | public function findOneByCriteria(UserCriteria $criteria) |
|
| 289 | { |
||
| 290 | 1 | $results = $this->getUserRepository()->findByCriteria($criteria); |
|
| 291 | return count($results) > 0 ? $results[0] : null; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param User $user |
||
| 296 | * @param $password |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | public function checkPassword(User $user, $password) |
||
| 300 | { |
||
| 301 | $bcrypt = new Bcrypt(); |
||
| 302 | $bcrypt->setCost(14); |
||
| 303 | |||
| 304 | return $bcrypt->verify($password, $user->getPassword()); |
||
| 305 | } |
||
| 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.