| Conditions | 15 |
| Paths | 3 |
| Total Lines | 57 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 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 | } |
||
| 117 |