| Conditions | 7 |
| Paths | 24 |
| Total Lines | 62 |
| Code Lines | 38 |
| 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 |
||
| 56 | public function create(array $data) |
||
| 57 | { |
||
| 58 | $validator = Validator::make($data, [ |
||
| 59 | 'email' => 'required|email|unique:users,email', |
||
| 60 | 'username' => 'required|string|between:1,255|unique:users,username|' . Models\User::USERNAME_RULES, |
||
| 61 | 'name_first' => 'required|string|between:1,255', |
||
| 62 | 'name_last' => 'required|string|between:1,255', |
||
| 63 | 'password' => 'sometimes|nullable|' . Models\User::PASSWORD_RULES, |
||
| 64 | 'root_admin' => 'required|boolean', |
||
| 65 | 'custom_id' => 'sometimes|nullable|unique:users,id', |
||
| 66 | ]); |
||
| 67 | |||
| 68 | // Run validator, throw catchable and displayable exception if it fails. |
||
| 69 | // Exception includes a JSON result of failed validation rules. |
||
| 70 | if ($validator->fails()) { |
||
| 71 | throw new DisplayValidationException($validator->errors()); |
||
| 72 | } |
||
| 73 | |||
| 74 | DB::beginTransaction(); |
||
| 75 | |||
| 76 | try { |
||
| 77 | $user = new Models\User; |
||
| 78 | $uuid = new UuidService; |
||
| 79 | |||
| 80 | // Support for API Services |
||
| 81 | if (isset($data['custom_id']) && ! is_null($data['custom_id'])) { |
||
| 82 | $user->id = $token; |
||
| 83 | } |
||
| 84 | |||
| 85 | // UUIDs are not mass-fillable. |
||
| 86 | $user->uuid = $uuid->generate('users', 'uuid'); |
||
| 87 | |||
| 88 | $user->fill([ |
||
| 89 | 'email' => $data['email'], |
||
| 90 | 'username' => $data['username'], |
||
| 91 | 'name_first' => $data['name_first'], |
||
| 92 | 'name_last' => $data['name_last'], |
||
| 93 | 'password' => Hash::make((empty($data['password'])) ? str_random(30) : $data['password']), |
||
| 94 | 'root_admin' => $data['root_admin'], |
||
| 95 | 'language' => Settings::get('default_language', 'en'), |
||
| 96 | ]); |
||
| 97 | $user->save(); |
||
| 98 | |||
| 99 | // Setup a Password Reset to use when they set a password. |
||
| 100 | // Only used if no password is provided. |
||
| 101 | if (empty($data['password'])) { |
||
| 102 | $token = str_random(32); |
||
| 103 | DB::table('password_resets')->insert([ |
||
| 104 | 'email' => $user->email, |
||
| 105 | 'token' => $token, |
||
| 106 | 'created_at' => Carbon::now()->toDateTimeString(), |
||
| 107 | ]); |
||
| 108 | } |
||
| 109 | |||
| 110 | DB::commit(); |
||
| 111 | |||
| 112 | return $user; |
||
| 113 | } catch (\Exception $ex) { |
||
| 114 | DB::rollBack(); |
||
| 115 | throw $ex; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 198 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.