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 |
||
| 36 | class RaListingRepository extends EntityRepository |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var InstitutionAuthorizationRepositoryFilter |
||
| 40 | */ |
||
| 41 | private $authorizationRepositoryFilter; |
||
| 42 | |||
| 43 | public function __construct( |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param IdentityId $identityId The RA's identity id. |
||
| 54 | * @return null|RaListing[] |
||
|
|
|||
| 55 | */ |
||
| 56 | public function findByIdentityId(IdentityId $identityId) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param IdentityId $identityId The RA's identity id. |
||
| 63 | * @param Institution $raInstitution |
||
| 64 | * @return null|RaListing |
||
| 65 | */ |
||
| 66 | public function findByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution) |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * @param IdentityId $identityId The RA's identity id. |
||
| 77 | * @param Institution $raInstitution |
||
| 78 | * @param InstitutionAuthorizationContextInterface $authorizationContext |
||
| 79 | * @return null|RaListing |
||
| 80 | */ |
||
| 81 | public function findByIdentityIdAndRaInstitutionWithContext( |
||
| 82 | IdentityId $identityId, |
||
| 83 | Institution $raInstitution, |
||
| 84 | InstitutionAuthorizationContextInterface $authorizationContext |
||
| 85 | ) { |
||
| 86 | $queryBuilder = $this->createQueryBuilder('r') |
||
| 87 | ->where('r.identityId = :identityId') |
||
| 88 | ->andWhere('r.raInstitution = :raInstitution') |
||
| 89 | ->setParameter('identityId', $identityId) |
||
| 90 | ->setParameter('raInstitution', (string)$raInstitution) |
||
| 91 | ->orderBy('r.raInstitution'); |
||
| 92 | |||
| 93 | // Modify query to filter on authorization: |
||
| 94 | // For the RA listing we want identities that are already RA. Because we then need to look at the use_raa's |
||
| 95 | // we have to look at the RA-institutions because that's the institution the user is RA for and we should use |
||
| 96 | // those RA's. Hence the 'r.raInstitution'. |
||
| 97 | $this->authorizationRepositoryFilter->filter( |
||
| 98 | $queryBuilder, |
||
| 99 | $authorizationContext, |
||
| 100 | 'r.raInstitution', |
||
| 101 | 'iac' |
||
| 102 | ); |
||
| 103 | |||
| 104 | return $queryBuilder->getQuery()->getOneOrNullResult(); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param IdentityId $identityId The RA's identity id. |
||
| 109 | * @param Institution $institution |
||
| 110 | * @return RaListing[] |
||
| 111 | */ |
||
| 112 | public function findByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution) |
||
| 119 | |||
| 120 | public function save(RaListing $raListingEntry) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) The amount of if statements do not necessarily make the method |
||
| 128 | * @SuppressWarnings(PHPMD.NPathComplexity) below complex or hard to maintain. |
||
| 129 | * |
||
| 130 | * @param RaListingQuery $query |
||
| 131 | * @return \Doctrine\ORM\Query |
||
| 132 | */ |
||
| 133 | public function createSearchQuery(RaListingQuery $query) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param RaListingQuery $query |
||
| 203 | * @return \Doctrine\ORM\Query |
||
| 204 | */ |
||
| 205 | public function createOptionsQuery(RaListingQuery $query) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param Institution $raInstitution |
||
| 227 | * @return ArrayCollection |
||
| 228 | */ |
||
| 229 | public function listRasFor(Institution $raInstitution) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param IdentityId $identityId |
||
| 242 | * @return void |
||
| 243 | */ |
||
| 244 | View Code Duplication | public function removeByIdentityId(IdentityId $identityId) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @param IdentityId $identityId |
||
| 257 | * @param Institution $raInstitution |
||
| 258 | * @return void |
||
| 259 | */ |
||
| 260 | View Code Duplication | public function removeByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param IdentityId $identityId |
||
| 274 | * @param Institution $institution |
||
| 275 | * @return void |
||
| 276 | */ |
||
| 277 | View Code Duplication | public function removeByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution) |
|
| 288 | } |
||
| 289 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.