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 |
||
27 | final class AuthenticationVerificationResult |
||
28 | { |
||
29 | /** |
||
30 | * Registration was a success. |
||
31 | */ |
||
32 | const STATUS_SUCCESS = 'SUCCESS'; |
||
33 | |||
34 | /** |
||
35 | * The response challenge did not match the request challenge. |
||
36 | */ |
||
37 | const STATUS_REQUEST_RESPONSE_MISMATCH = 'REQUEST_RESPONSE_MISMATCH'; |
||
38 | |||
39 | /** |
||
40 | * The response challenge was not for the given registration. |
||
41 | */ |
||
42 | const STATUS_RESPONSE_REGISTRATION_MISMATCH = 'RESPONSE_REGISTRATION_MISMATCH'; |
||
43 | |||
44 | /** |
||
45 | * The response was signed by another party than the device, indicating it was tampered with. |
||
46 | */ |
||
47 | const STATUS_RESPONSE_NOT_SIGNED_BY_DEVICE = 'RESPONSE_NOT_SIGNED_BY_DEVICE'; |
||
48 | |||
49 | /** |
||
50 | * The decoding of the device's public key failed. |
||
51 | */ |
||
52 | const STATUS_PUBLIC_KEY_DECODING_FAILED = 'PUBLIC_KEY_DECODING_FAILED'; |
||
53 | |||
54 | /** |
||
55 | * The device sent a sign counter that was equal to or lower than the previously recorded counter. |
||
56 | */ |
||
57 | const STATUS_SIGN_COUNTER_TOO_LOW = 'SIGN_COUNTER_TOO_LOW'; |
||
58 | |||
59 | /** |
||
60 | * The U2F device reported an error. |
||
61 | * |
||
62 | * @see \Surfnet\StepupU2fBundle\Dto\SignResponse::$errorCode |
||
63 | * @see \Surfnet\StepupU2fBundle\Dto\SignResponse::ERROR_CODE_* constants |
||
64 | */ |
||
65 | const STATUS_DEVICE_ERROR = 'DEVICE_ERROR'; |
||
66 | |||
67 | /** |
||
68 | * The AppIDs of the server and a message did not match. |
||
69 | */ |
||
70 | const STATUS_APP_ID_MISMATCH = 'APP_ID_MISMATCH'; |
||
71 | |||
72 | /** |
||
73 | * No registration matching the authentication's key handle could be found. |
||
74 | */ |
||
75 | const STATUS_REGISTRATION_UNKNOWN = 'REGISTRATION_UNKNOWN'; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | private $status; |
||
81 | |||
82 | /** |
||
83 | * @param U2fAuthenticationVerificationResult $u2fResult |
||
84 | * @return self |
||
85 | */ |
||
86 | public static function from(U2fAuthenticationVerificationResult $u2fResult) |
||
87 | { |
||
88 | $result = new self; |
||
89 | |||
90 | View Code Duplication | if ($u2fResult->wasSuccessful()) { |
|
|
|||
91 | $result->status = self::STATUS_SUCCESS; |
||
92 | } elseif ($u2fResult->didDeviceReportAnyError()) { |
||
93 | $result->status = self::STATUS_DEVICE_ERROR; |
||
94 | } elseif ($u2fResult->didResponseChallengeNotMatchRequestChallenge()) { |
||
95 | $result->status = self::STATUS_REQUEST_RESPONSE_MISMATCH; |
||
96 | } elseif ($u2fResult->wasResponseNotSignedByDevice()) { |
||
97 | $result->status = self::STATUS_RESPONSE_NOT_SIGNED_BY_DEVICE; |
||
98 | } elseif ($u2fResult->didPublicKeyDecodingFail()) { |
||
99 | $result->status = self::STATUS_PUBLIC_KEY_DECODING_FAILED; |
||
100 | } elseif ($u2fResult->wasSignCounterTooLow()) { |
||
101 | $result->status = self::STATUS_SIGN_COUNTER_TOO_LOW; |
||
102 | } elseif ($u2fResult->didntAppIdsMatch()) { |
||
103 | $result->status = self::STATUS_APP_ID_MISMATCH; |
||
104 | } else { |
||
105 | throw new LogicException('Unknown authentication verification result status'); |
||
106 | } |
||
107 | |||
108 | return $result; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @return self |
||
113 | */ |
||
114 | public static function registrationUnknown() |
||
115 | { |
||
116 | $result = new self; |
||
117 | $result->status = self::STATUS_REGISTRATION_UNKNOWN; |
||
118 | |||
119 | return $result; |
||
120 | } |
||
121 | |||
122 | private function __construct() |
||
123 | { |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function wasSuccessful() |
||
130 | { |
||
131 | return $this->status === self::STATUS_SUCCESS; |
||
132 | } |
||
133 | |||
134 | public function didDeviceReportError() |
||
135 | { |
||
136 | return $this->status === self::STATUS_DEVICE_ERROR; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | public function getStatus() |
||
146 | } |
||
147 |
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.