| Conditions | 6 |
| Paths | 14 |
| Total Lines | 51 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 48 | public function create(array $data) |
||
| 49 | { |
||
| 50 | $validator = Validator::make($data, [ |
||
| 51 | 'email' => 'required|email|unique:users,email', |
||
| 52 | 'username' => 'required|string|between:1,255|unique:users,username|' . Models\User::USERNAME_RULES, |
||
| 53 | 'name_first' => 'required|string|between:1,255', |
||
| 54 | 'name_last' => 'required|string|between:1,255', |
||
| 55 | 'password' => 'sometimes|nullable|' . Models\User::PASSWORD_RULES, |
||
| 56 | 'root_admin' => 'required|boolean', |
||
| 57 | 'custom_id' => 'sometimes|nullable|unique:users,id', |
||
| 58 | ]); |
||
| 59 | |||
| 60 | // Run validator, throw catchable and displayable exception if it fails. |
||
| 61 | // Exception includes a JSON result of failed validation rules. |
||
| 62 | if ($validator->fails()) { |
||
| 63 | throw new DisplayValidationException(json_encode($validator->errors())); |
||
| 64 | } |
||
| 65 | |||
| 66 | DB::beginTransaction(); |
||
| 67 | |||
| 68 | try { |
||
| 69 | $user = new Models\User; |
||
| 70 | $uuid = new UuidService; |
||
| 71 | |||
| 72 | // Support for API Services |
||
| 73 | if (isset($data['custom_id']) && ! is_null($data['custom_id'])) { |
||
| 74 | $user->id = $token; |
||
|
|
|||
| 75 | } |
||
| 76 | |||
| 77 | // UUIDs are not mass-fillable. |
||
| 78 | $user->uuid = $uuid->generate('users', 'uuid'); |
||
| 79 | |||
| 80 | $user->fill([ |
||
| 81 | 'email' => $data['email'], |
||
| 82 | 'username' => $data['username'], |
||
| 83 | 'name_first' => $data['name_first'], |
||
| 84 | 'name_last' => $data['name_last'], |
||
| 85 | 'password' => (empty($data['password'])) ? 'unset' : Hash::make($data['password']), |
||
| 86 | 'root_admin' => $data['root_admin'], |
||
| 87 | 'language' => Settings::get('default_language', 'en'), |
||
| 88 | ]); |
||
| 89 | $user->save(); |
||
| 90 | |||
| 91 | DB::commit(); |
||
| 92 | |||
| 93 | return $user; |
||
| 94 | } catch (\Exception $ex) { |
||
| 95 | DB::rollBack(); |
||
| 96 | throw $ex; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 181 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.