We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 15 |
| Paths | 16 |
| Total Lines | 49 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 2 | 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 |
||
| 95 | private function getModelAttributeValue($model, $field) |
||
| 96 | { |
||
| 97 | if (isset($field['entity']) && $field['entity'] !== false) { |
||
| 98 | $relational_entity = $this->parseRelationFieldNamesFromHtml([$field])[0]['name']; |
||
| 99 | |||
| 100 | $relation_array = explode('.', $relational_entity); |
||
| 101 | |||
| 102 | $relatedModel = array_reduce(array_splice($relation_array, 0, -1), function ($obj, $method) { |
||
| 103 | return $obj->{$method} ? $obj->{$method} : $obj; |
||
| 104 | }, $model); |
||
| 105 | |||
| 106 | $relationMethod = Arr::last($relation_array); |
||
| 107 | if (method_exists($relatedModel, $relationMethod)) { |
||
| 108 | $relation = $relatedModel->{$relationMethod}(); |
||
| 109 | $relation_type = get_class($relation); |
||
| 110 | |||
| 111 | switch ($relation_type) { |
||
| 112 | case HasOne::class: |
||
| 113 | case MorphOne::class: |
||
| 114 | return $relatedModel->{$relationMethod}->{Str::afterLast($relational_entity, '.')}; |
||
| 115 | break; |
||
| 116 | |||
| 117 | case HasMany::class: |
||
| 118 | case MorphMany::class: |
||
| 119 | case BelongsToMany::class: |
||
| 120 | case MorphToMany::class: |
||
| 121 | $attribute_value = $this->getManyRelationAttributeValue($relatedModel, $relationMethod, $field, $relation_type); |
||
| 122 | // we only want to return the json_encoded values here |
||
| 123 | if (is_string($attribute_value)) { |
||
| 124 | return $attribute_value; |
||
| 125 | } |
||
| 126 | break; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | return $relatedModel->{$relationMethod}; |
||
| 131 | } |
||
| 132 | |||
| 133 | if (is_string($field['name'])) { |
||
| 134 | return $model->{$field['name']}; |
||
| 135 | } |
||
| 136 | |||
| 137 | if (is_array($field['name'])) { |
||
| 138 | $result = []; |
||
| 139 | foreach ($field['name'] as $key => $value) { |
||
| 140 | $result = $model->{$value}; |
||
| 141 | } |
||
| 142 | |||
| 143 | return $result; |
||
| 144 | } |
||
| 198 |