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 |
||
| 9 | class Auth extends Provider |
||
| 10 | { |
||
| 11 | use SendsRegisterActions; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected $loginRequiredFor = [ |
||
| 17 | 'logout' |
||
| 18 | ]; |
||
| 19 | |||
| 20 | const REGISTRATION_COMPLETE_EXPERIENCE_ID = '11:10105'; |
||
| 21 | const ACCOUNT_TYPE_OTHER = 'other'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Login as pinner. |
||
| 25 | * |
||
| 26 | * @param string $username |
||
| 27 | * @param string $password |
||
| 28 | * @param bool $autoLogin |
||
| 29 | * @return bool |
||
| 30 | */ |
||
| 31 | public function login($username, $password, $autoLogin = true) |
||
| 32 | { |
||
| 33 | if ($this->request->isLoggedIn()) return true; |
||
| 34 | |||
| 35 | $this->checkCredentials($username, $password); |
||
| 36 | |||
| 37 | // Trying to load previously saved cookies from last login session for this username. |
||
| 38 | // Then grab user profile info to check, if cookies are ok. If an empty response |
||
| 39 | // was returned, then send login request. |
||
| 40 | if($autoLogin && $this->processAutoLogin($username)) { |
||
| 41 | return true; |
||
| 42 | } |
||
| 43 | |||
| 44 | return $this->processLogin($username, $password); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function logout() |
||
| 48 | { |
||
| 49 | $this->request->logout(); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Register a new user. |
||
| 54 | * |
||
| 55 | * @param string $email |
||
| 56 | * @param string $password |
||
| 57 | * @param string $name |
||
| 58 | * @param string $country |
||
| 59 | * @param int $age |
||
| 60 | * |
||
| 61 | * @return bool |
||
| 62 | */ |
||
| 63 | public function register($email, $password, $name, $country = 'GB', $age = 18) |
||
| 64 | { |
||
| 65 | $data = [ |
||
| 66 | "age" => $age, |
||
| 67 | "email" => $email, |
||
| 68 | "password" => $password, |
||
| 69 | "country" => $country, |
||
| 70 | "first_name" => $name, |
||
| 71 | "gender" => "male", |
||
| 72 | "container" => 'home_page', |
||
| 73 | ]; |
||
| 74 | |||
| 75 | return $this->makeRegisterCall($data); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Register a new business account. |
||
| 80 | * |
||
| 81 | * @param string $email |
||
| 82 | * @param string $password |
||
| 83 | * @param string $businessName |
||
| 84 | * @param string $website |
||
| 85 | * @return bool|mixed |
||
| 86 | */ |
||
| 87 | public function registerBusiness($email, $password, $businessName, $website = '') |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | public function isLoggedIn() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $link |
||
| 110 | * @return array|bool |
||
| 111 | */ |
||
| 112 | public function confirmEmail($link) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Validates password and login. |
||
| 119 | * |
||
| 120 | * @param string $username |
||
| 121 | * @param string $password |
||
| 122 | */ |
||
| 123 | protected function checkCredentials($username, $password) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return bool|Response |
||
| 132 | */ |
||
| 133 | protected function sendEmailVerificationAction() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | protected function completeRegistration() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param array $data |
||
| 155 | * @return bool|mixed |
||
| 156 | */ |
||
| 157 | View Code Duplication | protected function makeRegisterCall($data) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @param array $data |
||
| 172 | * @return bool|mixed |
||
| 173 | */ |
||
| 174 | View Code Duplication | protected function makeBusinessRegisterCall($data) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $username |
||
| 189 | * @param string $password |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | protected function processLogin($username, $password) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $username |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | protected function processAutoLogin($username) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | protected function getProfile() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | protected function sendRegisterActions() |
||
| 248 | } |
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.