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 |
||
| 22 | class User extends AbstractRepository |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * {@inheritDoc} |
||
| 27 | */ |
||
| 28 | public function findBy(array $criteria, array $sort = null, $limit = null, $skip = null) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Finds a document by its identifier |
||
| 47 | * |
||
| 48 | * @param string|object $id The identifier |
||
| 49 | * @param int $lockMode |
||
| 50 | * @param int $lockVersion |
||
|
|
|||
| 51 | * @param array $options |
||
| 52 | * @throws Mapping\MappingException |
||
| 53 | * @throws LockException |
||
| 54 | * @throws UserDeactivatedException |
||
| 55 | * @return null | UserInterface |
||
| 56 | */ |
||
| 57 | public function find($id, $lockMode = \Doctrine\ODM\MongoDB\LockMode::NONE, $lockVersion = null, array $options = []) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param array $criteria |
||
| 64 | * @param array $options |
||
| 65 | * @throws UserDeactivatedException |
||
| 66 | * @return null | UserInterface |
||
| 67 | */ |
||
| 68 | View Code Duplication | public function findOneBy(array $criteria, array $options = []) |
|
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritDoc} |
||
| 81 | */ |
||
| 82 | View Code Duplication | public function createQueryBuilder($findDrafts = false) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Creates a User |
||
| 93 | * |
||
| 94 | * @see \Core\Repository\AbstractRepository::create() |
||
| 95 | * @return UserInterface |
||
| 96 | */ |
||
| 97 | public function create(array $data = null) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Finds user by profile identifier |
||
| 111 | * |
||
| 112 | * @param string $identifier |
||
| 113 | * @param string $provider |
||
| 114 | * @param array $options |
||
| 115 | * @return UserInterface |
||
| 116 | */ |
||
| 117 | public function findByProfileIdentifier($identifier, $provider, array $options = []) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Returns true if profile is already assigned to anotherUser |
||
| 124 | * |
||
| 125 | * @param int $curentUserId |
||
| 126 | * @param string $identifier |
||
| 127 | * @param string $provider |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | public function isProfileAssignedToAnotherUser($curentUserId, $identifier, $provider) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Finds user by login name |
||
| 147 | * |
||
| 148 | * @param string $login |
||
| 149 | * @param array $options |
||
| 150 | * @return UserInterface |
||
| 151 | */ |
||
| 152 | public function findByLogin($login, array $options = []) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param $email |
||
| 160 | * @param bool $isDraft |
||
| 161 | * |
||
| 162 | * @return UserInterface|null |
||
| 163 | */ |
||
| 164 | public function findByEmail($email, $isDraft = false) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Finds user by login name or email |
||
| 181 | * |
||
| 182 | * @param string $identity |
||
| 183 | * @param string $suffix |
||
| 184 | * |
||
| 185 | * @return UserInterface|null |
||
| 186 | */ |
||
| 187 | public function findByLoginOrEmail($identity, $suffix = '') |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Find an user by a token hash. |
||
| 201 | * |
||
| 202 | * @param string $tokenHash |
||
| 203 | * |
||
| 204 | * @return UserInterface|null |
||
| 205 | */ |
||
| 206 | public function findByToken($tokenHash) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Finds user by internal id |
||
| 218 | * |
||
| 219 | * @param array $ids |
||
| 220 | * @return \MongoCursor |
||
| 221 | */ |
||
| 222 | public function findByIds(array $ids) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Find user by query |
||
| 233 | * |
||
| 234 | * @param String $query |
||
| 235 | * @deprecated since 0.19 not used anymore and probably broken. |
||
| 236 | * @return object |
||
| 237 | */ |
||
| 238 | public function findByQuery($query) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Copy user info into the applications info Entity |
||
| 257 | * |
||
| 258 | * @param \Auth\Entity\Info $info |
||
| 259 | */ |
||
| 260 | public function copyUserInfo(Info $info) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param UserInterface $user |
||
| 268 | * @param array $options |
||
| 269 | * @throws UserDeactivatedException |
||
| 270 | * @return null | UserInterface |
||
| 271 | */ |
||
| 272 | protected function assertEntity(UserInterface $user = null, array $options) |
||
| 281 | } |
||
| 282 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.