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 User 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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class User |
||
| 20 | { |
||
| 21 | /** @var EntityManager $em */ |
||
| 22 | protected $em; |
||
| 23 | |||
| 24 | /** @var PersonService */ |
||
| 25 | private $personSvc; |
||
| 26 | |||
| 27 | 22 | public function __construct(EntityManager $em, PersonService $personSvc) |
|
| 32 | |||
| 33 | /** |
||
| 34 | * @param array $data |
||
| 35 | * @return UserEntity |
||
| 36 | */ |
||
| 37 | 16 | public function createFromArray(array $data) |
|
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * @return array |
||
| 55 | */ |
||
| 56 | 1 | public function toArray(UserEntity $user) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @param UserEntity $user |
||
| 72 | * @return UserEntity |
||
| 73 | */ |
||
| 74 | 16 | public function saveUser(UserEntity $user) |
|
| 75 | { |
||
| 76 | 16 | return $this->getUserRepository()->save($user); |
|
| 77 | 1 | } |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @param int $id |
||
| 81 | * @return UserEntity|null |
||
| 82 | */ |
||
| 83 | 1 | View Code Duplication | public function findUserById($id) |
| 84 | { |
||
| 85 | 1 | $criteria = new UserCriteria(); |
|
| 86 | 1 | $criteria->setId($id); |
|
| 87 | 1 | $results = $this->getUserRepository()->findByCriteria($criteria); |
|
| 88 | 1 | return (count($results)) ? $results[0] : null; |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param string $email |
||
| 93 | * @return UserEntity|null |
||
| 94 | */ |
||
| 95 | 1 | View Code Duplication | public function findUserByEmail($email) |
| 96 | { |
||
| 97 | 1 | $criteria = new UserCriteria(); |
|
| 98 | 1 | $criteria->setEmail($email); |
|
| 99 | 1 | $result = $this->getUserRepository()->findByCriteria($criteria); |
|
| 100 | 1 | return count($result) ? $result[0] : null; |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return UserRepository |
||
| 105 | */ |
||
| 106 | 17 | private function getUserRepository() |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @return \Del\Repository\EmailLink |
||
| 113 | */ |
||
| 114 | 5 | private function getEmailLinkRepository() |
|
| 118 | |||
| 119 | 4 | public function registerUser(array $data) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param UserEntity $user |
||
| 155 | * @param $password |
||
| 156 | * @return UserEntity |
||
| 157 | */ |
||
| 158 | 6 | public function changePassword(UserEntity $user, $password) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @param UserEntity $user |
||
| 172 | * @param int $expiry_days |
||
| 173 | */ |
||
| 174 | 4 | public function generateEmailLink(UserEntity $user, $expiry_days = 7) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param EmailLink $link |
||
| 188 | */ |
||
| 189 | 4 | public function deleteEmailLink(EmailLink $link) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param UserEntity $user |
||
| 198 | */ |
||
| 199 | 16 | public function deleteUser(UserEntity $user) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @param $email |
||
| 208 | * @param $token |
||
| 209 | * @throws EmailLinkException |
||
| 210 | */ |
||
| 211 | 4 | public function findEmailLink($email, $token) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $email |
||
| 228 | * @param string $password |
||
| 229 | * @return int |
||
| 230 | * @throws UserException |
||
| 231 | */ |
||
| 232 | 6 | function authenticate($email, $password) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @param UserCriteria $criteria |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | 1 | public function findByCriteria(UserCriteria $criteria) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @param UserEntity $user |
||
| 282 | * @param $password |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | 1 | public function checkPassword(UserEntity $user, $password) |
|
| 292 | |||
| 293 | } |
||
| 294 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: