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 |
||
| 21 | class Module extends BaseModule |
||
| 22 | { |
||
| 23 | /** Module version */ |
||
| 24 | const VERSION = '0.1.0'; |
||
| 25 | |||
| 26 | /** @var array view templates for emails composing */ |
||
| 27 | public $mailViews = [ |
||
| 28 | 'confirm' => 'confirm', |
||
| 29 | 'register' => 'register', |
||
| 30 | 'restore' => 'restore', |
||
| 31 | 'passchanged' => 'passchanged', |
||
| 32 | 'block' => 'block', |
||
| 33 | 'unblock' => 'unblock', |
||
| 34 | ]; |
||
| 35 | /** @var bool whether to enable user registration */ |
||
| 36 | public $enableRegistration = true; |
||
| 37 | /** |
||
| 38 | * List of fields used for registration. |
||
| 39 | * Email is always used and can be omitted. |
||
| 40 | * If you not specify password, they will be generated automatically |
||
| 41 | * You can specify: password, name, gender, birth |
||
| 42 | * if you specify `password` or `name` they required for fill. |
||
| 43 | * `gender` and `birth` is always optional. |
||
| 44 | * |
||
| 45 | * @var string[] |
||
| 46 | */ |
||
| 47 | public $registrationFields = []; |
||
| 48 | /** @var bool whether to enable send the email to the user for confirm the email address */ |
||
| 49 | public $enableConfirmation = true; |
||
| 50 | /** @var bool whether to enable send notification email about register to the user */ |
||
| 51 | public $enableRegistrationEmail = true; |
||
| 52 | /** @var bool whether to enable send notification email about user blocking */ |
||
| 53 | public $enableBlockingEmail = true; |
||
| 54 | /** @var bool whether to enable send notification email about user unblocking */ |
||
| 55 | public $enableUnblockingEmail = true; |
||
| 56 | /** @var bool whether to enable password restore by email */ |
||
| 57 | public $enablePasswordRestore = true; |
||
| 58 | /** @var bool whether to enable send new password email */ |
||
| 59 | public $enableNewPasswordEmail = true; |
||
| 60 | /** |
||
| 61 | * @var bool whether to automatically generate password on restore |
||
| 62 | * Password will be generated only if user password is empty |
||
| 63 | */ |
||
| 64 | public $generatePassOnRestore = true; |
||
| 65 | /** |
||
| 66 | * Email sender address |
||
| 67 | * If not set use Yii::$app->params['adminEmail'], and if they empty use 'no-reply@'.$_SERVER['HTTP_HOST'] |
||
| 68 | * Can be set as array ['email'=>'name'] |
||
| 69 | * @var string|array |
||
| 70 | */ |
||
| 71 | public $sender; |
||
| 72 | /** |
||
| 73 | * Use only email for login (medium.com style) |
||
| 74 | * If true, user's email used for send unique URL link to enter on site |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | public $loginByEmail = false; |
||
| 78 | /** @var int the time you want the user will be remembered without asking for credentials */ |
||
| 79 | public $rememberTime = 2592000; |
||
| 80 | /** @var int the time before a confirmation token becomes invalid */ |
||
| 81 | public $confirmationTime = 86400; // one month |
||
| 82 | /** @var int the time before a recovery token becomes invalid */ |
||
| 83 | public $restoreTime = 10800; // one day |
||
| 84 | /** @var array Model map */ |
||
| 85 | public $modelMap = []; |
||
| 86 | /** |
||
| 87 | * @var string The prefix for user module URL. |
||
| 88 | * @See [[GroupUrlRule::prefix]] |
||
| 89 | */ |
||
| 90 | public $urlPrefix = 'activeuser'; |
||
| 91 | /** @var array The rules for frontend to be used in URL management. */ |
||
| 92 | public $urlRulesFrontend = [ |
||
| 93 | ]; |
||
| 94 | public $frontendUrlManager; |
||
| 95 | /** @var array The rules for backend to be used in URL management. */ |
||
| 96 | public $urlRulesBackend = [ |
||
| 97 | ]; |
||
| 98 | /** |
||
| 99 | * The URL for login in email only mode |
||
| 100 | * @var string|array |
||
| 101 | */ |
||
| 102 | public $loginUrl = ['/activeuser/account/login']; |
||
| 103 | /** |
||
| 104 | * @var yii\mail\BaseMailer |
||
| 105 | */ |
||
| 106 | protected $mailer; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Send email |
||
| 110 | * @param int $type email type |
||
| 111 | * @param array $params email views params |
||
| 112 | */ |
||
| 113 | public function sendMessage($type, $params) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Check that registration enabled |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | public function isRegistrationEnabled() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @inheritdoc |
||
| 177 | */ |
||
| 178 | public function getViewPath() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Check that field need for register |
||
| 185 | * @param string $name field name |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | public function isFieldForRegister($name) |
||
| 192 | } |
||
| 193 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: