Conditions | 12 |
Paths | 21 |
Total Lines | 70 |
Code Lines | 43 |
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 |
||
37 | public function handle() |
||
38 | { |
||
39 | $email = $this->argument('email'); |
||
40 | $password = $this->argument('password'); |
||
41 | |||
42 | $delete = $this->option('delete'); |
||
43 | $admin = $this->option('admin'); |
||
44 | $noadmin = $this->option('noadmin'); |
||
45 | |||
46 | $user = User::whereEmail($email)->first(); |
||
47 | |||
48 | if ($delete) { |
||
49 | if (!$user) { |
||
50 | $this->error(sprintf('User %s does not exist!', $email)); |
||
51 | } else { |
||
52 | $user->delete(); |
||
53 | |||
54 | $this->warn(sprintf('%s was deleted', $user->username)); |
||
55 | } |
||
56 | |||
57 | return; |
||
58 | } |
||
59 | |||
60 | if (empty($email) && empty($password)) { |
||
61 | $headers = ['Name', 'Email', 'Admin', 'MMEX Guid', 'Locale', 'API Key', 'Created at']; |
||
62 | |||
63 | $users = User::all(['name', 'email', 'is_admin', 'mmex_guid', 'locale', 'api_token', 'created_at'])->toArray(); |
||
64 | |||
65 | $this->info(sprintf('%d users found', count($users))); |
||
66 | |||
67 | $this->table($headers, $users); |
||
68 | |||
69 | return; |
||
70 | } |
||
71 | |||
72 | if (!$user) { |
||
73 | $name = $this->ask('Name of user?'); |
||
74 | $user = User::create([ |
||
75 | 'name' => $name, |
||
76 | 'email' => $email, |
||
77 | 'password' => bcrypt($password), |
||
|
|||
78 | ]); |
||
79 | |||
80 | $this->info(sprintf('User %s was created!', $user->username)); |
||
81 | } else { |
||
82 | if ($password) { |
||
83 | $user->password = bcrypt($password); |
||
84 | $user->save(); |
||
85 | |||
86 | $this->info(sprintf('%s\'s password was updated!', $user->name)); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | if ($admin || $noadmin) { |
||
91 | if (!$user) { |
||
92 | $this->error(sprintf('User %s does not exist!', $email)); |
||
93 | } else { |
||
94 | $user->is_admin = $admin || $noadmin; |
||
95 | $user->save(); |
||
96 | |||
97 | if ($admin == true) { |
||
98 | $this->info('Admin privileges set'); |
||
99 | } else { |
||
100 | $this->info('Admin privileges removed'); |
||
101 | } |
||
102 | |||
103 | return; |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.