Conditions | 15 |
Paths | 144 |
Total Lines | 48 |
Code Lines | 27 |
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 |
||
26 | protected function prepareForValidation(): void |
||
27 | { |
||
28 | $data = $this->all(); |
||
29 | |||
30 | $guardian = $this->route('guardian') ?? app('cortex.auth.guardian'); |
||
31 | $country = $data['country_code'] ?? null; |
||
32 | $twoFactor = $guardian->getTwoFactor(); |
||
33 | |||
34 | $data['email_verified'] = $this->get('email_verified', false); |
||
35 | $data['phone_verified'] = $this->get('phone_verified', false); |
||
36 | |||
37 | if ($guardian->exists && empty($data['password'])) { |
||
38 | unset($data['password'], $data['password_confirmation']); |
||
39 | } |
||
40 | |||
41 | // Update email verification date |
||
42 | if ($data['email_verified'] && $guardian->email_verified !== $data['email_verified']) { |
||
43 | $data['email_verified_at'] = now(); |
||
44 | } |
||
45 | |||
46 | // Update phone verification date |
||
47 | if ($data['phone_verified'] && $guardian->phone_verified !== $data['phone_verified']) { |
||
48 | $data['phone_verified_at'] = now(); |
||
49 | } |
||
50 | |||
51 | // Set abilities |
||
52 | if ($this->user($this->get('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) { |
||
53 | $data['abilities'] = $this->user($this->get('guard'))->can('superadmin') ? $this->get('abilities', []) |
||
54 | : $this->user($this->get('guard'))->abilities->pluck('id')->intersect($this->get('abilities', []))->toArray(); |
||
|
|||
55 | } else { |
||
56 | unset($data['abilities']); |
||
57 | } |
||
58 | |||
59 | // Set roles |
||
60 | if ($this->user($this->get('guard'))->can('assign', \Cortex\Auth\Models\Role::class) && $data['roles']) { |
||
61 | $data['roles'] = $this->user($this->get('guard'))->can('superadmin') ? $this->get('roles', []) |
||
62 | : $this->user($this->get('guard'))->roles->pluck('id')->intersect($this->get('roles', []))->toArray(); |
||
63 | } else { |
||
64 | unset($data['roles']); |
||
65 | } |
||
66 | |||
67 | if ($twoFactor && (isset($data['phone_verified_at']) || $country !== $guardian->country_code)) { |
||
68 | array_set($twoFactor, 'phone.enabled', false); |
||
69 | $data['two_factor'] = $twoFactor; |
||
70 | } |
||
71 | |||
72 | $this->replace($data); |
||
73 | } |
||
74 | |||
95 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.