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 |
||
| 21 | class User extends AbstractRepository |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * {@inheritDoc} |
||
| 26 | */ |
||
| 27 | View Code Duplication | public function findBy(array $criteria, array $sort = null, $limit = null, $skip = null) |
|
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritDoc} |
||
| 39 | * @return null | UserInterface |
||
| 40 | */ |
||
| 41 | View Code Duplication | public function findOneBy(array $criteria) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritDoc} |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function createQueryBuilder($findDrafts = false) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Creates a User |
||
| 65 | * |
||
| 66 | * @see \Core\Repository\AbstractRepository::create() |
||
| 67 | * @return UserInterface |
||
| 68 | */ |
||
| 69 | public function create(array $data = null) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Finds user by profile identifier |
||
| 83 | * |
||
| 84 | * @param string $identifier |
||
| 85 | * @param string $provider |
||
| 86 | * @return UserInterface |
||
| 87 | */ |
||
| 88 | public function findByProfileIdentifier($identifier, $provider) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns true if profile is already assigned to anotherUser |
||
| 95 | * |
||
| 96 | * @param int $curentUserId |
||
| 97 | * @param string $identifier |
||
| 98 | * @param string $provider |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | public function isProfileAssignedToAnotherUser($curentUserId, $identifier, $provider) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Finds user by login name |
||
| 118 | * |
||
| 119 | * @param string $login |
||
| 120 | * @return UserInterface |
||
| 121 | */ |
||
| 122 | public function findByLogin($login) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param $email |
||
| 130 | * @param bool $isDraft |
||
| 131 | * |
||
| 132 | * @return UserInterface|null |
||
| 133 | */ |
||
| 134 | public function findByEmail($email, $isDraft = false) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Finds user by login name or email |
||
| 151 | * |
||
| 152 | * @param string $identity |
||
| 153 | * @param string $suffix |
||
| 154 | * |
||
| 155 | * @return UserInterface|null |
||
| 156 | */ |
||
| 157 | public function findByLoginOrEmail($identity, $suffix = '') |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Find an user by a token hash. |
||
| 171 | * |
||
| 172 | * @param string $tokenHash |
||
| 173 | * |
||
| 174 | * @return UserInterface|null |
||
| 175 | */ |
||
| 176 | public function findByToken($tokenHash) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Finds user by internal id |
||
| 188 | * |
||
| 189 | * @param array $ids |
||
| 190 | * @return \MongoCursor |
||
| 191 | */ |
||
| 192 | public function findByIds(array $ids) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Find user by query |
||
| 203 | * |
||
| 204 | * @param String $query |
||
| 205 | * @deprecated since 0.19 not used anymore and probably broken. |
||
| 206 | * @return object |
||
| 207 | */ |
||
| 208 | public function findByQuery($query) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Copy user info into the applications info Entity |
||
| 227 | * |
||
| 228 | * @param \Auth\Entity\Info $info |
||
| 229 | */ |
||
| 230 | public function copyUserInfo(Info $info) |
||
| 235 | } |
||
| 236 |
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.