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 IdentityRepository extends EntityRepository |
||
29 | { |
||
30 | /** |
||
31 | * @param string $id |
||
32 | * @return Identity|null |
||
33 | */ |
||
34 | public function find($id) |
||
35 | { |
||
36 | /** @var Identity|null $identity */ |
||
37 | $identity = parent::find($id); |
||
38 | |||
39 | return $identity; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param Identity $identity |
||
44 | */ |
||
45 | public function save(Identity $identity) |
||
46 | { |
||
47 | $entityManager = $this->getEntityManager(); |
||
48 | $entityManager->persist($identity); |
||
49 | $entityManager->flush(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param IdentityQuery $query |
||
54 | * @return \Doctrine\ORM\Query |
||
55 | */ |
||
56 | public function createSearchQuery(IdentityQuery $query) |
||
57 | { |
||
58 | $queryBuilder = $this->createQueryBuilder('i'); |
||
59 | |||
60 | $queryBuilder |
||
61 | ->where('i.institution = :institution') |
||
62 | ->setParameter('institution', $query->institution); |
||
63 | |||
64 | if ($query->nameId) { |
||
65 | $queryBuilder |
||
66 | ->andWhere('i.nameId = :nameId') |
||
67 | ->setParameter('nameId', $query->nameId); |
||
68 | } |
||
69 | |||
70 | if ($query->email) { |
||
71 | $queryBuilder |
||
72 | ->andWhere('MATCH_AGAINST(i.email, :email) > 0') |
||
73 | ->setParameter('email', $query->email); |
||
74 | } |
||
75 | |||
76 | if ($query->commonName) { |
||
77 | $queryBuilder |
||
78 | ->andWhere('MATCH_AGAINST(i.commonName, :commonName) > 0') |
||
79 | ->setParameter('commonName', $query->commonName); |
||
80 | } |
||
81 | |||
82 | return $queryBuilder->getQuery(); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param string[] $nameIds |
||
87 | * @return Identity[] Indexed by NameID. |
||
88 | */ |
||
89 | public function findByNameIdsIndexed(array $nameIds) |
||
90 | { |
||
91 | return $this->getEntityManager()->createQueryBuilder() |
||
92 | ->select('i') |
||
93 | ->from('Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity', 'i', 'i.nameId') |
||
94 | ->where('i.nameId IN (:nameIds)') |
||
95 | ->setParameter('nameIds', $nameIds) |
||
96 | ->getQuery() |
||
97 | ->getResult(); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param NameId $nameId |
||
102 | * @param Institution $institution |
||
103 | * @return Identity |
||
104 | */ |
||
105 | public function findOneByNameIdAndInstitution(NameId $nameId, Institution $institution) |
||
115 | |||
116 | View Code Duplication | public function removeByIdentityId(IdentityId $identityId) |
|
125 | } |
||
126 |
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.