We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 13 |
Paths | 26 |
Total Lines | 39 |
Code Lines | 18 |
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 |
||
17 | public function validateRules(string $attribute, mixed $value): array |
||
18 | { |
||
19 | $entry = CrudPanelFacade::getCurrentEntry(); |
||
|
|||
20 | |||
21 | // if the attribute is not set in the request, and an entry exists, |
||
22 | // we will check if there is a previous value, as this field might not have changed. |
||
23 | if (! Arr::has($this->data, $attribute) && $entry) { |
||
24 | if (str_contains($attribute, '.') && get_class($entry) === get_class(CrudPanelFacade::getModel())) { |
||
25 | $previousValue = Arr::get($this->data, '_order_'.Str::before($attribute, '.')); |
||
26 | $previousValue = Arr::get($previousValue, Str::after($attribute, '.')); |
||
27 | } else { |
||
28 | $previousValue = Arr::get($entry, $attribute); |
||
29 | } |
||
30 | |||
31 | if ($previousValue && empty($value)) { |
||
32 | return []; |
||
33 | } |
||
34 | |||
35 | Arr::set($this->data, $attribute, $previousValue ?? $value); |
||
36 | } |
||
37 | |||
38 | // if the value is an uploaded file, or the attribute is not |
||
39 | // set in the request, we force fill the data with the value |
||
40 | if ($value instanceof UploadedFile || ! Arr::has($this->data, $attribute)) { |
||
41 | Arr::set($this->data, $attribute, $value); |
||
42 | } |
||
43 | |||
44 | // if there are no entry, and the new value it's not a file ... well we don't want it at all. |
||
45 | if (! $entry && ! $value instanceof UploadedFile) { |
||
46 | Arr::set($this->data, $attribute, null); |
||
47 | } |
||
48 | |||
49 | $fieldErrors = $this->validateFieldRules($attribute); |
||
50 | |||
51 | if (! empty($value) && ! empty($this->getFileRules())) { |
||
52 | $fileErrors = $this->validateFileRules($attribute, $value); |
||
53 | } |
||
54 | |||
55 | return array_merge($fieldErrors, $fileErrors ?? []); |
||
56 | } |
||
63 |