We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 14 |
| Paths | 9 |
| Total Lines | 56 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 3 | 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 |
||
| 87 | private function getModelAttributeValue($model, $field) |
||
| 88 | { |
||
| 89 | if (isset($field['entity'])) { |
||
| 90 | $relational_entity = $this->parseRelationFieldNamesFromHtml([$field])[0]['name']; |
||
| 91 | |||
| 92 | $relation_array = explode('.', $relational_entity); |
||
| 93 | |||
| 94 | $relatedModel = $relatedModel = array_reduce(array_splice($relation_array, 0, -1), function ($obj, $method) { |
||
| 95 | return $obj->{$method} ? $obj->{$method} : $obj; |
||
| 96 | }, $model); |
||
| 97 | |||
| 98 | $relationMethod = Arr::last($relation_array); |
||
| 99 | |||
| 100 | if (method_exists($relatedModel, $relationMethod) && $relatedModel->{$relationMethod}() instanceof HasOne) { |
||
| 101 | return $relatedModel->{$relationMethod}->{Arr::last(explode('.', $relational_entity))}; |
||
| 102 | } else { |
||
| 103 | // if pivot is true and there is `fields` array in this field we are trying to sync a pivot with |
||
| 104 | // extra attributes on it. It's a Repeatable Field so its values are sent as json. |
||
| 105 | if (isset($field['pivot']) && $field['pivot'] && isset($field['fields']) && is_array($field['fields'])) { |
||
| 106 | //we remove the first field from repeatable because it is our relation. |
||
| 107 | $pivot_fields = Arr::where($field['fields'], function ($item) use ($field) { |
||
| 108 | return $field['name'] != $item['name']; |
||
| 109 | }); |
||
| 110 | |||
| 111 | //we grab the related models |
||
| 112 | $related_models = $relatedModel->{$relationMethod}; |
||
| 113 | $return = []; |
||
| 114 | |||
| 115 | //for any given model, we grab the attributes that belong to our pivot table. |
||
| 116 | foreach ($related_models as $related_model) { |
||
| 117 | $item[$field['name']] = $related_model->getKey(); |
||
| 118 | //for any given related model, we attach the pivot fields. |
||
| 119 | foreach ($pivot_fields as $pivot_field) { |
||
| 120 | $item[$pivot_field['name']] = $related_model->pivot->{$pivot_field['name']}; |
||
| 121 | } |
||
| 122 | $return[] = $item; |
||
| 123 | } |
||
| 124 | //we return the json encoded result as expected by repeatable field. |
||
| 125 | return json_encode($return); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $relatedModel->{$relationMethod}; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | if (is_string($field['name'])) { |
||
| 133 | return $model->{$field['name']}; |
||
| 134 | } |
||
| 135 | |||
| 136 | if (is_array($field['name'])) { |
||
| 137 | $result = []; |
||
| 138 | foreach ($field['name'] as $key => $value) { |
||
| 139 | $result = $model->{$value}; |
||
| 140 | } |
||
| 141 | |||
| 142 | return $result; |
||
| 143 | } |
||
| 146 |