We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 24 |
Paths | 24 |
Total Lines | 89 |
Code Lines | 53 |
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 |
||
89 | private function getModelAttributeValue($model, $field) |
||
90 | { |
||
91 | if (isset($field['entity']) && $field['entity'] !== false) { |
||
92 | $relational_entity = $this->parseRelationFieldNamesFromHtml([$field])[0]['name']; |
||
93 | |||
94 | $relation_array = explode('.', $relational_entity); |
||
95 | |||
96 | $relatedModel = array_reduce(array_splice($relation_array, 0, -1), function ($obj, $method) { |
||
97 | return $obj->{$method} ? $obj->{$method} : $obj; |
||
98 | }, $model); |
||
99 | |||
100 | $relationMethod = Arr::last($relation_array); |
||
101 | if (method_exists($relatedModel, $relationMethod)) { |
||
102 | $relation = $relatedModel->{$relationMethod}(); |
||
103 | $relation_type = (new \ReflectionClass($relation))->getShortName(); |
||
104 | |||
105 | switch ($relation_type) { |
||
106 | case 'HasOne': |
||
107 | case 'MorphOne': |
||
108 | return $relatedModel->{$relationMethod}->{Arr::last(explode('.', $relational_entity))}; |
||
109 | break; |
||
110 | case 'HasMany': |
||
111 | case 'MorphMany': |
||
112 | if (isset($field['pivotFields']) && is_array($field['pivotFields'])) { |
||
113 | $pivot_fields = Arr::where($field['pivotFields'], function ($item) use ($field) { |
||
114 | return $field['name'] != $item['name']; |
||
115 | }); |
||
116 | $related_models = $relatedModel->{$relationMethod}; |
||
117 | $return = []; |
||
118 | |||
119 | // for any given model, we grab the attributes that belong to our pivot table. |
||
120 | foreach ($related_models as $related_model) { |
||
121 | $item = []; |
||
122 | //for any given related model, we attach the pivot fields. |
||
123 | foreach ($pivot_fields as $pivot_field) { |
||
124 | $item[$pivot_field['name']] = $related_model->{$pivot_field['name']}; |
||
125 | } |
||
126 | $item[$related_model->getKeyName()] = $related_model->getKey(); |
||
127 | $return[] = $item; |
||
128 | } |
||
129 | // we return the json encoded result as expected by repeatable field. |
||
130 | return json_encode($return); |
||
131 | } |
||
132 | break; |
||
133 | case 'BelongsToMany': |
||
134 | case 'MorphToMany': |
||
135 | // if pivot is true and there are `pivotFields` we need to get those pivot values to show on the edit page |
||
136 | if (isset($field['pivot']) && $field['pivot'] && isset($field['pivotFields']) && is_array($field['pivotFields'])) { |
||
137 | |||
138 | // we remove our current relation from the pivotFields. |
||
139 | $pivot_fields = Arr::where($field['pivotFields'], function ($item) use ($field) { |
||
140 | return $field['name'] != $item['name']; |
||
141 | }); |
||
142 | |||
143 | $related_models = $relatedModel->{$relationMethod}; |
||
144 | $return = []; |
||
145 | |||
146 | // for any given model, we grab the attributes that belong to our pivot table. |
||
147 | foreach ($related_models as $related_model) { |
||
148 | $item = []; |
||
149 | $item[$field['name']] = $related_model->getKey(); |
||
150 | //for any given related model, we attach the pivot fields. |
||
151 | foreach ($pivot_fields as $pivot_field) { |
||
152 | $item[$pivot_field['name']] = $related_model->pivot->{$pivot_field['name']}; |
||
153 | } |
||
154 | $return[] = $item; |
||
155 | } |
||
156 | |||
157 | // we return the json encoded result as expected by repeatable field. |
||
158 | return json_encode($return); |
||
159 | } |
||
160 | break; |
||
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 |