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 |
||
| 37 | class User extends ActiveRecord implements IdentityInterface |
||
| 38 | { |
||
| 39 | const STATUS_DELETED = 0; |
||
| 40 | const STATUS_ACTIVE = 10; |
||
| 41 | const USER_GUEST = 0; |
||
| 42 | |||
| 43 | public $confirmPassword; |
||
| 44 | public $newPassword; |
||
| 45 | public $password; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @inheritdoc |
||
| 49 | */ |
||
| 50 | public function rules() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @inheritdoc |
||
| 80 | */ |
||
| 81 | public function scenarios() |
||
| 82 | { |
||
| 83 | return [ |
||
| 84 | 'signup' => ['username', 'email', 'password', 'first_name', 'last_name'], |
||
| 85 | 'resetPassword' => ['password_hash', 'password_reset_token'], |
||
| 86 | 'requestPasswordResetToken' => ['email'], |
||
| 87 | |||
| 88 | 'registerService' => ['email', 'first_name', 'last_name'], |
||
| 89 | 'updateProfile' => ['email', 'first_name', 'last_name'], |
||
| 90 | 'completeRegistration' => ['first_name', 'last_name', 'username', 'email'], |
||
| 91 | 'changePassword' => ['password', 'newPassword', 'confirmPassword'], |
||
| 92 | // admin |
||
| 93 | 'search' => ['id', 'username', 'email', 'status', 'create_time', 'first_name', 'last_name'], |
||
| 94 | 'admin' => ['username', 'status', 'email', 'password', 'first_name', 'last_name'], |
||
| 95 | 'adminSignup' => ['username', 'status', 'email', 'password', 'first_name', 'last_name', 'auth_key'], |
||
| 96 | 'passwordResetToken' => ['password_reset_token'], |
||
| 97 | ]; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @inheritdoc |
||
| 102 | */ |
||
| 103 | public static function tableName() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @inheritdoc |
||
| 110 | */ |
||
| 111 | public function attributeLabels() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return array List of all possible statuses for User instance |
||
| 132 | */ |
||
| 133 | public static function getStatuses() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @inheritdoc |
||
| 143 | */ |
||
| 144 | public function behaviors() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @inheritdoc |
||
| 163 | */ |
||
| 164 | public static function findIdentityByAccessToken($token, $type = null) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Finds an identity by the given ID. |
||
| 172 | * |
||
| 173 | * @param string|integer $id the ID to be looked for |
||
| 174 | * @return IdentityInterface|null the identity object that matches the given ID. |
||
| 175 | */ |
||
| 176 | public static function findIdentity($id) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Finds user by username |
||
| 209 | * |
||
| 210 | * @param string $username |
||
| 211 | * @return null|User |
||
| 212 | */ |
||
| 213 | public static function findByUsername($username) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Finds user by password reset token |
||
| 220 | * |
||
| 221 | * @param string $token password reset token |
||
| 222 | * @return User|null |
||
| 223 | */ |
||
| 224 | public static function findByPasswordResetToken($token) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Finds out if password reset token is valid |
||
| 238 | * |
||
| 239 | * @param string $token password reset token |
||
| 240 | * @return boolean |
||
| 241 | */ |
||
| 242 | public static function isPasswordResetTokenValid($token) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return int|string current user ID |
||
| 255 | */ |
||
| 256 | public function getId() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return string current user auth key |
||
| 263 | */ |
||
| 264 | public function getAuthKey() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $authKey |
||
| 271 | * @return boolean if auth key is valid for current user |
||
| 272 | */ |
||
| 273 | public function validateAuthKey($authKey) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param string $password password to validate |
||
| 280 | * @return bool if password provided is valid for current user |
||
| 281 | */ |
||
| 282 | public function validatePassword($password) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Generates password hash from password and sets it to the model |
||
| 289 | * |
||
| 290 | * @param string $password |
||
| 291 | */ |
||
| 292 | public function setPassword($password) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Generates "remember me" authentication key |
||
| 299 | */ |
||
| 300 | public function generateAuthKey() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Generates new password reset token |
||
| 307 | */ |
||
| 308 | public function generatePasswordResetToken() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Removes password reset token |
||
| 315 | */ |
||
| 316 | public function removePasswordResetToken() |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * @param bool $insert |
||
| 324 | * @param array $changedAttributes |
||
| 325 | */ |
||
| 326 | public function afterSave($insert, $changedAttributes) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Returns array of ids of propertyGroups for user model |
||
| 346 | * @return array |
||
| 347 | * @throws \Exception |
||
| 348 | */ |
||
| 349 | private function getPropertyIdsForUsers() |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns gravatar image link for user |
||
| 369 | * @param int $size Avatar size in pixels |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | public function gravatar($size = 40) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Relation to UserService model describing social services available exact user to login |
||
| 380 | * @return \yii\db\ActiveQuery |
||
| 381 | */ |
||
| 382 | public function getServices() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @return mixed|string Display name for the user visual identification |
||
| 389 | */ |
||
| 390 | public function getDisplayName() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @inheritdoc |
||
| 398 | */ |
||
| 399 | public function beforeSave($insert) |
||
| 409 | } |
||
| 410 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.