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 |
||
| 27 | class VerifiedSecondFactorRepository extends EntityRepository |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @param string $id |
||
| 31 | * @return VerifiedSecondFactor|null |
||
| 32 | */ |
||
| 33 | public function find($id) |
||
| 34 | { |
||
| 35 | /** @var VerifiedSecondFactor|null $secondFactor */ |
||
| 36 | $secondFactor = parent::find($id); |
||
| 37 | |||
| 38 | return $secondFactor; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param VerifiedSecondFactorQuery $query |
||
| 43 | * @return Query |
||
| 44 | */ |
||
| 45 | public function createSearchQuery(VerifiedSecondFactorQuery $query) |
||
| 46 | { |
||
| 47 | $queryBuilder = $this->createQueryBuilder('sf'); |
||
| 48 | |||
| 49 | if ($query->identityId) { |
||
| 50 | $queryBuilder |
||
| 51 | ->andWhere('sf.identityId = :identityId') |
||
| 52 | ->setParameter('identityId', (string) $query->identityId); |
||
| 53 | } |
||
| 54 | |||
| 55 | if ($query->secondFactorId) { |
||
| 56 | $queryBuilder |
||
| 57 | ->andWhere('sf.id = :secondFactorId') |
||
| 58 | ->setParameter('secondFactorId', (string) $query->secondFactorId); |
||
| 59 | } |
||
| 60 | |||
| 61 | if (is_string($query->registrationCode)) { |
||
| 62 | $queryBuilder |
||
| 63 | ->andWhere('sf.registrationCode = :registrationCode') |
||
| 64 | ->setParameter('registrationCode', $query->registrationCode); |
||
| 65 | } |
||
| 66 | |||
| 67 | return $queryBuilder->getQuery(); |
||
| 68 | } |
||
| 69 | |||
| 70 | View Code Duplication | public function removeByIdentityId(IdentityId $identityId) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @param VerifiedSecondFactor $secondFactor |
||
| 82 | */ |
||
| 83 | public function save(VerifiedSecondFactor $secondFactor) |
||
| 88 | |||
| 89 | public function remove(VerifiedSecondFactor $secondFactor) |
||
| 94 | } |
||
| 95 |
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.