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 |
||
| 14 | class LoginForm extends Model |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var string email |
||
| 18 | */ |
||
| 19 | public $email; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string password |
||
| 23 | */ |
||
| 24 | public $password; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var bool remember me |
||
| 28 | */ |
||
| 29 | public $rememberMe = true; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var bool|UserModel |
||
| 33 | */ |
||
| 34 | protected $user = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @inheritdoc |
||
| 38 | */ |
||
| 39 | public function rules() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @inheritdoc |
||
| 51 | */ |
||
| 52 | View Code Duplication | public function attributeLabels() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Validates the password. |
||
| 63 | * This method serves as the inline validation for password. |
||
| 64 | * |
||
| 65 | * @param $attribute |
||
| 66 | * @param $params |
||
| 67 | */ |
||
| 68 | public function validatePassword($attribute, $params) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Logs in a user using the provided username and password. |
||
| 82 | * |
||
| 83 | * @return bool whether the user is logged in successfully |
||
| 84 | */ |
||
| 85 | public function login() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Finds user by [[email]] |
||
| 96 | * |
||
| 97 | * @return UserModel|null |
||
| 98 | */ |
||
| 99 | public function getUser() |
||
| 107 | } |
||
| 108 |
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.