| Conditions | 13 |
| Paths | 64 |
| Total Lines | 41 |
| 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 |
||
| 29 | protected function prepareForValidation(): void |
||
| 30 | { |
||
| 31 | $data = $this->all(); |
||
| 32 | |||
| 33 | $admin = $this->route('admin') ?? app('cortex.auth.admin'); |
||
| 34 | $country = $data['country_code'] ?? null; |
||
| 35 | $twoFactor = $admin->getTwoFactor(); |
||
| 36 | |||
| 37 | if ($admin->exists && empty($data['password'])) { |
||
| 38 | unset($data['password'], $data['password_confirmation']); |
||
| 39 | } |
||
| 40 | |||
| 41 | // Set abilities |
||
| 42 | if (! empty($data['abilities'])) { |
||
| 43 | if ($this->user($this->route('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) { |
||
| 44 | $abilities = array_map('intval', $this->get('abilities', [])); |
||
| 45 | $data['abilities'] = $this->user($this->route('guard'))->can('superadmin') ? $abilities |
||
| 46 | : $this->user($this->route('guard'))->getAbilities()->pluck('id')->intersect($abilities)->toArray(); |
||
| 47 | } else { |
||
| 48 | unset($data['abilities']); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | // Set roles |
||
| 53 | if (! empty($data['roles'])) { |
||
| 54 | if ($data['roles'] && $this->user($this->route('guard'))->can('assign', \Cortex\Auth\Models\Role::class)) { |
||
| 55 | $roles = array_map('intval', $this->get('roles', [])); |
||
| 56 | $data['roles'] = $this->user($this->route('guard'))->can('superadmin') ? $roles |
||
| 57 | : $this->user($this->route('guard'))->roles->pluck('id')->intersect($roles)->toArray(); |
||
| 58 | } else { |
||
| 59 | unset($data['roles']); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | if ($twoFactor && (isset($data['phone_verified_at']) || $country !== $admin->country_code)) { |
||
| 64 | array_set($twoFactor, 'phone.enabled', false); |
||
| 65 | $data['two_factor'] = $twoFactor; |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->replace($data); |
||
| 69 | } |
||
| 70 | |||
| 104 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.