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 RegistrationVerificationResult |
||
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_UNMATCHED_REGISTRATION_CHALLENGE = 1; |
||
40 | |||
41 | /** |
||
42 | * The response was signed by another party than the device, indicating it was tampered with. |
||
43 | */ |
||
44 | const STATUS_RESPONSE_NOT_SIGNED_BY_DEVICE = 2; |
||
45 | |||
46 | /** |
||
47 | * The device has not been manufactured by a trusted party. |
||
48 | */ |
||
49 | const STATUS_UNTRUSTED_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\RegisterResponse::$errorCode |
||
60 | * @see \Surfnet\StepupU2fBundle\Dto\RegisterResponse::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 | * @var int |
||
71 | */ |
||
72 | private $status; |
||
73 | |||
74 | /** |
||
75 | * @var Registration|null |
||
76 | */ |
||
77 | private $registration; |
||
78 | |||
79 | /** |
||
80 | * @var int|null |
||
81 | */ |
||
82 | private $deviceErrorCode; |
||
83 | |||
84 | /** |
||
85 | * @param Registration $registration |
||
86 | * @return RegistrationVerificationResult |
||
87 | */ |
||
88 | public static function success(Registration $registration) |
||
95 | |||
96 | /** |
||
97 | * @param int $errorCode |
||
98 | * @return RegistrationVerificationResult |
||
99 | */ |
||
100 | View Code Duplication | public static function deviceReportedError($errorCode) |
|
122 | |||
123 | /** |
||
124 | * @return RegistrationVerificationResult |
||
125 | */ |
||
126 | public static function responseChallengeDidNotMatchRequestChallenge() |
||
130 | |||
131 | /** |
||
132 | * @return RegistrationVerificationResult |
||
133 | */ |
||
134 | public static function responseWasNotSignedByDevice() |
||
138 | |||
139 | /** |
||
140 | * @return RegistrationVerificationResult |
||
141 | */ |
||
142 | public static function deviceCannotBeTrusted() |
||
146 | |||
147 | /** |
||
148 | * @return RegistrationVerificationResult |
||
149 | */ |
||
150 | public static function publicKeyDecodingFailed() |
||
154 | |||
155 | /** |
||
156 | * @return RegistrationVerificationResult |
||
157 | */ |
||
158 | public static function appIdMismatch() |
||
162 | |||
163 | /** |
||
164 | * @param int $status |
||
165 | */ |
||
166 | private function __construct($status) |
||
170 | |||
171 | /** |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function wasSuccessful() |
||
178 | |||
179 | /** |
||
180 | * @return Registration|null |
||
181 | */ |
||
182 | public function getRegistration() |
||
190 | |||
191 | /** |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function didDeviceReportABadRequest() |
||
198 | |||
199 | /** |
||
200 | * @return bool |
||
201 | */ |
||
202 | public function wasClientConfigurationUnsupported() |
||
206 | |||
207 | /** |
||
208 | * @return bool |
||
209 | */ |
||
210 | public function wasDeviceAlreadyRegistered() |
||
214 | |||
215 | /** |
||
216 | * @return bool |
||
217 | */ |
||
218 | public function didDeviceTimeOut() |
||
222 | |||
223 | /** |
||
224 | * @return bool |
||
225 | */ |
||
226 | public function didDeviceReportAnUnknownError() |
||
230 | |||
231 | /** |
||
232 | * @return bool |
||
233 | */ |
||
234 | public function didDeviceReportAnyError() |
||
238 | |||
239 | /** |
||
240 | * @return bool |
||
241 | */ |
||
242 | public function didResponseChallengeNotMatchRequestChallenge() |
||
246 | |||
247 | /** |
||
248 | * @return bool |
||
249 | */ |
||
250 | public function wasResponseNotSignedByDevice() |
||
254 | |||
255 | /** |
||
256 | * @return bool |
||
257 | */ |
||
258 | public function canDeviceNotBeTrusted() |
||
262 | |||
263 | /** |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function didPublicKeyDecodingFail() |
||
270 | |||
271 | /** |
||
272 | * @return bool |
||
273 | */ |
||
274 | public function didntAppIdsMatch() |
||
278 | |||
279 | /** |
||
280 | * @param int $errorCode |
||
281 | * @return bool |
||
282 | */ |
||
283 | private function didDeviceReportError($errorCode) |
||
287 | } |
||
288 |
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.