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 |
||
29 | final class AuthenticationVerificationResult |
||
30 | { |
||
31 | /** |
||
32 | * Registration was a success. |
||
33 | */ |
||
34 | const STATUS_SUCCESS = 0; |
||
35 | |||
36 | /** |
||
37 | * The response challenge did not match the request challenge. |
||
38 | */ |
||
39 | const STATUS_REQUEST_RESPONSE_MISMATCH = 1; |
||
40 | |||
41 | /** |
||
42 | * The response challenge was not for the given registration. |
||
43 | */ |
||
44 | const STATUS_RESPONSE_REGISTRATION_MISMATCH = 2; |
||
45 | |||
46 | /** |
||
47 | * The response was signed by another party than the device, indicating it was tampered with. |
||
48 | */ |
||
49 | const STATUS_RESPONSE_NOT_SIGNED_BY_DEVICE = 3; |
||
50 | |||
51 | /** |
||
52 | * The decoding of the device's public key failed. |
||
53 | */ |
||
54 | const STATUS_PUBLIC_KEY_DECODING_FAILED = 4; |
||
55 | |||
56 | /** |
||
57 | * The U2F device reported an error. |
||
58 | * |
||
59 | * @see \Surfnet\StepupU2fBundle\Dto\SignResponse::$errorCode |
||
60 | * @see \Surfnet\StepupU2fBundle\Dto\SignResponse::ERROR_CODE_* constants |
||
61 | */ |
||
62 | const STATUS_DEVICE_ERROR = 5; |
||
63 | |||
64 | /** |
||
65 | * The AppIDs of the server and a message did not match. |
||
66 | */ |
||
67 | const STATUS_APP_ID_MISMATCH = 6; |
||
68 | |||
69 | /** |
||
70 | * The sign response's counter was lower than the given registration's sign counter. |
||
71 | */ |
||
72 | const STATUS_SIGN_COUNTER_TOO_LOW = 7; |
||
73 | |||
74 | /** |
||
75 | * @var int |
||
76 | */ |
||
77 | private $status; |
||
78 | |||
79 | /** |
||
80 | * @var Registration|null |
||
81 | */ |
||
82 | private $registration; |
||
83 | |||
84 | /** |
||
85 | * @var int|null |
||
86 | */ |
||
87 | private $deviceErrorCode; |
||
88 | |||
89 | /** |
||
90 | * @param Registration $registration |
||
91 | * @return self |
||
92 | */ |
||
93 | public static function success(Registration $registration) |
||
100 | |||
101 | /** |
||
102 | * @param int $errorCode |
||
103 | * @return self |
||
104 | */ |
||
105 | View Code Duplication | public static function deviceReportedError($errorCode) |
|
125 | |||
126 | /** |
||
127 | * @return self |
||
128 | */ |
||
129 | public static function requestResponseMismatch() |
||
133 | |||
134 | /** |
||
135 | * @return self |
||
136 | */ |
||
137 | public static function responseRegistrationMismatch() |
||
141 | |||
142 | /** |
||
143 | * @return self |
||
144 | */ |
||
145 | public static function responseWasNotSignedByDevice() |
||
149 | |||
150 | /** |
||
151 | * @return self |
||
152 | */ |
||
153 | public static function publicKeyDecodingFailed() |
||
157 | |||
158 | /** |
||
159 | * @return self |
||
160 | */ |
||
161 | public static function appIdMismatch() |
||
165 | |||
166 | /** |
||
167 | * @return self |
||
168 | */ |
||
169 | public static function signCounterTooLow() |
||
173 | |||
174 | /** |
||
175 | * @param int $status |
||
176 | */ |
||
177 | private function __construct($status) |
||
181 | |||
182 | /** |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function wasSuccessful() |
||
189 | |||
190 | /** |
||
191 | * @return Registration|null |
||
192 | */ |
||
193 | public function getRegistration() |
||
201 | |||
202 | /** |
||
203 | * @return bool |
||
204 | */ |
||
205 | public function didDeviceReportABadRequest() |
||
209 | |||
210 | /** |
||
211 | * @return bool |
||
212 | */ |
||
213 | public function wasClientConfigurationUnsupported() |
||
217 | |||
218 | /** |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function wasKeyHandleUnknownToDevice() |
||
225 | |||
226 | /** |
||
227 | * @return bool |
||
228 | */ |
||
229 | public function didDeviceTimeOut() |
||
233 | |||
234 | /** |
||
235 | * @return bool |
||
236 | */ |
||
237 | public function didDeviceReportAnUnknownError() |
||
241 | |||
242 | /** |
||
243 | * @return bool |
||
244 | */ |
||
245 | public function didDeviceReportAnyError() |
||
249 | |||
250 | /** |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function didResponseChallengeNotMatchRequestChallenge() |
||
257 | |||
258 | /** |
||
259 | * @return bool |
||
260 | */ |
||
261 | public function didResponseChallengeNotMatchRegistration() |
||
265 | |||
266 | /** |
||
267 | * @return bool |
||
268 | */ |
||
269 | public function wasResponseNotSignedByDevice() |
||
273 | |||
274 | /** |
||
275 | * @return bool |
||
276 | */ |
||
277 | public function didPublicKeyDecodingFail() |
||
281 | |||
282 | /** |
||
283 | * @return bool |
||
284 | */ |
||
285 | public function didntAppIdsMatch() |
||
289 | |||
290 | /** |
||
291 | * @return bool |
||
292 | */ |
||
293 | public function wasSignCounterTooLow() |
||
297 | |||
298 | /** |
||
299 | * @param int $errorCode |
||
300 | * @return bool |
||
301 | */ |
||
302 | private function didDeviceReportError($errorCode) |
||
306 | } |
||
307 |
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.