ValidatesMedia::filterItemRules()   C
last analyzed

Complexity

Conditions 15
Paths 3

Size

Total Lines 57
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 32
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 57
rs 5.9166

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Spatie\MediaLibraryPro\Rules\Concerns;
4
5
use Spatie\MediaLibraryPro\Rules\GroupRules\MinItemsRule;
6
use Spatie\MediaLibraryPro\Rules\GroupRules\MinTotalSizeInKbRule;
7
use Spatie\MediaLibraryPro\Rules\ItemRules\AttributeRule;
8
use Spatie\MediaLibraryPro\Rules\UploadedMediaRules;
9
10
/** @var $this \Illuminate\Foundation\Http\FormRequest */
11
trait ValidatesMedia
12
{
13
    protected function validateSingleMedia(): UploadedMediaRules
14
    {
15
        return (new UploadedMediaRules())->maxItems(1);
16
    }
17
18
    protected function validateMultipleMedia()
19
    {
20
        return new UploadedMediaRules();
21
    }
22
23
    public function validateResolved()
24
    {
25
        $this->prepareForValidation();
0 ignored issues
show
Bug introduced by
It seems like prepareForValidation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        $this->/** @scrutinizer ignore-call */ 
26
               prepareForValidation();
Loading history...
26
27
        if (! $this->passesAuthorization()) {
0 ignored issues
show
Bug introduced by
It seems like passesAuthorization() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        if (! $this->/** @scrutinizer ignore-call */ passesAuthorization()) {
Loading history...
28
            $this->failedAuthorization();
0 ignored issues
show
Bug introduced by
It seems like failedAuthorization() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
            $this->/** @scrutinizer ignore-call */ 
29
                   failedAuthorization();
Loading history...
29
        }
30
31
        $validator = $this->getValidatorInstance();
0 ignored issues
show
Bug introduced by
It seems like getValidatorInstance() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        /** @scrutinizer ignore-call */ 
32
        $validator = $this->getValidatorInstance();
Loading history...
32
33
        $rules = $validator->getRules();
34
35
        $this->rewrittenRules = $this->moveItemRulesToMediaItems($rules);
0 ignored issues
show
Bug Best Practice introduced by
The property rewrittenRules does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
37
        $validator->setRules($this->rewrittenRules);
38
39
        if ($validator->fails()) {
40
            $this->failedValidation($validator);
0 ignored issues
show
Bug introduced by
It seems like failedValidation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            $this->/** @scrutinizer ignore-call */ 
41
                   failedValidation($validator);
Loading history...
41
        }
42
43
        $this->passedValidation();
0 ignored issues
show
Bug introduced by
It seems like passedValidation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $this->/** @scrutinizer ignore-call */ 
44
               passedValidation();
Loading history...
44
    }
45
46
    public function getRewrittenRules(): array
47
    {
48
        return $this->rewrittenRules ?? [];
49
    }
50
51
    public function moveItemRulesToMediaItems(array $rules): array
52
    {
53
        [$itemRules, $remainingRules] = $this->filterItemRules($rules);
54
55
        return array_merge($remainingRules, $itemRules);
56
    }
57
58
    public function filterItemRules(array $allAttributeRules): array
59
    {
60
        $itemRules = [];
61
        $remainingRules = [];
62
63
        foreach ($allAttributeRules as $attribute => $attributeRules) {
64
            $remainingRules[$attribute] = [];
65
66
            if (is_string($attributeRules)) {
67
                $remainingRules[$attribute] = $allAttributeRules;
68
69
                continue;
70
            }
71
72
            foreach ($attributeRules as $rule) {
73
                if (is_string($rule)) {
74
                    $remainingRules[$attribute][] = $rule;
75
                } elseif ($rule instanceof UploadedMediaRules) {
76
                    foreach ($rule->groupRules as $groupRule) {
77
                        $remainingRules[$attribute][] = $groupRule;
78
                    }
79
                    foreach ($rule->itemRules as $itemRule) {
80
                        if ($itemRule instanceof AttributeRule) {
81
                            $ruleAttribute = $itemRule->attribute;
82
83
                            $itemRules["{$attribute}.*.{$ruleAttribute}"][] = $itemRule;
84
                        } else {
85
                            $itemRules["{$attribute}.*"][] = $itemRule;
86
                        }
87
                    }
88
                } else {
89
                    $remainingRules[$attribute][] = $rule;
90
                }
91
92
                $minimumRuleUsed = collect($remainingRules[$attribute])->contains(function ($rule) {
93
                    if (is_string($rule)) {
94
                        return false;
95
                    }
96
97
                    if ($rule instanceof MinItemsRule && $rule->getMinItemCount()) {
98
                        return true;
99
                    }
100
101
                    if ($rule instanceof MinTotalSizeInKbRule && $rule->getMinTotalSizeInKb()) {
102
                        return true;
103
                    }
104
105
                    return false;
106
                });
107
108
                if ($minimumRuleUsed) {
109
                    $remainingRules[$attribute][] = 'required';
110
                }
111
            }
112
        }
113
114
        return [$itemRules, $remainingRules];
115
    }
116
}
117