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 |
||
| 60 | class IdentityCommandHandler extends CommandHandler |
||
| 61 | { |
||
| 62 | /** |
||
| 63 | * @var \Surfnet\Stepup\Identity\EventSourcing\IdentityRepository |
||
| 64 | */ |
||
| 65 | private $repository; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var \Surfnet\Stepup\Identity\Entity\ConfigurableSettings |
||
| 69 | */ |
||
| 70 | private $configurableSettings; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param RepositoryInterface $repository |
||
| 74 | * @param ConfigurableSettings $configurableSettings |
||
| 75 | */ |
||
| 76 | public function __construct(RepositoryInterface $repository, ConfigurableSettings $configurableSettings) |
||
| 77 | { |
||
| 78 | $this->repository = $repository; |
||
|
|
|||
| 79 | $this->configurableSettings = $configurableSettings; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function handleCreateIdentityCommand(CreateIdentityCommand $command) |
||
| 83 | { |
||
| 84 | $preferredLocale = new Locale($command->preferredLocale); |
||
| 85 | $this->assertIsValidLocale($preferredLocale); |
||
| 86 | |||
| 87 | $identity = Identity::create( |
||
| 88 | new IdentityId($command->id), |
||
| 89 | new Institution($command->institution), |
||
| 90 | new NameId($command->nameId), |
||
| 91 | new CommonName($command->commonName), |
||
| 92 | new Email($command->email), |
||
| 93 | $preferredLocale |
||
| 94 | ); |
||
| 95 | |||
| 96 | $this->repository->save($identity); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function handleUpdateIdentityCommand(UpdateIdentityCommand $command) |
||
| 100 | { |
||
| 101 | /** @var IdentityApi $identity */ |
||
| 102 | $identity = $this->repository->load(new IdentityId($command->id)); |
||
| 103 | |||
| 104 | $identity->rename(new CommonName($command->commonName)); |
||
| 105 | $identity->changeEmail(new Email($command->email)); |
||
| 106 | |||
| 107 | $this->repository->save($identity); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function handleBootstrapIdentityWithYubikeySecondFactorCommand( |
||
| 111 | BootstrapIdentityWithYubikeySecondFactorCommand $command |
||
| 112 | ) { |
||
| 113 | $preferredLocale = new Locale($command->preferredLocale); |
||
| 114 | $this->assertIsValidLocale($preferredLocale); |
||
| 115 | |||
| 116 | // @todo add check if Identity does not already exist based on NameId |
||
| 117 | $identity = Identity::create( |
||
| 118 | new IdentityId($command->identityId), |
||
| 119 | new Institution($command->institution), |
||
| 120 | new NameId($command->nameId), |
||
| 121 | new CommonName($command->commonName), |
||
| 122 | new Email($command->email), |
||
| 123 | $preferredLocale |
||
| 124 | ); |
||
| 125 | |||
| 126 | $identity->bootstrapYubikeySecondFactor( |
||
| 127 | new SecondFactorId($command->secondFactorId), |
||
| 128 | new YubikeyPublicId($command->yubikeyPublicId) |
||
| 129 | ); |
||
| 130 | |||
| 131 | $this->repository->save($identity); |
||
| 132 | } |
||
| 133 | |||
| 134 | View Code Duplication | public function handleProveYubikeyPossessionCommand(ProveYubikeyPossessionCommand $command) |
|
| 135 | { |
||
| 136 | /** @var IdentityApi $identity */ |
||
| 137 | $identity = $this->repository->load(new IdentityId($command->identityId)); |
||
| 138 | |||
| 139 | $identity->provePossessionOfYubikey( |
||
| 140 | new SecondFactorId($command->secondFactorId), |
||
| 141 | new YubikeyPublicId($command->yubikeyPublicId), |
||
| 142 | $this->configurableSettings->createNewEmailVerificationWindow() |
||
| 143 | ); |
||
| 144 | |||
| 145 | $this->repository->save($identity); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param ProvePhonePossessionCommand $command |
||
| 150 | */ |
||
| 151 | View Code Duplication | public function handleProvePhonePossessionCommand(ProvePhonePossessionCommand $command) |
|
| 152 | { |
||
| 153 | /** @var IdentityApi $identity */ |
||
| 154 | $identity = $this->repository->load(new IdentityId($command->identityId)); |
||
| 155 | |||
| 156 | $identity->provePossessionOfPhone( |
||
| 157 | new SecondFactorId($command->secondFactorId), |
||
| 158 | new PhoneNumber($command->phoneNumber), |
||
| 159 | $this->configurableSettings->createNewEmailVerificationWindow() |
||
| 160 | ); |
||
| 161 | |||
| 162 | $this->repository->save($identity); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param ProveGssfPossessionCommand $command |
||
| 167 | */ |
||
| 168 | public function handleProveGssfPossessionCommand(ProveGssfPossessionCommand $command) |
||
| 169 | { |
||
| 170 | /** @var IdentityApi $identity */ |
||
| 171 | $identity = $this->repository->load(new IdentityId($command->identityId)); |
||
| 172 | |||
| 173 | $identity->provePossessionOfGssf( |
||
| 174 | new SecondFactorId($command->secondFactorId), |
||
| 175 | new StepupProvider($command->stepupProvider), |
||
| 176 | new GssfId($command->gssfId), |
||
| 177 | $this->configurableSettings->createNewEmailVerificationWindow() |
||
| 178 | ); |
||
| 179 | |||
| 180 | $this->repository->save($identity); |
||
| 181 | } |
||
| 182 | |||
| 183 | View Code Duplication | public function handleProveU2fDevicePossessionCommand(ProveU2fDevicePossessionCommand $command) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @param VerifyEmailCommand $command |
||
| 199 | */ |
||
| 200 | public function handleVerifyEmailCommand(VerifyEmailCommand $command) |
||
| 201 | { |
||
| 202 | /** @var IdentityApi $identity */ |
||
| 203 | $identity = $this->repository->load(new IdentityId($command->identityId)); |
||
| 204 | |||
| 205 | $identity->verifyEmail($command->verificationNonce); |
||
| 206 | |||
| 207 | $this->repository->save($identity); |
||
| 208 | } |
||
| 209 | |||
| 210 | public function handleVetSecondFactorCommand(VetSecondFactorCommand $command) |
||
| 236 | |||
| 237 | public function handleRevokeOwnSecondFactorCommand(RevokeOwnSecondFactorCommand $command) |
||
| 238 | { |
||
| 239 | /** @var IdentityApi $identity */ |
||
| 240 | $identity = $this->repository->load(new IdentityId($command->identityId)); |
||
| 241 | $identity->revokeSecondFactor(new SecondFactorId($command->secondFactorId)); |
||
| 242 | |||
| 245 | |||
| 246 | public function handleRevokeRegistrantsSecondFactorCommand(RevokeRegistrantsSecondFactorCommand $command) |
||
| 257 | |||
| 258 | public function handleExpressLocalePreferenceCommand(ExpressLocalePreferenceCommand $command) |
||
| 259 | { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param Locale $locale |
||
| 272 | */ |
||
| 273 | private function assertIsValidLocale(Locale $locale) |
||
| 281 | } |
||
| 282 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.