Conditions | 17 |
Paths | 132 |
Total Lines | 52 |
Code Lines | 41 |
Lines | 32 |
Ratio | 61.54 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
113 | public function sendMessage($type, $params) |
||
|
|||
114 | { |
||
115 | if ($this->mailer === null) { |
||
116 | /** @var yii\swiftmailer\Mailer mailer */ |
||
117 | $this->mailer = Yii::$app->mailer; |
||
118 | $this->mailer->viewPath = $this->getViewPath() . '/mails'; |
||
119 | $this->mailer->getView()->theme = Yii::$app->view->theme; |
||
120 | } |
||
121 | switch ($type) { |
||
122 | View Code Duplication | case 'register': |
|
123 | if ($this->enableRegistrationEmail) { |
||
124 | $message = $this->mailer->compose($this->mailViews[$type], $params); |
||
125 | $message->setSubject(Yii::t('activeuser_general', 'Thank you for register on site')); |
||
126 | } |
||
127 | break; |
||
128 | View Code Duplication | case 'confirm': |
|
129 | if ($this->enableConfirmation) { |
||
130 | $message = $this->mailer->compose($this->mailViews[$type], $params); |
||
131 | $message->setSubject(Yii::t('activeuser_general', 'Email address confirmation needed')); |
||
132 | } |
||
133 | break; |
||
134 | View Code Duplication | case 'restore': |
|
135 | if ($this->enableConfirmation) { |
||
136 | $message = $this->mailer->compose($this->mailViews[$type], $params); |
||
137 | $message->setSubject(Yii::t('activeuser_general', 'Password restore request')); |
||
138 | } |
||
139 | break; |
||
140 | View Code Duplication | case 'passchanged': |
|
141 | if ($this->enableConfirmation) { |
||
142 | $message = $this->mailer->compose($this->mailViews[$type], $params); |
||
143 | $message->setSubject(Yii::t('activeuser_general', 'Password was changed')); |
||
144 | } |
||
145 | break; |
||
146 | View Code Duplication | case 'block': |
|
147 | $message = $this->mailer->compose($this->mailViews[$type], $params); |
||
148 | $message->setSubject(Yii::t('activeuser_general', 'You are blocked')); |
||
149 | break; |
||
150 | View Code Duplication | case 'unblock': |
|
151 | $message = $this->mailer->compose($this->mailViews[$type], $params); |
||
152 | $message->setSubject(Yii::t('activeuser_general', 'You are unblocked')); |
||
153 | break; |
||
154 | } |
||
155 | if (!empty($message)) { |
||
156 | $user = $params['user']; |
||
157 | if ($this->sender === null) { |
||
158 | $this->sender = isset(Yii::$app->params['adminEmail']) ? Yii::$app->params['adminEmail'] : 'no-reply@' . (empty($_SERVER['HTTP_HOST']) ? 'example.com' : $_SERVER['HTTP_HOST']); |
||
159 | } |
||
160 | $message->setTo(empty($user->name) ? $user->email : [$user->email => $user->name]); |
||
161 | $message->setFrom($this->sender); |
||
162 | $this->mailer->send($message); |
||
163 | } |
||
164 | } |
||
165 | |||
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: