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 |
||
65 | class Behavior extends BaseBehavior |
||
66 | { |
||
67 | |||
68 | /** |
||
69 | * @var User |
||
70 | */ |
||
71 | public $owner; |
||
72 | |||
73 | /** |
||
74 | * @var callable|bool weather enabling this behavior. This property use in special case you need to disable it in runtime environment. |
||
75 | * When it is callable this object instance will be parse to first parameter. |
||
76 | * |
||
77 | * Example: |
||
78 | * ```php |
||
79 | * function(\vxm\mfa\Behavior $behavior) { |
||
80 | * |
||
81 | * |
||
82 | * } |
||
83 | * ``` |
||
84 | */ |
||
85 | public $enable = true; |
||
86 | |||
87 | /** |
||
88 | * @var string|array the URL for login when [[verifyRequired()]] is called. |
||
89 | * If an array is given, [[\yii\web\UrlManager::createUrl()]] will be called to create the corresponding URL. |
||
90 | * The first element of the array should be the route to the verify action, and the rest of |
||
91 | * the name-value pairs are GET parameters used to construct the verify URL. For example, |
||
92 | * |
||
93 | * ```php |
||
94 | * 'site/mfa-verify' |
||
95 | * ``` |
||
96 | * |
||
97 | * If this property is `null`, a 403 HTTP exception will be raised when [[verifyRequired()]] is called. |
||
98 | */ |
||
99 | public $verifyUrl; |
||
100 | |||
101 | /** |
||
102 | * @var string the session variable name used to store values of an identity logged in. |
||
103 | */ |
||
104 | public $mfaParam = '__mfa'; |
||
105 | |||
106 | /** |
||
107 | * @inheritDoc |
||
108 | */ |
||
109 | 13 | public function init() |
|
117 | |||
118 | /** |
||
119 | * @inheritDoc |
||
120 | */ |
||
121 | 13 | public function events() |
|
131 | |||
132 | /** |
||
133 | * Event trigger when before user log in to system. It will be require an user verify otp digits except when user logged in via cookie base. |
||
134 | * |
||
135 | * @param UserEvent $event an event triggered |
||
136 | * @throws ForbiddenHttpException |
||
137 | */ |
||
138 | 11 | public function beforeLogin(UserEvent $event) |
|
156 | |||
157 | /** |
||
158 | * Switches to a logged in identity for the current user. |
||
159 | * |
||
160 | * @see \yii\web\User::switchIdentity() |
||
161 | */ |
||
162 | 5 | public function switchIdentityLoggedIn() |
|
173 | |||
174 | /** |
||
175 | * Save the user identity logged in object when an identity need to verify. |
||
176 | * |
||
177 | * @param IdentityInterface|null $identity the identity object associated with the currently logged user. |
||
178 | * @param int $duration number of seconds that the user can remain in logged-in status. |
||
179 | */ |
||
180 | 11 | public function saveIdentityLoggedIn(IdentityInterface $identity, int $duration) |
|
184 | |||
185 | /** |
||
186 | * Get an identity logged in. |
||
187 | * |
||
188 | * @return array|null Returns an array of 'identity' and 'duration' if valid, otherwise null. |
||
189 | * @see saveIdentityLoggedIn() |
||
190 | */ |
||
191 | 12 | public function getIdentityLoggedIn() |
|
218 | |||
219 | /** |
||
220 | * Removes the identity logged in. |
||
221 | */ |
||
222 | 2 | public function removeIdentityLoggedIn() |
|
226 | |||
227 | /** |
||
228 | * @var Otp|null an otp instance use to generate and validate otp. |
||
229 | */ |
||
230 | private $_otp; |
||
231 | |||
232 | /** |
||
233 | * Get an otp instance. |
||
234 | * |
||
235 | * @return Otp|null an otp instance use to generate and validate otp. |
||
236 | * @throws \yii\base\InvalidConfigException |
||
237 | */ |
||
238 | 8 | public function getOtp() |
|
246 | |||
247 | /** |
||
248 | * Set an otp instance use to generate and validate otp. |
||
249 | * |
||
250 | * @param array|string|Otp $otp object instance |
||
251 | * @throws \yii\base\InvalidConfigException |
||
252 | */ |
||
253 | 8 | public function setOtp($otp) |
|
261 | |||
262 | /** |
||
263 | * Generate an otp by current user logged in |
||
264 | * |
||
265 | * @return string|null an otp of current user logged in. |
||
266 | * @throws \yii\base\InvalidConfigException |
||
267 | * @throws \yii\base\NotSupportedException |
||
268 | */ |
||
269 | 4 | View Code Duplication | public function generateOtpByIdentityLoggedIn() |
286 | |||
287 | /** |
||
288 | * Validate an otp by current user logged in |
||
289 | * |
||
290 | * @param string $otp need to be validate |
||
291 | * @return bool weather an otp given is valid with identity logged in |
||
292 | * @throws \yii\base\InvalidConfigException |
||
293 | * @throws \yii\base\NotSupportedException |
||
294 | */ |
||
295 | 3 | View Code Duplication | public function validateOtpByIdentityLoggedIn(string $otp) |
311 | |||
312 | /** |
||
313 | * Return a qr code uri of current user |
||
314 | * |
||
315 | * @param array $params list of information use to show on an authenticator app. |
||
316 | * |
||
317 | * Example: |
||
318 | * ```php |
||
319 | * ['issuer' => 'VXM', 'label' => '[email protected]', 'image' => 'https://google.com'] |
||
320 | * ``` |
||
321 | * |
||
322 | * @return string|null qr code uri. If `null`, it means the user is a guest or not enable mfa. |
||
323 | * @throws \Throwable |
||
324 | */ |
||
325 | 4 | public function getQrCodeUri(array $params) |
|
341 | |||
342 | /** |
||
343 | * Redirects the user browser to the mfa verify page.. |
||
344 | * |
||
345 | * Make sure you set [[verifyUrl]] so that the user browser can be redirected to the specified verify URL after |
||
346 | * calling this method. |
||
347 | * |
||
348 | * Note that when [[verifyUrl]] is set, calling this method will NOT terminate the application execution. |
||
349 | * |
||
350 | * @return \yii\web\Response the redirection response if [[verifyUrl]] is set |
||
351 | * @throws ForbiddenHttpException |
||
352 | */ |
||
353 | 11 | protected function verifyRequired() |
|
365 | |||
366 | } |
||
367 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: