We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 23 |
| Paths | 19 |
| Total Lines | 83 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 95 | private function getModelAttributeValue($model, $field) |
||
| 96 | { |
||
| 97 | if (isset($field['entity'])) { |
||
| 98 | $relational_entity = $this->parseRelationFieldNamesFromHtml([$field])[0]['name']; |
||
| 99 | |||
| 100 | $relation_array = explode('.', $relational_entity); |
||
| 101 | |||
| 102 | $relatedModel = $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 | if ($relation instanceof HasOne || $relation instanceof MorphOne) { |
||
| 110 | return $relatedModel->{$relationMethod}->{Arr::last(explode('.', $relational_entity))}; |
||
| 111 | } |
||
| 112 | |||
| 113 | if ($relation instanceof HasMany || $relation instanceof MorphMany) { |
||
| 114 | if (isset($field['pivotFields']) && is_array($field['pivotFields'])) { |
||
| 115 | $pivot_fields = Arr::where($field['pivotFields'], function ($item) use ($field) { |
||
| 116 | return $field['name'] != $item['name']; |
||
| 117 | }); |
||
| 118 | $related_models = $relatedModel->{$relationMethod}; |
||
| 119 | $return = []; |
||
| 120 | |||
| 121 | // for any given model, we grab the attributes that belong to our pivot table. |
||
| 122 | foreach ($related_models as $related_model) { |
||
| 123 | //for any given related model, we attach the pivot fields. |
||
| 124 | foreach ($pivot_fields as $pivot_field) { |
||
| 125 | $item[$pivot_field['name']] = $related_model->{$pivot_field['name']}; |
||
| 126 | $item[$related_model->getKeyName()] = $related_model->getKey(); |
||
| 127 | } |
||
| 128 | $return[] = $item; |
||
| 129 | } |
||
| 130 | // we return the json encoded result as expected by repeatable field. |
||
| 131 | return json_encode($return); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | if ($relation instanceof BelongsToMany || $relation instanceof MorphToMany) { |
||
| 136 | |||
| 137 | // if pivot is true and there are `pivotFields` we need to get those pivot values to show on the edit page |
||
| 138 | if (isset($field['pivot']) && $field['pivot'] && isset($field['pivotFields']) && is_array($field['pivotFields'])) { |
||
| 139 | |||
| 140 | // we remove our current relation from the pivotFields. |
||
| 141 | $pivot_fields = Arr::where($field['pivotFields'], function ($item) use ($field) { |
||
| 142 | return $field['name'] != $item['name']; |
||
| 143 | }); |
||
| 144 | |||
| 145 | $related_models = $relatedModel->{$relationMethod}; |
||
| 146 | $return = []; |
||
| 147 | |||
| 148 | // for any given model, we grab the attributes that belong to our pivot table. |
||
| 149 | foreach ($related_models as $related_model) { |
||
| 150 | $item[$field['name']] = $related_model->getKey(); |
||
| 151 | //for any given related model, we attach the pivot fields. |
||
| 152 | foreach ($pivot_fields as $pivot_field) { |
||
| 153 | $item[$pivot_field['name']] = $related_model->pivot->{$pivot_field['name']}; |
||
| 154 | } |
||
| 155 | $return[] = $item; |
||
| 156 | } |
||
| 157 | |||
| 158 | // we return the json encoded result as expected by repeatable field. |
||
| 159 | return json_encode($return); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | return $relatedModel->{$relationMethod}; |
||
| 165 | } |
||
| 166 | |||
| 167 | if (is_string($field['name'])) { |
||
| 168 | return $model->{$field['name']}; |
||
| 169 | } |
||
| 170 | |||
| 171 | if (is_array($field['name'])) { |
||
| 172 | $result = []; |
||
| 173 | foreach ($field['name'] as $key => $value) { |
||
| 174 | $result = $model->{$value}; |
||
| 175 | } |
||
| 176 | |||
| 177 | return $result; |
||
| 178 | } |
||
| 181 |