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 |
||
12 | class Authy implements BaseProvider, SendSMSTokenContract, SendPhoneTokenContract |
||
13 | { |
||
14 | use CanSendToken; |
||
15 | |||
16 | /** |
||
17 | * Array containing configuration data. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | private $config; |
||
22 | |||
23 | /** |
||
24 | * Authy constructor. |
||
25 | */ |
||
26 | public function __construct() |
||
36 | |||
37 | /** |
||
38 | * Determine if the given user has two-factor authentication enabled. |
||
39 | * |
||
40 | * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user |
||
41 | * |
||
42 | * @return bool |
||
43 | */ |
||
44 | public function isEnabled(TwoFactorAuthenticatable $user) |
||
48 | |||
49 | /** |
||
50 | * Register the given user with the provider. |
||
51 | * |
||
52 | * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user |
||
53 | * @param bool $sms |
||
54 | * |
||
55 | * @return void |
||
56 | */ |
||
57 | public function register(TwoFactorAuthenticatable $user, $sms = false) |
||
74 | |||
75 | /** |
||
76 | * Send the user two-factor authentication token via SMS. |
||
77 | * |
||
78 | * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | View Code Duplication | public function sendSmsToken(TwoFactorAuthenticatable $user) |
|
97 | |||
98 | /** |
||
99 | * Start the user two-factor authentication via phone call. |
||
100 | * |
||
101 | * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user |
||
102 | * |
||
103 | * @return void |
||
104 | */ |
||
105 | View Code Duplication | public function sendPhoneCallToken(TwoFactorAuthenticatable $user) |
|
120 | |||
121 | /** |
||
122 | * Determine if the given token is valid for the given user. |
||
123 | * |
||
124 | * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user |
||
125 | * @param string $token |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function tokenIsValid(TwoFactorAuthenticatable $user, $token) |
||
145 | |||
146 | /** |
||
147 | * Delete the given user from the provider. |
||
148 | * |
||
149 | * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function delete(TwoFactorAuthenticatable $user) |
||
164 | |||
165 | /** |
||
166 | * Determine if the given user should be sent two-factor authentication token via SMS/phone call. |
||
167 | * |
||
168 | * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function canSendToken(TwoFactorAuthenticatable $user) |
||
184 | } |
||
185 |
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.