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 |
||
33 | View Code Duplication | final class BootstrapSmsSecondFactorCommand extends AbstractBootstrapCommand |
|
|
|||
34 | { |
||
35 | protected function configure() |
||
36 | { |
||
37 | $this |
||
38 | ->setDescription('Creates a SMS second factor for a specified user') |
||
39 | ->addArgument('name-id', InputArgument::REQUIRED, 'The NameID of the identity to create') |
||
40 | ->addArgument('institution', InputArgument::REQUIRED, 'The institution of the identity to create') |
||
41 | ->addArgument( |
||
42 | 'phone-number', |
||
43 | InputArgument::REQUIRED, |
||
44 | 'The phone number of the user should be formatted like "+31 (0) 612345678"' |
||
45 | ) |
||
46 | ->addArgument( |
||
47 | 'registration-status', |
||
48 | InputArgument::REQUIRED, |
||
49 | 'Valid arguments: unverified, verified, vetted' |
||
50 | ) |
||
51 | ->addArgument('actor-id', InputArgument::REQUIRED, 'The id of the vetting actor'); |
||
52 | } |
||
53 | |||
54 | protected function execute(InputInterface $input, OutputInterface $output) |
||
55 | { |
||
56 | $this->tokenStorage->setToken( |
||
57 | new AnonymousToken('cli.bootstrap-sms-token', 'cli', ['ROLE_SS', 'ROLE_RA']) |
||
58 | ); |
||
59 | $nameId = new NameId($input->getArgument('name-id')); |
||
60 | $institutionText = $input->getArgument('institution'); |
||
61 | $institution = new Institution($institutionText); |
||
62 | $mailVerificationRequired = $this->requiresMailVerification($institutionText); |
||
63 | $registrationStatus = $input->getArgument('registration-status'); |
||
64 | $phoneNumber = $input->getArgument('phone-number'); |
||
65 | $actorId = $input->getArgument('actor-id'); |
||
66 | $this->enrichEventMetadata($actorId); |
||
67 | if (!$this->tokenBootstrapService->hasIdentityWithNameIdAndInstitution($nameId, $institution)) { |
||
68 | $output->writeln( |
||
69 | sprintf( |
||
70 | '<error>An identity with name ID "%s" from institution "%s" does not exist, create it first.</error>', |
||
71 | $nameId->getNameId(), |
||
72 | $institution->getInstitution() |
||
73 | ) |
||
74 | ); |
||
75 | |||
76 | return; |
||
77 | } |
||
78 | $identity = $this->tokenBootstrapService->findOneByNameIdAndInstitution($nameId, $institution); |
||
79 | $output->writeln(sprintf('<comment>Adding a %s SMS token for %s</comment>', $registrationStatus, $identity->commonName)); |
||
80 | $this->beginTransaction(); |
||
81 | $secondFactorId = Uuid::uuid4()->toString(); |
||
82 | |||
83 | try { |
||
84 | switch ($registrationStatus) { |
||
85 | case "unverified": |
||
86 | $output->writeln('<comment>Creating an unverified SMS token</comment>'); |
||
87 | $this->provePossession($secondFactorId, $identity, $phoneNumber); |
||
88 | break; |
||
89 | case "verified": |
||
90 | $output->writeln('<comment>Creating an unverified SMS token</comment>'); |
||
91 | $this->provePossession($secondFactorId, $identity, $phoneNumber); |
||
92 | $unverifiedSecondFactor = $this->tokenBootstrapService->findUnverifiedToken($identity->id, 'sms'); |
||
93 | if ($mailVerificationRequired) { |
||
94 | $output->writeln('<comment>Creating a verified SMS token</comment>'); |
||
95 | $this->verifyEmail($identity, $unverifiedSecondFactor); |
||
96 | } |
||
97 | break; |
||
98 | case "vetted": |
||
99 | $output->writeln('<comment>Creating an unverified SMS token</comment>'); |
||
100 | $this->provePossession($secondFactorId, $identity, $phoneNumber); |
||
101 | /** @var UnverifiedSecondFactor $unverifiedSecondFactor */ |
||
102 | $unverifiedSecondFactor = $this->tokenBootstrapService->findUnverifiedToken($identity->id, 'sms'); |
||
103 | if ($mailVerificationRequired) { |
||
104 | $output->writeln('<comment>Creating a verified SMS token</comment>'); |
||
105 | $this->verifyEmail($identity, $unverifiedSecondFactor); |
||
106 | } |
||
107 | $verifiedSecondFactor = $this->tokenBootstrapService->findVerifiedToken($identity->id, 'sms'); |
||
108 | $output->writeln('<comment>Vetting the verified SMS token</comment>'); |
||
109 | $this->vetSecondFactor( |
||
110 | 'sms', |
||
111 | $actorId, |
||
112 | $identity, |
||
113 | $secondFactorId, |
||
114 | $verifiedSecondFactor, |
||
115 | $phoneNumber |
||
116 | ); |
||
117 | break; |
||
118 | } |
||
119 | $this->finishTransaction(); |
||
120 | } catch (Exception $e) { |
||
121 | $output->writeln( |
||
122 | sprintf( |
||
123 | '<error>An Error occurred when trying to bootstrap the SMS token: "%s"</error>', |
||
124 | $e->getMessage() |
||
125 | ) |
||
126 | ); |
||
127 | $this->rollback(); |
||
128 | throw $e; |
||
129 | } |
||
130 | $output->writeln( |
||
131 | sprintf( |
||
132 | '<info>Successfully registered a SMS token with UUID %s</info>', |
||
133 | $identity->id, |
||
134 | $registrationStatus, |
||
135 | $secondFactorId |
||
136 | ) |
||
137 | ); |
||
138 | } |
||
139 | |||
140 | private function provePossession($secondFactorId, $identity, $phoneNumber) |
||
149 | } |
||
150 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.