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 |
||
| 15 | class TwoFactorAuthenticationController extends Controller implements TwoFactorAuthenticationInterface |
||
| 16 | { |
||
| 17 | use AuthenticatesUsersWith2FA; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * User Model. |
||
| 21 | */ |
||
| 22 | protected $TwoFAModel; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Assigns $usersModel Property a Model instance. |
||
| 26 | */ |
||
| 27 | public function __construct() |
||
| 28 | { |
||
| 29 | $this->TwoFAModel = TwoFactorAuthenticationServiceProvider::getTwoFAModelInstance(); |
||
| 30 | |||
| 31 | $this->middleware(function ($request, $next) { |
||
| 32 | $this->setUser(auth()->user()); |
||
|
|
|||
| 33 | return $next($request); |
||
| 34 | }); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Setup two factor authentication. |
||
| 39 | * |
||
| 40 | * @param \Illuminate\Http\Request |
||
| 41 | * @param \Illuminate\Http\Response |
||
| 42 | * |
||
| 43 | * @throws \Thecodework\TwoFactorAuthentications\Exceptions\TwoFactorAuthenticationExceptions |
||
| 44 | */ |
||
| 45 | public function setupTwoFactorAuthentication(Request $request) |
||
| 46 | { |
||
| 47 | $this->updateUserWith2FAGeneratedKey(); |
||
| 48 | $user = $this->getUser(); |
||
| 49 | $totp = new TOTP( |
||
| 50 | config('2fa-config.account_name'), |
||
| 51 | $user->two_factor_secret_key, |
||
| 52 | config('2fa-config.period'), |
||
| 53 | config('2fa-config.digest_algorithm'), |
||
| 54 | config('2fa-config.number_of_digits') |
||
| 55 | ); |
||
| 56 | |||
| 57 | $barcode = $totp->getQrCodeUri(); |
||
| 58 | |||
| 59 | // Return Barcode image if ajax Request |
||
| 60 | if ($request->ajax()) { |
||
| 61 | return $barcode; |
||
| 62 | } |
||
| 63 | |||
| 64 | return view('2fa::setup', compact('barcode', 'user')); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Disable 2FA. |
||
| 69 | * |
||
| 70 | * @param \Illuminate\Http\Request |
||
| 71 | * |
||
| 72 | * @return mixed |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function enableTwoFactorAuthentication(Request $request) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Enable 2FA. |
||
| 94 | * |
||
| 95 | * @param \Illuminate\Http\Request |
||
| 96 | * |
||
| 97 | * @return mixed |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function disableTwoFactorAuthentication(Request $request) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Verify Two Factor Authentication. |
||
| 120 | * |
||
| 121 | * @param \Illuminate\Http\Request $request |
||
| 122 | */ |
||
| 123 | public function verifyTwoFactorAuthentication(Request $request) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Encode Random String to 32 Base Transfer Encoding. |
||
| 141 | * |
||
| 142 | * @param int $length Length of the encoded string. |
||
| 143 | * |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | private function base32EncodedString($length = 30): |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Generate a more truly "random" alpha-numeric string. |
||
| 154 | * |
||
| 155 | * @param int $length |
||
| 156 | * |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | private function strRandom($length = 30): |
||
| 174 | |||
| 175 | private function updateUserWith2FAGeneratedKey() |
||
| 185 | } |
||
| 186 |
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: