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 |
||
| 70 | class IdentityCommandHandler extends SimpleCommandHandler |
||
| 71 | { |
||
| 72 | /** |
||
| 73 | * @var \Surfnet\Stepup\Identity\EventSourcing\IdentityRepository |
||
| 74 | */ |
||
| 75 | private $eventSourcedRepository; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var IdentityRepository |
||
| 79 | */ |
||
| 80 | private $identityProjectionRepository; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var \Surfnet\Stepup\Identity\Entity\ConfigurableSettings |
||
| 84 | */ |
||
| 85 | private $configurableSettings; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var AllowedSecondFactorListService |
||
| 89 | */ |
||
| 90 | private $allowedSecondFactorListService; |
||
| 91 | |||
| 92 | /** @var SecondFactorTypeService */ |
||
| 93 | private $secondFactorTypeService; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var InstitutionConfigurationOptionsService |
||
| 97 | */ |
||
| 98 | private $institutionConfigurationOptionsService; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var InstitutionConfigurationRepository |
||
| 102 | */ |
||
| 103 | private $institutionConfigurationRepository; |
||
| 104 | /** |
||
| 105 | * @var SecondFactorProvePossessionHelper |
||
| 106 | */ |
||
| 107 | private $provePossessionHelper; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param RepositoryInterface $eventSourcedRepository |
||
| 111 | * @param IdentityRepository $identityProjectionRepository |
||
| 112 | * @param ConfigurableSettings $configurableSettings |
||
| 113 | * @param AllowedSecondFactorListService $allowedSecondFactorListService |
||
| 114 | * @param SecondFactorTypeService $secondFactorTypeService |
||
| 115 | * @param SecondFactorProvePossessionHelper $provePossessionHelper |
||
| 116 | * @param InstitutionConfigurationOptionsService $institutionConfigurationOptionsService |
||
| 117 | * @param InstitutionConfigurationRepository $institutionConfigurationRepository |
||
| 118 | */ |
||
| 119 | public function __construct( |
||
| 120 | RepositoryInterface $eventSourcedRepository, |
||
| 121 | IdentityRepository $identityProjectionRepository, |
||
| 122 | ConfigurableSettings $configurableSettings, |
||
| 123 | AllowedSecondFactorListService $allowedSecondFactorListService, |
||
| 124 | SecondFactorTypeService $secondFactorTypeService, |
||
| 125 | SecondFactorProvePossessionHelper $provePossessionHelper, |
||
| 126 | InstitutionConfigurationOptionsService $institutionConfigurationOptionsService, |
||
| 127 | InstitutionConfigurationRepository $institutionConfigurationRepository |
||
| 128 | ) { |
||
| 129 | $this->eventSourcedRepository = $eventSourcedRepository; |
||
|
|
|||
| 130 | $this->identityProjectionRepository = $identityProjectionRepository; |
||
| 131 | $this->configurableSettings = $configurableSettings; |
||
| 132 | $this->allowedSecondFactorListService = $allowedSecondFactorListService; |
||
| 133 | $this->secondFactorTypeService = $secondFactorTypeService; |
||
| 134 | $this->provePossessionHelper = $provePossessionHelper; |
||
| 135 | $this->institutionConfigurationOptionsService = $institutionConfigurationOptionsService; |
||
| 136 | $this->institutionConfigurationRepository = $institutionConfigurationRepository; |
||
| 137 | } |
||
| 138 | |||
| 139 | public function handleCreateIdentityCommand(CreateIdentityCommand $command) |
||
| 140 | { |
||
| 141 | $preferredLocale = new Locale($command->preferredLocale); |
||
| 142 | $this->assertIsValidLocale($preferredLocale); |
||
| 143 | |||
| 144 | $identity = Identity::create( |
||
| 145 | new IdentityId($command->id), |
||
| 146 | new Institution($command->institution), |
||
| 147 | new NameId($command->nameId), |
||
| 148 | new CommonName($command->commonName), |
||
| 149 | new Email($command->email), |
||
| 150 | $preferredLocale |
||
| 151 | ); |
||
| 152 | |||
| 153 | $this->eventSourcedRepository->save($identity); |
||
| 154 | } |
||
| 155 | |||
| 156 | public function handleUpdateIdentityCommand(UpdateIdentityCommand $command) |
||
| 157 | { |
||
| 158 | /** @var IdentityApi $identity */ |
||
| 159 | $identity = $this->eventSourcedRepository->load(new IdentityId($command->id)); |
||
| 160 | |||
| 161 | $identity->rename(new CommonName($command->commonName)); |
||
| 162 | $identity->changeEmail(new Email($command->email)); |
||
| 163 | |||
| 164 | $this->eventSourcedRepository->save($identity); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function handleBootstrapIdentityWithYubikeySecondFactorCommand( |
||
| 168 | BootstrapIdentityWithYubikeySecondFactorCommand $command |
||
| 169 | ) { |
||
| 170 | $preferredLocale = new Locale($command->preferredLocale); |
||
| 171 | $this->assertIsValidLocale($preferredLocale); |
||
| 172 | |||
| 173 | $institution = new Institution($command->institution); |
||
| 174 | $nameId = new NameId($command->nameId); |
||
| 175 | |||
| 176 | if ($this->identityProjectionRepository->hasIdentityWithNameIdAndInstitution($nameId, $institution)) { |
||
| 177 | throw DuplicateIdentityException::forBootstrappingWithYubikeySecondFactor($nameId, $institution); |
||
| 178 | } |
||
| 179 | |||
| 180 | $identity = Identity::create( |
||
| 181 | new IdentityId($command->identityId), |
||
| 182 | $institution, |
||
| 183 | $nameId, |
||
| 184 | new CommonName($command->commonName), |
||
| 185 | new Email($command->email), |
||
| 186 | $preferredLocale |
||
| 187 | ); |
||
| 188 | |||
| 189 | $configurationInstitution = new ConfigurationInstitution( |
||
| 190 | (string) $identity->getInstitution() |
||
| 191 | ); |
||
| 192 | |||
| 193 | $tokenCount = $this->institutionConfigurationOptionsService->getMaxNumberOfTokensFor($configurationInstitution); |
||
| 194 | $identity->setMaxNumberOfTokens($tokenCount); |
||
| 195 | |||
| 196 | $identity->bootstrapYubikeySecondFactor( |
||
| 197 | new SecondFactorId($command->secondFactorId), |
||
| 198 | new YubikeyPublicId($command->yubikeyPublicId) |
||
| 199 | ); |
||
| 200 | |||
| 201 | $this->eventSourcedRepository->save($identity); |
||
| 202 | } |
||
| 203 | |||
| 204 | View Code Duplication | public function handleProveYubikeyPossessionCommand(ProveYubikeyPossessionCommand $command) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @param ProvePhonePossessionCommand $command |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function handleProvePhonePossessionCommand(ProvePhonePossessionCommand $command) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @param ProveGssfPossessionCommand $command |
||
| 256 | */ |
||
| 257 | public function handleProveGssfPossessionCommand(ProveGssfPossessionCommand $command) |
||
| 283 | |||
| 284 | View Code Duplication | public function handleProveU2fDevicePossessionCommand(ProveU2fDevicePossessionCommand $command) |
|
| 285 | { |
||
| 286 | /** @var IdentityApi $identity */ |
||
| 287 | $identity = $this->eventSourcedRepository->load(new IdentityId($command->identityId)); |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param VerifyEmailCommand $command |
||
| 310 | */ |
||
| 311 | public function handleVerifyEmailCommand(VerifyEmailCommand $command) |
||
| 320 | |||
| 321 | public function handleVetSecondFactorCommand(VetSecondFactorCommand $command) |
||
| 350 | |||
| 351 | public function handleSelfVetSecondFactorCommand(SelfVetSecondFactorCommand $command) |
||
| 367 | |||
| 368 | public function handleRevokeOwnSecondFactorCommand(RevokeOwnSecondFactorCommand $command) |
||
| 376 | |||
| 377 | public function handleRevokeRegistrantsSecondFactorCommand(RevokeRegistrantsSecondFactorCommand $command) |
||
| 388 | |||
| 389 | public function handleExpressLocalePreferenceCommand(ExpressLocalePreferenceCommand $command) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param Locale $locale |
||
| 403 | */ |
||
| 404 | private function assertIsValidLocale(Locale $locale) |
||
| 412 | |||
| 413 | private function assertSecondFactorIsAllowedFor(SecondFactorType $secondFactor, Institution $institution) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param IdentityApi $identity |
||
| 430 | * @return bool |
||
| 431 | */ |
||
| 432 | private function emailVerificationIsRequired(IdentityApi $identity) |
||
| 447 | } |
||
| 448 |
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.