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 |
||
13 | class TwoFactorAuthenticationController extends Controller implements TwoFactorAuthenticationInterface |
||
14 | { |
||
15 | use AuthenticatesUsersWith2FA; |
||
16 | |||
17 | /** |
||
18 | * Setup two factor authentication. |
||
19 | * |
||
20 | * @param \Illuminate\Http\Request |
||
21 | * @param \Illuminate\Http\Response |
||
22 | */ |
||
23 | public function setupTwoFactorAuthentication(Request $request) |
||
41 | |||
42 | /** |
||
43 | * Disable 2FA. |
||
44 | * |
||
45 | * @param \Illuminate\Http\Request |
||
46 | * |
||
47 | * @return \Illuminate\Http\RedirectResponse |
||
48 | */ |
||
49 | View Code Duplication | public function enableTwoFactorAuthentication(Request $request) |
|
57 | |||
58 | /** |
||
59 | * Enable 2FA. |
||
60 | * |
||
61 | * @param \Illuminate\Http\Request |
||
62 | * |
||
63 | * @return \Illuminate\Http\RedirectResponse |
||
64 | */ |
||
65 | View Code Duplication | public function disableTwoFactorAuthentication(Request $request) |
|
74 | |||
75 | /** |
||
76 | * Verify Two Factor Authentication. |
||
77 | * |
||
78 | * @param \Illuminate\Http\Request $request |
||
79 | */ |
||
80 | public function verifyTwoFactorAuthentication(Request $request) |
||
95 | |||
96 | /** |
||
97 | * Encode Random String to 32 Base Transfer Encoding. |
||
98 | * |
||
99 | * @param int $length Length of the encoded string. |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | private function base32EncodedString($length = 30): |
||
108 | |||
109 | /** |
||
110 | * Generate a more truly "random" alpha-numeric string. |
||
111 | * |
||
112 | * @param int $length |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | private function strRandom($length = 30): |
||
131 | } |
||
132 |
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.