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 |
||
24 | class TotpController extends \yii\web\Controller |
||
25 | { |
||
26 | public function behaviors() |
||
27 | { |
||
28 | return array_merge( |
||
29 | parent::behaviors(), |
||
30 | [ |
||
31 | 'access' => [ |
||
32 | 'class' => AccessControl::class, |
||
33 | 'denyCallback' => [$this, 'denyCallback'], |
||
34 | 'rules' => [ |
||
35 | // ? - guest |
||
36 | [ |
||
37 | 'actions' => ['check'], |
||
38 | 'roles' => ['?'], |
||
39 | 'allow' => true, |
||
40 | ], |
||
41 | // @ - authenticated |
||
42 | [ |
||
43 | 'actions' => ['enable', 'disable', 'toggle'], |
||
44 | 'roles' => ['@'], |
||
45 | 'allow' => true, |
||
46 | ], |
||
47 | ], |
||
48 | ], |
||
49 | ] |
||
50 | ); |
||
51 | } |
||
52 | |||
53 | public function denyCallback() |
||
57 | |||
58 | public function actionEnable($back = null) |
||
59 | { |
||
60 | /** @var MfaIdentityInterface $user */ |
||
61 | $user = Yii::$app->user->identity; |
||
62 | View Code Duplication | if ($user->getTotpSecret()) { |
|
|
|||
63 | Yii::$app->session->setFlash( |
||
64 | 'error', |
||
65 | Yii::t('mfa', 'Two-factor authentication is already enabled. Disable first.') |
||
66 | ); |
||
67 | |||
68 | return empty($back) ? $this->goHome() : $this->deferredRedirect($back); |
||
69 | } |
||
70 | |||
71 | $model = new InputForm(); |
||
72 | $secret = $this->module->getTotp()->getSecret(); |
||
73 | |||
74 | if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
||
75 | if ($this->module->getTotp()->verifyCode($secret, $model->code)) { |
||
76 | $user->setTotpSecret($secret); |
||
77 | $this->module->getTotp()->setIsVerified(true); |
||
78 | if ($user->save() && Yii::$app->user->login($user)) { |
||
79 | $recovery = new RecoveryCodeCollection(); |
||
80 | $codes = $recovery->generate(); |
||
81 | if (!$codes->save()) { |
||
82 | Yii::$app->session->setFlash( |
||
83 | 'error', |
||
84 | Yii::t( |
||
85 | 'mfa', |
||
86 | 'Sorry, we have failed to generate your recovery codes. Please try again later.' |
||
87 | ) |
||
88 | ); |
||
89 | } else { |
||
90 | return $this->actionCodes($codes->getCodes(), $back); |
||
91 | } |
||
92 | Yii::$app->session->setFlash( |
||
93 | 'success', |
||
94 | Yii::t('mfa', 'Two-factor authentication successfully enabled.') |
||
95 | ); |
||
96 | |||
97 | return empty($back) ? $this->goBack() : $this->deferredRedirect($back); |
||
98 | View Code Duplication | } else { |
|
99 | Yii::$app->session->setFlash( |
||
100 | 'error', |
||
101 | Yii::t( |
||
102 | 'mfa', |
||
103 | 'Sorry, we have failed to enable two-factor authentication.' |
||
104 | ) |
||
105 | ); |
||
106 | |||
107 | return empty($back) ? $this->goHome() : $this->deferredRedirect($back); |
||
108 | } |
||
109 | } else { |
||
110 | $model->addError( |
||
111 | 'code', |
||
112 | Yii::t('mfa', 'Wrong verification code. Please verify your secret and try again.') |
||
113 | ); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | $qrcode = $this->module->getTotp()->getQRCodeImageAsDataUri($user->getUsername(), $secret); |
||
118 | |||
119 | return $this->render('enable', compact('model', 'secret', 'qrcode')); |
||
120 | } |
||
121 | |||
122 | public function actionCodes(array $codes, $back = null) |
||
135 | |||
136 | public function actionDisable($back = null) |
||
137 | { |
||
165 | |||
166 | public function deferredRedirect($url = null) |
||
170 | |||
171 | public function actionToggle($back = null) |
||
178 | |||
179 | public function actionCheck(bool $useRecoveryCode = false) |
||
212 | |||
213 | |||
214 | public function actionRecover($back = null) |
||
242 | |||
243 | /** |
||
244 | * @inheritDoc |
||
245 | */ |
||
246 | public function goBack($defaultUrl = null) |
||
255 | } |
||
256 |
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.