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 |
||
38 | final class BootstrapIdentityWithSmsSecondFactorCommand extends AbstractBootstrapCommand |
||
39 | { |
||
40 | protected function configure() |
||
41 | { |
||
42 | $this |
||
43 | ->setDescription('Creates an identity with a SMS second factor') |
||
44 | ->addArgument('name-id', InputArgument::REQUIRED, 'The NameID of the identity to create') |
||
45 | ->addArgument('institution', InputArgument::REQUIRED, 'The institution of the identity to create') |
||
46 | ->addArgument('common-name', InputArgument::REQUIRED, 'The Common Name of the identity to create') |
||
47 | ->addArgument('email', InputArgument::REQUIRED, 'The e-mail address of the identity to create') |
||
48 | ->addArgument('preferred-locale', InputArgument::REQUIRED, 'The preferred locale of the identity to create') |
||
49 | ->addArgument( |
||
50 | 'phone-number', |
||
51 | InputArgument::REQUIRED, |
||
52 | 'The phone number of the user should be formatted like "+31 (0) 612345678"' |
||
53 | ) |
||
54 | ->addArgument( |
||
55 | 'registration-status', |
||
56 | InputArgument::REQUIRED, |
||
57 | 'Valid arguments: unverified, verified, vetted' |
||
58 | ); |
||
59 | } |
||
60 | |||
61 | protected function execute(InputInterface $input, OutputInterface $output) |
||
62 | { |
||
63 | $this->tokenStorage->setToken( |
||
64 | new AnonymousToken('cli.bootstrap-identity-with-sms-token', 'cli', ['ROLE_SS', 'ROLE_RA']) |
||
|
|||
65 | ); |
||
66 | $nameId = new NameId($input->getArgument('name-id')); |
||
67 | $institutionText = $input->getArgument('institution'); |
||
68 | $institution = new Institution($institutionText); |
||
69 | $mailVerificationRequired = $this->requiresMailVerification($institutionText); |
||
70 | $commonName = $input->getArgument('common-name'); |
||
71 | $email = $input->getArgument('email'); |
||
72 | $preferredLocale = $input->getArgument('preferred-locale'); |
||
73 | $registrationStatus = $input->getArgument('registration-status'); |
||
74 | $phoneNumber = $input->getArgument('phone-number'); |
||
75 | $identity = false; |
||
76 | $output->writeln(sprintf('<notice>Adding a %s SMS token for %s</notice>', $registrationStatus, $commonName)); |
||
77 | |||
78 | if ($this->identityRepository->hasIdentityWithNameIdAndInstitution($nameId, $institution)) { |
||
79 | $output->writeln( |
||
80 | sprintf( |
||
81 | '<notice>An identity with name ID "%s" from institution "%s" already exists, using that identity</notice>', |
||
82 | $nameId->getNameId(), |
||
83 | $institution->getInstitution() |
||
84 | ) |
||
85 | ); |
||
86 | $identity = $this->identityRepository->findOneByNameIdAndInstitution($nameId, $institution); |
||
87 | } |
||
88 | |||
89 | $this->beginTransaction(); |
||
90 | $secondFactorId = Uuid::uuid4()->toString(); |
||
91 | |||
92 | if (!$identity) { |
||
93 | $output->writeln('<notice>Creating a new identity</notice>'); |
||
94 | $identity = $this->createIdentity($institution, $nameId, $commonName, $email, $preferredLocale); |
||
95 | } |
||
96 | |||
97 | try { |
||
98 | switch ($registrationStatus) { |
||
99 | case "unverified": |
||
100 | $output->writeln('<notice>Creating an unverified SMS token</notice>'); |
||
101 | $this->provePossession($secondFactorId, $identity, $phoneNumber); |
||
102 | break; |
||
103 | case "verified": |
||
104 | $output->writeln('<notice>Creating an unverified SMS token</notice>'); |
||
105 | $this->provePossession($secondFactorId, $identity, $phoneNumber); |
||
106 | /** @var UnverifiedSecondFactor $unverifiedSecondFactor */ |
||
107 | $unverifiedSecondFactor = $this->unverifiedSecondFactorRepository->findOneBy( |
||
108 | ['identityId' => $identity->id, 'type' => 'sms'] |
||
109 | ); |
||
110 | if ($mailVerificationRequired) { |
||
111 | $output->writeln('<notice>Creating a verified SMS token</notice>'); |
||
112 | $this->verifyEmail($identity, $unverifiedSecondFactor); |
||
113 | } |
||
114 | break; |
||
115 | case "vetted": |
||
116 | $output->writeln('<notice>Creating an unverified SMS token</notice>'); |
||
117 | $this->provePossession($secondFactorId, $identity, $phoneNumber); |
||
118 | /** @var UnverifiedSecondFactor $unverifiedSecondFactor */ |
||
119 | $unverifiedSecondFactor = $this->unverifiedSecondFactorRepository->findOneBy( |
||
120 | ['identityId' => $identity->id, 'type' => 'sms'] |
||
121 | ); |
||
122 | if ($mailVerificationRequired) { |
||
123 | $output->writeln('<notice>Creating a verified SMS token</notice>'); |
||
124 | $this->verifyEmail($identity, $unverifiedSecondFactor); |
||
125 | } |
||
126 | /** @var VerifiedSecondFactor $verifiedSecondFactor */ |
||
127 | $verifiedSecondFactor = $this->verifiedSecondFactorRepository->findOneBy( |
||
128 | ['identityId' => $identity->id, 'type' => 'sms'] |
||
129 | ); |
||
130 | $output->writeln('<notice>Vetting the verified SMS token</notice>'); |
||
131 | $this->vetSecondFactor( |
||
132 | 'sms', |
||
133 | 'db9b8bdf-720c-44ba-a4c4-154953e45f14', |
||
134 | $identity, |
||
135 | $secondFactorId, |
||
136 | $verifiedSecondFactor, |
||
137 | $phoneNumber |
||
138 | ); |
||
139 | break; |
||
140 | } |
||
141 | $this->finishTransaction(); |
||
142 | } catch (Exception $e) { |
||
143 | $output->writeln( |
||
144 | sprintf( |
||
145 | '<error>An Error occurred when trying to bootstrap the identity: "%s"</error>', |
||
146 | $e->getMessage() |
||
147 | ) |
||
148 | ); |
||
149 | $this->rollback(); |
||
150 | throw $e; |
||
151 | } |
||
152 | $output->writeln( |
||
153 | sprintf( |
||
154 | '<info>Successfully created identity with UUID %s and %s second factor with UUID %s</info>', |
||
155 | $identity->id, |
||
156 | $registrationStatus, |
||
157 | $secondFactorId |
||
158 | ) |
||
159 | ); |
||
160 | } |
||
161 | |||
162 | View Code Duplication | private function provePossession($secondFactorId, $identity, $phoneNumber) |
|
171 | |||
172 | View Code Duplication | private function verifyEmail($identity, $unverifiedSecondFactor) |
|
180 | |||
181 | protected function createIdentity( |
||
200 | } |
||
201 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: