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 |
||
| 41 | class SecondFactorService |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var \Surfnet\StepupMiddlewareClientBundle\Identity\Service\SecondFactorService |
||
| 45 | */ |
||
| 46 | private $secondFactors; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \Surfnet\StepupSelfService\SelfServiceBundle\Service\CommandService |
||
| 50 | */ |
||
| 51 | private $commandService; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \Surfnet\StepupSelfService\SelfServiceBundle\Service\U2fSecondFactorService |
||
| 55 | */ |
||
| 56 | private $u2fSecondFactorService; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param MiddlewareSecondFactorService $secondFactors |
||
| 60 | * @param CommandService $commandService |
||
| 61 | * @param U2fSecondFactorService $u2fSecondFactorService |
||
| 62 | */ |
||
| 63 | public function __construct( |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param string $identityId |
||
| 75 | * @param string $nonce |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | public function verifyEmail($identityId, $nonce) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param RevokeCommand $command |
||
| 91 | * @return bool |
||
| 92 | */ |
||
| 93 | public function revoke(RevokeCommand $command) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Returns whether the given registrant has registered second factors with Step-up. The state of the second factor |
||
| 116 | * is irrelevant. |
||
| 117 | * |
||
| 118 | * @param string $identityId |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | public function doSecondFactorsExistForIdentity($identityId) |
||
| 131 | |||
| 132 | public function identityHasSecondFactorOfStateWithId($identityId, $state, $secondFactorId) |
||
| 133 | { |
||
| 134 | switch ($state) { |
||
| 135 | case 'unverified': |
||
| 136 | $secondFactors = $this->findUnverifiedByIdentity($identityId); |
||
| 137 | break; |
||
| 138 | case 'verified': |
||
| 139 | $secondFactors = $this->findVerifiedByIdentity($identityId); |
||
| 140 | break; |
||
| 141 | case 'vetted': |
||
| 142 | $secondFactors = $this->findVettedByIdentity($identityId); |
||
| 143 | break; |
||
| 144 | default: |
||
| 145 | throw new LogicException(sprintf('Invalid second factor state "%s" given.', $state)); |
||
| 146 | } |
||
| 147 | |||
| 148 | if (count($secondFactors->getElements()) === 0) { |
||
| 149 | return false; |
||
| 150 | } |
||
| 151 | |||
| 152 | foreach ($secondFactors->getElements() as $secondFactor) { |
||
| 153 | if ($secondFactor->id === $secondFactorId) { |
||
| 154 | return true; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | return false; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns the given registrant's unverified second factors. |
||
| 163 | * |
||
| 164 | * @param string $identityId |
||
| 165 | * @return UnverifiedSecondFactorCollection |
||
|
|
|||
| 166 | */ |
||
| 167 | public function findUnverifiedByIdentity($identityId) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Returns the given registrant's verified second factors. |
||
| 176 | * |
||
| 177 | * @param string $identityId |
||
| 178 | * @return VerifiedSecondFactorCollection |
||
| 179 | */ |
||
| 180 | public function findVerifiedByIdentity($identityId) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Returns the given registrant's verified second factors. |
||
| 189 | * |
||
| 190 | * @param string $identityId |
||
| 191 | * @return VettedSecondFactorCollection |
||
| 192 | */ |
||
| 193 | public function findVettedByIdentity($identityId) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param string $secondFactorId |
||
| 202 | * @return null|UnverifiedSecondFactor |
||
| 203 | */ |
||
| 204 | public function findOneUnverified($secondFactorId) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $secondFactorId |
||
| 211 | * @return null|VerifiedSecondFactor |
||
| 212 | */ |
||
| 213 | public function findOneVerified($secondFactorId) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $secondFactorId |
||
| 220 | * @return null|VettedSecondFactor |
||
| 221 | */ |
||
| 222 | public function findOneVetted($secondFactorId) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $identityId |
||
| 229 | * @param string $verificationNonce |
||
| 230 | * @return UnverifiedSecondFactor|null |
||
| 231 | */ |
||
| 232 | View Code Duplication | public function findUnverifiedByVerificationNonce($identityId, $verificationNonce) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $secondFactorId |
||
| 254 | * @param string $identityId |
||
| 255 | * @return null|string |
||
| 256 | */ |
||
| 257 | View Code Duplication | public function getRegistrationCode($secondFactorId, $identityId) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param array $allSecondFactors |
||
| 278 | * @param UnverifiedSecondFactorCollection $unverifiedCollection |
||
| 279 | * @param VerifiedSecondFactorCollection $verifiedCollection |
||
| 280 | * @param VettedSecondFactorCollection $vettedCollection |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | private function determineAvailable( |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param array $allSecondFactors |
||
| 297 | * @param CollectionDto $collection |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | private function filterAvailableSecondFactors(array $allSecondFactors, CollectionDto $collection) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param $identity |
||
| 313 | * @param $allSecondFactors |
||
| 314 | * @param $allowedSecondFactors |
||
| 315 | * @return SecondFactorTypeCollection |
||
| 316 | */ |
||
| 317 | public function getSecondFactorsForIdentity($identity, $allSecondFactors, $allowedSecondFactors) |
||
| 340 | } |
||
| 341 |
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.