| Conditions | 8 |
| Paths | 65 |
| Total Lines | 52 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 43 | public function handle() |
||
| 44 | { |
||
| 45 | $user = new User(); |
||
| 46 | |||
| 47 | // set username |
||
| 48 | $user->username = $this->argument('username'); |
||
| 49 | if (User::where('username', $user->username)->exists()) { |
||
| 50 | $this->error('User '.$user->username.' exists.'); |
||
| 51 | return; |
||
| 52 | } |
||
| 53 | |||
| 54 | // set realname |
||
| 55 | if ($this->option('realname')) { |
||
| 56 | $user->realname = $this->option('realname'); |
||
| 57 | } |
||
| 58 | else { |
||
| 59 | $user->realname = $this->ask('Real Name'); |
||
| 60 | } |
||
| 61 | |||
| 62 | // set email |
||
| 63 | if ($this->option('email')) { |
||
| 64 | $user->email = $this->option('email'); |
||
| 65 | } |
||
| 66 | else { |
||
| 67 | $user->email = $this->ask('Email'); |
||
| 68 | } |
||
| 69 | |||
| 70 | // set user level, start with 1 and upgrade as specified |
||
| 71 | $user->level = 1; |
||
| 72 | if ($this->option('read')) { |
||
| 73 | $user->level = 5; |
||
| 74 | } |
||
| 75 | if ($this->option('admin')) { |
||
| 76 | $user->level = 10; |
||
| 77 | } |
||
| 78 | |||
| 79 | // set password |
||
| 80 | if ($this->argument('password')) { |
||
| 81 | $user->password = $this->argument('password'); |
||
| 82 | } |
||
| 83 | else { |
||
| 84 | $user->password = $this->secret('Password'); |
||
| 85 | } |
||
| 86 | |||
| 87 | // save user |
||
| 88 | if ($user->save()) { |
||
| 89 | $this->info('User '.$user->username.' created.'); |
||
| 90 | } |
||
| 91 | else { |
||
| 92 | $this->error('Failed to create user '.$user->username); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 |