| 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 |
||
| 38 | protected function prepareForValidation(): void |
||
| 39 | { |
||
| 40 | $data = $this->all(); |
||
| 41 | |||
| 42 | $manager = $this->route('manager') ?? app('cortex.auth.manager'); |
||
| 43 | $country = $data['country_code'] ?? null; |
||
| 44 | $twoFactor = $manager->getTwoFactor(); |
||
| 45 | |||
| 46 | if ($manager->exists && empty($data['password'])) { |
||
| 47 | unset($data['password'], $data['password_confirmation']); |
||
| 48 | } |
||
| 49 | |||
| 50 | // Set abilities |
||
| 51 | if (! empty($data['abilities'])) { |
||
| 52 | if ($this->user($this->route('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) { |
||
| 53 | $abilities = array_map('intval', $this->get('abilities', [])); |
||
| 54 | $data['abilities'] = $this->user($this->route('guard'))->isA('superadmin') ? $abilities |
||
| 55 | : $this->user($this->route('guard'))->getAbilities()->pluck('id')->intersect($abilities)->toArray(); |
||
| 56 | } else { |
||
| 57 | unset($data['abilities']); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | // Set roles |
||
| 62 | if (! empty($data['roles'])) { |
||
| 63 | if ($data['roles'] && $this->user($this->route('guard'))->can('assign', \Cortex\Auth\Models\Role::class)) { |
||
| 64 | $roles = array_map('intval', $this->get('roles', [])); |
||
| 65 | $data['roles'] = $this->user($this->route('guard'))->isA('superadmin') ? $roles |
||
| 66 | : $this->user($this->route('guard'))->roles->pluck('id')->intersect($roles)->toArray(); |
||
| 67 | } else { |
||
| 68 | unset($data['roles']); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | if ($twoFactor && (isset($data['phone_verified_at']) || $country !== $manager->country_code)) { |
||
| 73 | array_set($twoFactor, 'phone.enabled', false); |
||
| 74 | $data['two_factor'] = $twoFactor; |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->replace($data); |
||
| 78 | } |
||
| 79 | |||
| 100 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: