We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 45 |
Total Lines | 261 |
Duplicated Lines | 0 % |
Changes | 18 | ||
Bugs | 9 | Features | 1 |
Complex classes like Update often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Update, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | trait Update |
||
9 | { |
||
10 | /* |
||
11 | |-------------------------------------------------------------------------- |
||
12 | | UPDATE |
||
13 | |-------------------------------------------------------------------------- |
||
14 | */ |
||
15 | |||
16 | /** |
||
17 | * Update a row in the database. |
||
18 | * |
||
19 | * @param int $id The entity's id |
||
20 | * @param array $input All inputs to be updated. |
||
21 | * @return object |
||
22 | */ |
||
23 | public function update($id, $input) |
||
24 | { |
||
25 | $item = $this->model->findOrFail($id); |
||
26 | |||
27 | [$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($input); |
||
|
|||
28 | $updated = $item->update($directInputs); |
||
29 | $this->createRelationsForItem($item, $relationInputs); |
||
30 | |||
31 | return $item; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Get all fields needed for the EDIT ENTRY form. |
||
36 | * |
||
37 | * @param int $id The id of the entry that is being edited. |
||
38 | * @return array The fields with attributes, fake attributes and values. |
||
39 | */ |
||
40 | public function getUpdateFields($id = false) |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Get the value of the 'name' attribute from the declared relation model in the given field. |
||
63 | * |
||
64 | * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model. |
||
65 | * @param array $field The CRUD field array. |
||
66 | * @return mixed The value of the 'name' attribute from the relation model. |
||
67 | */ |
||
68 | private function getModelAttributeValue($model, $field) |
||
69 | { |
||
70 | $model = $model->withFakes(); |
||
71 | |||
72 | $fieldEntity = $field['entity'] ?? false; |
||
73 | $fakeField = $field['fake'] ?? false; |
||
74 | |||
75 | if ($fieldEntity && ! $fakeField) { |
||
76 | return $this->getModelAttributeValueFromRelationship($model, $field); |
||
77 | } |
||
78 | |||
79 | if (is_string($field['name'])) { |
||
80 | return $model->{$field['name']}; |
||
81 | } |
||
82 | |||
83 | if (is_array($field['name'])) { |
||
84 | $result = []; |
||
85 | foreach ($field['name'] as $name) { |
||
86 | $result[] = $model->{$name}; |
||
87 | } |
||
88 | |||
89 | return $result; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Returns the value of the given attribute in the relationship. |
||
95 | * It takes into account nested relationships. |
||
96 | * |
||
97 | * @param \Illuminate\Database\Eloquent\Model $model |
||
98 | * @param array $field |
||
99 | * @return mixed |
||
100 | */ |
||
101 | private function getModelAttributeValueFromRelationship($model, $field) |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * This function checks if the provided model uses the CrudTrait. |
||
189 | * If IT DOES it adds the fakes to the model attributes. |
||
190 | * Otherwise just return the model back. |
||
191 | * |
||
192 | * @param \Illuminate\Database\Eloquent\Model $model |
||
193 | * @return \Illuminate\Database\Eloquent\Model |
||
194 | */ |
||
195 | private function getModelWithFakes($model) |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Returns the model and the method from the relation string starting from the provided model. |
||
206 | * |
||
207 | * @param Illuminate\Database\Eloquent\Model $model |
||
208 | * @param array $field |
||
209 | * @return array |
||
210 | */ |
||
211 | private function getModelAndMethodFromEntity($model, $field) |
||
212 | { |
||
213 | // HasOne and MorphOne relations contains the field in the relation string. We want only the relation part. |
||
214 | $relationEntity = $this->getOnlyRelationEntity($field); |
||
215 | |||
216 | $relationArray = explode('.', $relationEntity); |
||
217 | |||
218 | $relatedModel = array_reduce(array_splice($relationArray, 0, -1), function ($obj, $method) { |
||
219 | // if the string ends with `_id` we strip it out |
||
220 | $method = Str::endsWith($method, '_id') ? Str::replaceLast('_id', '', $method) : $method; |
||
221 | |||
222 | return $obj->{$method} ? $obj->{$method} : $obj; |
||
223 | }, $model); |
||
224 | |||
225 | $relationMethod = Str::afterLast($relationEntity, '.'); |
||
226 | |||
227 | return [$relatedModel, $relationMethod]; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Return the subfields values from the related model. |
||
232 | * |
||
233 | * @param array $subfields |
||
234 | * @param \Illuminate\Database\Eloquent\Model $relatedModel |
||
235 | * @return array |
||
236 | */ |
||
237 | private function getSubfieldsValues($subfields, $relatedModel) |
||
269 | } |
||
270 | } |
||
271 |