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 |
||
28 | class AuditLogRepository extends EntityRepository |
||
29 | { |
||
30 | /** |
||
31 | * An array of event FQCNs that pertain to second factors (verification, vetting, revocation etc.). |
||
32 | * |
||
33 | * @var string[] |
||
34 | */ |
||
35 | private static $secondFactorEvents = [ |
||
36 | 'Surfnet\Stepup\Identity\Event\YubikeySecondFactorBootstrappedEvent', |
||
37 | 'Surfnet\Stepup\Identity\Event\GssfPossessionProvenEvent', |
||
38 | 'Surfnet\Stepup\Identity\Event\PhonePossessionProvenEvent', |
||
39 | 'Surfnet\Stepup\Identity\Event\YubikeyPossessionProvenEvent', |
||
40 | 'Surfnet\Stepup\Identity\Event\EmailVerifiedEvent', |
||
41 | 'Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent', |
||
42 | 'Surfnet\Stepup\Identity\Event\UnverifiedSecondFactorRevokedEvent', |
||
43 | 'Surfnet\Stepup\Identity\Event\VerifiedSecondFactorRevokedEvent', |
||
44 | 'Surfnet\Stepup\Identity\Event\VettedSecondFactorRevokedEvent', |
||
45 | 'Surfnet\Stepup\Identity\Event\CompliedWithUnverifiedSecondFactorRevocationEvent', |
||
46 | 'Surfnet\Stepup\Identity\Event\CompliedWithVerifiedSecondFactorRevocationEvent', |
||
47 | 'Surfnet\Stepup\Identity\Event\CompliedWithVettedSecondFactorRevocationEvent', |
||
48 | 'Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaaEvent', |
||
49 | 'Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaEvent', |
||
50 | 'Surfnet\Stepup\Identity\Event\AppointedAsRaaEvent', |
||
51 | 'Surfnet\Stepup\Identity\Event\AppointedAsRaEvent', |
||
52 | 'Surfnet\Stepup\Identity\Event\RegistrationAuthorityRetractedEvent', |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * @param SecondFactorAuditLogQuery $query |
||
57 | * @return Query |
||
58 | */ |
||
59 | public function createSecondFactorSearchQuery(SecondFactorAuditLogQuery $query) |
||
60 | { |
||
61 | $queryBuilder = $this |
||
62 | ->createQueryBuilder('al') |
||
63 | ->where('al.identityInstitution = :identityInstitution') |
||
64 | ->setParameter('identityInstitution', $query->identityInstitution) |
||
65 | ->andWhere('al.identityId = :identityId') |
||
66 | ->andWhere('al.event IN (:secondFactorEvents)') |
||
67 | ->setParameter('identityId', $query->identityId) |
||
68 | ->setParameter('secondFactorEvents', self::$secondFactorEvents); |
||
69 | |||
70 | switch ($query->orderBy) { |
||
71 | case 'secondFactorId': |
||
72 | case 'secondFactorType': |
||
73 | case 'event': |
||
74 | case 'recordedOn': |
||
75 | case 'actorId': |
||
76 | $queryBuilder->orderBy( |
||
77 | sprintf('al.%s', $query->orderBy), |
||
78 | $query->orderDirection === 'desc' ? 'DESC' : 'ASC' |
||
79 | ); |
||
80 | break; |
||
81 | default: |
||
82 | throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy)); |
||
83 | } |
||
84 | |||
85 | return $queryBuilder->getQuery(); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param IdentityId $actorId |
||
90 | * @return AuditLogEntry[] |
||
91 | */ |
||
92 | public function findEntriesWhereIdentityIsActorOnly(IdentityId $actorId) |
||
101 | |||
102 | /** |
||
103 | * @param IdentityId $identityId |
||
104 | * @return void |
||
105 | */ |
||
106 | View Code Duplication | public function removeByIdentityId(IdentityId $identityId) |
|
115 | |||
116 | /** |
||
117 | * @param AuditLogEntry $entry |
||
118 | */ |
||
119 | public function save(AuditLogEntry $entry) |
||
125 | |||
126 | public function saveAll(array $entries) |
||
136 | } |
||
137 |
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.