We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 12 |
| Paths | 31 |
| Total Lines | 58 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 19 | public function validate(string $attribute, mixed $value, Closure $fail): void |
||
| 20 | { |
||
| 21 | if (! is_array($value)) { |
||
| 22 | try { |
||
| 23 | $value = json_decode($value, true); |
||
| 24 | } catch(\Exception $e) { |
||
| 25 | $fail('Unable to determine the value type'); |
||
| 26 | |||
| 27 | return; |
||
| 28 | } |
||
| 29 | } |
||
| 30 | // `upload_multiple` sends [[0 => null]] (null after `ConvertEmptyStringsToNull`) when nothing changes on the field |
||
| 31 | // for that reason we need to manually check what we are getting from the request |
||
| 32 | if (CrudPanelFacade::getCurrentEntry() !== false) { |
||
|
|
|||
| 33 | $filesToClear = CrudPanelFacade::getRequest()->input('clear_'.$attribute) ?? []; |
||
| 34 | $previousFiles = CrudPanelFacade::getCurrentEntry()->{$attribute} ?? []; |
||
| 35 | |||
| 36 | if (is_string($previousFiles) && ! isset(CrudPanelFacade::getCurrentEntry()->getCasts()[$attribute])) { |
||
| 37 | $previousFiles = json_decode($previousFiles, true); |
||
| 38 | } |
||
| 39 | |||
| 40 | $previousFilesWithoutCleared[$attribute] = array_diff($previousFiles, $filesToClear); |
||
| 41 | |||
| 42 | // we are only going to check if the deleted files could break the validation rules |
||
| 43 | if (count($value) === 1 && empty($value[0])) { |
||
| 44 | $validator = Validator::make($previousFilesWithoutCleared, [ |
||
| 45 | $attribute => $this->arrayRules, |
||
| 46 | ], $this->validator->customMessages, $this->validator->customAttributes); |
||
| 47 | |||
| 48 | if ($validator->fails()) { |
||
| 49 | $fail($validator->errors()->first($attribute)); |
||
| 50 | } |
||
| 51 | |||
| 52 | return; |
||
| 53 | } |
||
| 54 | |||
| 55 | // we are now going to check if the previous files - deleted files + new files still pass the validation |
||
| 56 | $previousFilesWithoutClearedPlusNewFiles[$attribute] = array_merge($previousFilesWithoutCleared[$attribute], $value); |
||
| 57 | |||
| 58 | $validator = Validator::make($previousFilesWithoutClearedPlusNewFiles, [ |
||
| 59 | $attribute => $this->arrayRules, |
||
| 60 | ], $this->validator->customMessages, $this->validator->customAttributes); |
||
| 61 | |||
| 62 | if ($validator->fails()) { |
||
| 63 | $fail($validator->errors()->first($attribute)); |
||
| 64 | |||
| 65 | return; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | // we are now going to perform the file validation on the actual files |
||
| 70 | foreach ($value as $file) { |
||
| 71 | $validator = Validator::make([$attribute => $file], [ |
||
| 72 | $attribute => $this->fileRules, |
||
| 73 | ], $this->validator->customMessages, $this->validator->customAttributes); |
||
| 74 | |||
| 75 | if ($validator->fails()) { |
||
| 76 | $fail($validator->errors()->first($attribute)); |
||
| 77 | } |
||
| 81 |