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 | 2 | ||
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']) && $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}->{Arr::last(explode('.', $relational_entity))}; |
||
115 | break; |
||
116 | case HasMany::class: |
||
117 | case MorphMany::class: |
||
118 | if (isset($field['pivotFields']) && is_array($field['pivotFields'])) { |
||
119 | $pivot_fields = Arr::where($field['pivotFields'], function ($item) use ($field) { |
||
120 | return $field['name'] != $item['name']; |
||
121 | }); |
||
122 | $related_models = $relatedModel->{$relationMethod}; |
||
123 | $return = []; |
||
124 | |||
125 | // for any given model, we grab the attributes that belong to our pivot table. |
||
126 | foreach ($related_models as $related_model) { |
||
127 | $item = []; |
||
128 | //for any given related model, we attach the pivot fields. |
||
129 | foreach ($pivot_fields as $pivot_field) { |
||
130 | $item[$pivot_field['name']] = $related_model->{$pivot_field['name']}; |
||
131 | } |
||
132 | $item[$related_model->getKeyName()] = $related_model->getKey(); |
||
133 | $return[] = $item; |
||
134 | } |
||
135 | // we return the json encoded result as expected by repeatable field. |
||
136 | return json_encode($return); |
||
137 | } |
||
138 | break; |
||
139 | case BelongsToMany::class: |
||
140 | case MorphToMany::class: |
||
141 | // if pivot is true and there are `pivotFields` we need to get those pivot values to show on the edit page |
||
142 | if (isset($field['pivot']) && $field['pivot'] && isset($field['pivotFields']) && is_array($field['pivotFields'])) { |
||
143 | |||
144 | // we remove our current relation from the pivotFields. |
||
145 | $pivot_fields = Arr::where($field['pivotFields'], function ($item) use ($field) { |
||
146 | return $field['name'] != $item['name']; |
||
147 | }); |
||
148 | |||
149 | $related_models = $relatedModel->{$relationMethod}; |
||
150 | $return = []; |
||
151 | |||
152 | // for any given model, we grab the attributes that belong to our pivot table. |
||
153 | foreach ($related_models as $related_model) { |
||
154 | $item = []; |
||
155 | $item[$field['name']] = $related_model->getKey(); |
||
156 | //for any given related model, we attach the pivot fields. |
||
157 | foreach ($pivot_fields as $pivot_field) { |
||
158 | $item[$pivot_field['name']] = $related_model->pivot->{$pivot_field['name']}; |
||
159 | } |
||
160 | $return[] = $item; |
||
161 | } |
||
162 | |||
163 | // we return the json encoded result as expected by repeatable field. |
||
164 | return json_encode($return); |
||
165 | } |
||
166 | break; |
||
167 | } |
||
168 | } |
||
169 | |||
170 | return $relatedModel->{$relationMethod}; |
||
171 | } |
||
172 | |||
173 | if (is_string($field['name'])) { |
||
174 | return $model->{$field['name']}; |
||
175 | } |
||
176 | |||
177 | if (is_array($field['name'])) { |
||
178 | $result = []; |
||
179 | foreach ($field['name'] as $key => $value) { |
||
180 | $result = $model->{$value}; |
||
181 | } |
||
182 | |||
183 | return $result; |
||
184 | } |
||
187 |