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 |
||
10 | class TfaController extends AppController |
||
11 | { |
||
12 | /** |
||
13 | * Display the index action. |
||
14 | * |
||
15 | * @return void |
||
16 | */ |
||
17 | public function index() |
||
20 | |||
21 | /** |
||
22 | * Display an intro to the Two-factor Authentication. |
||
23 | * |
||
24 | * @return \Cake\Network\Response|void |
||
25 | */ |
||
26 | public function intro() |
||
27 | { |
||
28 | $this->loadModel('Users'); |
||
29 | |||
30 | $user = $this->Users |
||
|
|||
31 | ->find() |
||
32 | ->where([ |
||
33 | 'Users.id' => $this->Auth->user('id') |
||
34 | ]) |
||
35 | ->select([ |
||
36 | 'id', |
||
37 | 'two_factor_auth_enabled' |
||
38 | ]) |
||
39 | ->first(); |
||
40 | |||
41 | View Code Duplication | if ($user->two_factor_auth_enabled == true) { |
|
42 | $this->Flash->error(__('You have already set-up the Two-factor authentication.')); |
||
43 | |||
44 | return $this->redirect(['controller' => 'users', 'action' => 'security']); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Configure the Two-factor Authentication. |
||
50 | * |
||
51 | * @return \Cake\Network\Response|void |
||
52 | */ |
||
53 | public function configure() |
||
54 | { |
||
55 | $this->loadModel('Users'); |
||
56 | |||
57 | $user = $this->Users |
||
58 | ->find() |
||
59 | ->contain([ |
||
60 | 'UsersTwoFactorAuth' |
||
61 | ]) |
||
62 | ->where([ |
||
63 | 'Users.id' => $this->Auth->user('id') |
||
64 | ]) |
||
65 | ->select([ |
||
66 | 'Users.id', |
||
67 | 'Users.username', |
||
68 | 'Users.two_factor_auth_enabled', |
||
69 | 'UsersTwoFactorAuth.id', |
||
70 | 'UsersTwoFactorAuth.user_id', |
||
71 | 'UsersTwoFactorAuth.secret', |
||
72 | 'UsersTwoFactorAuth.username' |
||
73 | ]) |
||
74 | ->first(); |
||
75 | |||
76 | View Code Duplication | if ($user->two_factor_auth_enabled == true) { |
|
77 | $this->Flash->error(__('You have already set-up the Two-factor authentication.')); |
||
78 | |||
79 | return $this->redirect(['controller' => 'users', 'action' => 'security']); |
||
80 | } |
||
81 | |||
82 | $tfa = new TwoFactorAuth('Xeta'); |
||
83 | |||
84 | if (!is_null($user->users_two_factor_auth)) { |
||
85 | $secret = $user->users_two_factor_auth->secret; |
||
86 | } else { |
||
87 | $this->loadModel('UsersTwoFactorAuth'); |
||
88 | $secret = $tfa->createSecret(); |
||
89 | |||
90 | $data = [ |
||
91 | 'user_id' => $this->Auth->user('id'), |
||
92 | 'secret' => $secret, |
||
93 | 'username' => $user->username |
||
94 | ]; |
||
95 | |||
96 | $entity = $this->UsersTwoFactorAuth->newEntity($data); |
||
97 | $this->UsersTwoFactorAuth->save($entity); |
||
98 | } |
||
99 | |||
100 | $imgSrc = $tfa->getQRCodeImageAsDataUri($user->username, $secret); |
||
101 | $secretCode = chunk_split($secret, 4, ' '); |
||
102 | |||
103 | $this->set(compact('imgSrc', 'secretCode')); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Enable the Two-factor Authentication. |
||
108 | * |
||
109 | * @return \Cake\Network\Response|void |
||
110 | */ |
||
111 | public function enable() |
||
181 | |||
182 | /** |
||
183 | * Disable the Two-factor Authentication. |
||
184 | * |
||
185 | * @return \Cake\Network\Response |
||
186 | */ |
||
187 | public function disable() |
||
238 | |||
239 | /** |
||
240 | * Display the recovery code and the status of the code. |
||
241 | * |
||
242 | * @return \Cake\Network\Response|void |
||
243 | */ |
||
244 | public function recoveryCode() |
||
273 | |||
274 | /** |
||
275 | * Generate a new recovery code. |
||
276 | * |
||
277 | * @return \Cake\Network\Response |
||
278 | */ |
||
279 | public function generateRecoveryCode() |
||
344 | |||
345 | /** |
||
346 | * Function to generate a recovery code. |
||
347 | * |
||
348 | * Generate a code into this format : |
||
349 | * XXXX-XXXX-XXXX-XXXX |
||
350 | * |
||
351 | * @param string $user The username of the user. |
||
352 | * |
||
353 | * @return string |
||
354 | */ |
||
355 | protected function _generateNewRecoveryCode($user) |
||
367 | } |
||
368 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.