Conditions | 9 |
Paths | 25 |
Total Lines | 66 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 2 | Features | 1 |
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 |
||
163 | public function createTestUser(array $fields = []) |
||
164 | { |
||
165 | $random = $this->getRandom(); |
||
166 | $username = $random->name(8); |
||
167 | $user = [ |
||
168 | 'uid' => 0, |
||
169 | 'name' => $username, |
||
170 | 'pass' => $random->name(16), |
||
171 | 'mail' => "[email protected]", |
||
172 | 'roles' => [ |
||
173 | DRUPAL_AUTHENTICATED_RID => 'authenticated user', |
||
174 | ], |
||
175 | ]; |
||
176 | |||
177 | $user = (object) $user; |
||
178 | |||
179 | if (!empty($fields)) { |
||
180 | $entity = new EntityDrupalWrapper('user'); |
||
181 | $required = $entity->getRequiredFields(); |
||
182 | |||
183 | // Fill fields. Field can be found by name or label. |
||
184 | foreach ($fields as $field_name => $value) { |
||
185 | $field_info = $entity->getFieldInfo($field_name); |
||
186 | |||
187 | if (!empty($field_info)) { |
||
188 | $field_name = $field_info['field_name']; |
||
189 | } |
||
190 | |||
191 | $user->$field_name = $value; |
||
192 | |||
193 | // Remove field from $required if it was there and filled. |
||
194 | unset($required[$field_name]); |
||
195 | } |
||
196 | |||
197 | // Throw an exception when one of required fields was not filled. |
||
198 | if (!empty($required)) { |
||
199 | throw new \Exception(sprintf( |
||
200 | 'The following fields "%s" are required and has not filled.', |
||
201 | implode('", "', $required) |
||
202 | )); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | if (isset($user->name)) { |
||
207 | $existing_user = user_load_by_name($user->name); |
||
208 | |||
209 | if (!empty($existing_user)) { |
||
210 | user_delete($existing_user->uid); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | // $this->user always exist but when no user created it has "false" as a value. |
||
215 | // Variable stored to another because RawDrupalContext::userCreate() will modify |
||
216 | // it and this will affect for future actions. |
||
217 | if (!empty($this->user)) { |
||
218 | $tmp = $this->user; |
||
219 | } |
||
220 | |||
221 | $this->userCreate($user); |
||
222 | |||
223 | if (isset($tmp)) { |
||
224 | $this->user = $tmp; |
||
225 | } |
||
226 | |||
227 | return $user; |
||
228 | } |
||
229 | } |
||
230 |
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: