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 UnverifiedSecondFactor extends AbstractSecondFactor |
||
42 | { |
||
43 | /** |
||
44 | * @var \Surfnet\Stepup\Identity\Api\Identity |
||
45 | */ |
||
46 | private $identity; |
||
47 | |||
48 | /** |
||
49 | * @var \Surfnet\Stepup\Identity\Value\SecondFactorId |
||
50 | */ |
||
51 | private $id; |
||
52 | |||
53 | /** |
||
54 | * @var \Surfnet\StepupBundle\Value\SecondFactorType |
||
55 | */ |
||
56 | private $type; |
||
57 | |||
58 | /** |
||
59 | * @var \Surfnet\Stepup\Identity\Value\SecondFactorIdentifier |
||
60 | */ |
||
61 | private $secondFactorIdentifier; |
||
62 | |||
63 | /** |
||
64 | * @var \Surfnet\Stepup\Identity\Value\EmailVerificationWindow |
||
65 | */ |
||
66 | private $verificationWindow; |
||
67 | |||
68 | /** |
||
69 | * @var string |
||
70 | */ |
||
71 | private $verificationNonce; |
||
72 | |||
73 | /** |
||
74 | * @param SecondFactorId $id |
||
75 | * @param Identity $identity |
||
76 | * @param SecondFactorType $type |
||
77 | * @param SecondFactorIdentifier $secondFactorIdentifier |
||
78 | * @param EmailVerificationWindow $emailVerificationWindow |
||
79 | * @param string $verificationNonce |
||
80 | * @return UnverifiedSecondFactor |
||
81 | */ |
||
82 | View Code Duplication | public static function create( |
|
|
|||
83 | SecondFactorId $id, |
||
84 | Identity $identity, |
||
85 | SecondFactorType $type, |
||
86 | $secondFactorIdentifier, |
||
87 | EmailVerificationWindow $emailVerificationWindow, |
||
88 | $verificationNonce |
||
89 | ) { |
||
90 | if (!is_string($verificationNonce)) { |
||
91 | throw InvalidArgumentException::invalidType('string', 'verificationNonce', $verificationNonce); |
||
92 | } |
||
93 | |||
94 | if (empty($verificationNonce)) { |
||
95 | throw new InvalidArgumentException("'verificationNonce' may not be empty"); |
||
96 | } |
||
97 | |||
98 | $secondFactor = new self(); |
||
99 | $secondFactor->id = $id; |
||
100 | $secondFactor->identity = $identity; |
||
101 | $secondFactor->type = $type; |
||
102 | $secondFactor->secondFactorIdentifier = $secondFactorIdentifier; |
||
103 | $secondFactor->verificationWindow = $emailVerificationWindow; |
||
104 | $secondFactor->verificationNonce = $verificationNonce; |
||
105 | |||
106 | return $secondFactor; |
||
107 | } |
||
108 | |||
109 | final public function __construct() |
||
110 | { |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return SecondFactorId |
||
115 | */ |
||
116 | public function getId() |
||
117 | { |
||
118 | return $this->id; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param string $verificationNonce |
||
123 | * @return bool |
||
124 | */ |
||
125 | public function hasNonce($verificationNonce) |
||
126 | { |
||
127 | return $this->verificationNonce === $verificationNonce; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function canBeVerifiedNow() |
||
134 | { |
||
135 | return $this->verificationWindow->isOpen(); |
||
136 | } |
||
137 | |||
138 | public function verifyEmail() |
||
139 | { |
||
140 | $this->apply( |
||
141 | new EmailVerifiedEvent( |
||
142 | $this->identity->getId(), |
||
143 | $this->identity->getInstitution(), |
||
144 | $this->id, |
||
145 | $this->type, |
||
146 | $this->secondFactorIdentifier, |
||
147 | DateTime::now(), |
||
148 | OtpGenerator::generate(8), |
||
149 | $this->identity->getCommonName(), |
||
150 | $this->identity->getEmail(), |
||
151 | $this->identity->getPreferredLocale() |
||
152 | ) |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | View Code Duplication | public function revoke() |
|
157 | { |
||
158 | $this->apply( |
||
159 | new UnverifiedSecondFactorRevokedEvent( |
||
160 | $this->identity->getId(), |
||
161 | $this->identity->getInstitution(), |
||
162 | $this->id, |
||
163 | $this->type, |
||
164 | $this->secondFactorIdentifier |
||
165 | ) |
||
166 | ); |
||
167 | } |
||
168 | |||
169 | View Code Duplication | public function complyWithRevocation(IdentityId $authorityId) |
|
170 | { |
||
171 | $this->apply( |
||
172 | new CompliedWithUnverifiedSecondFactorRevocationEvent( |
||
173 | $this->identity->getId(), |
||
174 | $this->identity->getInstitution(), |
||
175 | $this->id, |
||
176 | $this->type, |
||
177 | $this->secondFactorIdentifier, |
||
178 | $authorityId |
||
179 | ) |
||
180 | ); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param DateTime $registrationRequestedAt |
||
185 | * @param string $registrationCode |
||
186 | * @return VerifiedSecondFactor |
||
187 | */ |
||
188 | public function asVerified($registrationRequestedAt, $registrationCode) |
||
189 | { |
||
190 | return VerifiedSecondFactor::create( |
||
191 | $this->id, |
||
192 | $this->identity, |
||
193 | $this->type, |
||
194 | $this->secondFactorIdentifier, |
||
195 | $registrationRequestedAt, |
||
196 | $registrationCode |
||
197 | ); |
||
198 | } |
||
199 | |||
200 | protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event) |
||
206 | |||
207 | protected function getType() |
||
208 | { |
||
209 | return $this->type; |
||
210 | } |
||
211 | } |
||
212 |
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.