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 |
||
31 | class RaSecondFactorRepository extends EntityRepository |
||
32 | { |
||
33 | /** |
||
34 | * @param string $id |
||
35 | * @return RaSecondFactor|null |
||
36 | */ |
||
37 | public function find($id) |
||
38 | { |
||
39 | /** @var RaSecondFactor|null $secondFactor */ |
||
40 | $secondFactor = parent::find($id); |
||
41 | |||
42 | return $secondFactor; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param string $identityId |
||
47 | * @return RaSecondFactor[] |
||
48 | */ |
||
49 | public function findByIdentityId($identityId) |
||
50 | { |
||
51 | return parent::findBy(['identityId' => $identityId]); |
||
|
|||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) The amount of if statements do not necessarily make the method |
||
56 | * below complex or hard to maintain. |
||
57 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
58 | * |
||
59 | * @param RaSecondFactorQuery $query |
||
60 | * @return Query |
||
61 | */ |
||
62 | public function createSearchQuery(RaSecondFactorQuery $query) |
||
63 | { |
||
64 | $queryBuilder = $this |
||
65 | ->createQueryBuilder('sf') |
||
66 | ->andWhere('sf.institution = :institution') |
||
67 | ->setParameter('institution', $query->institution); |
||
68 | |||
69 | if ($query->name) { |
||
70 | $queryBuilder->andWhere('sf.name LIKE :name')->setParameter('name', sprintf('%%%s%%', $query->name)); |
||
71 | } |
||
72 | |||
73 | if ($query->type) { |
||
74 | $queryBuilder->andWhere('sf.type = :type')->setParameter('type', $query->type); |
||
75 | } |
||
76 | |||
77 | if ($query->secondFactorId) { |
||
78 | $queryBuilder |
||
79 | ->andWhere('sf.secondFactorId = :secondFactorId') |
||
80 | ->setParameter('secondFactorId', $query->secondFactorId); |
||
81 | } |
||
82 | |||
83 | if ($query->email) { |
||
84 | $queryBuilder->andWhere('sf.email LIKE :email')->setParameter('email', sprintf('%%%s%%', $query->email)); |
||
85 | } |
||
86 | |||
87 | if ($query->status) { |
||
88 | $stringStatus = $query->status; |
||
89 | if (!SecondFactorStatus::isValidStatus($stringStatus)) { |
||
90 | throw new RuntimeException(sprintf( |
||
91 | 'Received invalid status "%s" in RaSecondFactorRepository::createSearchQuery', |
||
92 | is_object($stringStatus) ? get_class($stringStatus) : (string) $stringStatus |
||
93 | )); |
||
94 | } |
||
95 | |||
96 | // we need to resolve the string value to database value using the correct doctrine type. Normally this is |
||
97 | // done by doctrine itself, however the queries PagerFanta creates somehow manages to mangle this... |
||
98 | // so we do it by hand |
||
99 | $doctrineType = Type::getType(SecondFactorStatusType::NAME); |
||
100 | $secondFactorStatus = SecondFactorStatus::$stringStatus(); |
||
101 | |||
102 | $databaseValue = $doctrineType->convertToDatabaseValue( |
||
103 | $secondFactorStatus, |
||
104 | $this->getEntityManager()->getConnection()->getDatabasePlatform() |
||
105 | ); |
||
106 | |||
107 | $queryBuilder->andWhere('sf.status = :status')->setParameter('status', $databaseValue); |
||
108 | } |
||
109 | |||
110 | switch ($query->orderBy) { |
||
111 | case 'name': |
||
112 | case 'type': |
||
113 | case 'secondFactorId': |
||
114 | case 'email': |
||
115 | case 'status': |
||
116 | $queryBuilder->orderBy( |
||
117 | sprintf('sf.%s', $query->orderBy), |
||
118 | $query->orderDirection === 'desc' ? 'DESC' : 'ASC' |
||
119 | ); |
||
120 | break; |
||
121 | } |
||
122 | |||
123 | return $queryBuilder->getQuery(); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param IdentityId $identityId |
||
128 | * @return void |
||
129 | */ |
||
130 | View Code Duplication | public function removeByIdentityId(IdentityId $identityId) |
|
139 | |||
140 | public function save(RaSecondFactor $secondFactor) |
||
145 | |||
146 | /** |
||
147 | * @param RaSecondFactor[] $secondFactors |
||
148 | */ |
||
149 | public function saveAll(array $secondFactors) |
||
159 | } |
||
160 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.