We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 18 |
| Paths | 480 |
| Total Lines | 49 |
| Code Lines | 26 |
| 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 |
||
| 18 | public function uploadFiles(Model $entry, $value = null) |
||
| 19 | { |
||
| 20 | if ($value && isset($value[0]) && is_null($value[0])) { |
||
| 21 | $value = false; |
||
| 22 | } |
||
| 23 | |||
| 24 | $filesToDelete = $this->getFilesToDeleteFromRequest(); |
||
| 25 | $value = $value ?? collect($value)->flatten()->toArray(); |
||
| 26 | $previousFiles = $this->getPreviousFiles($entry) ?? []; |
||
| 27 | |||
| 28 | if (is_array($previousFiles) && empty($previousFiles[0] ?? [])) { |
||
| 29 | $previousFiles = []; |
||
| 30 | } |
||
| 31 | |||
| 32 | if (! is_array($previousFiles) && is_string($previousFiles)) { |
||
| 33 | $previousFiles = json_decode($previousFiles, true); |
||
| 34 | } |
||
| 35 | |||
| 36 | if ($filesToDelete) { |
||
|
|
|||
| 37 | foreach ($previousFiles as $previousFile) { |
||
| 38 | if (in_array($previousFile, $filesToDelete)) { |
||
| 39 | Storage::disk($this->getDisk())->delete($previousFile); |
||
| 40 | |||
| 41 | $previousFiles = Arr::where($previousFiles, function ($value, $key) use ($previousFile) { |
||
| 42 | return $value != $previousFile; |
||
| 43 | }); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | if (! is_array($value)) { |
||
| 49 | $value = []; |
||
| 50 | } |
||
| 51 | |||
| 52 | foreach ($value as $file) { |
||
| 53 | if ($file && is_file($file)) { |
||
| 54 | $fileName = $this->getFileName($file); |
||
| 55 | $file->storeAs($this->getPath(), $fileName, $this->getDisk()); |
||
| 56 | $previousFiles[] = $this->getPath().$fileName; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | $previousFiles = array_values($previousFiles); |
||
| 61 | |||
| 62 | if (empty($previousFiles)) { |
||
| 63 | return null; |
||
| 64 | } |
||
| 65 | |||
| 66 | return isset($entry->getCasts()[$this->getName()]) || $this->isFake() ? $previousFiles : json_encode($previousFiles); |
||
| 67 | } |
||
| 128 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.