We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 53 | 
| Total Lines | 299 | 
| Duplicated Lines | 0 % | 
| Changes | 3 | ||
| Bugs | 1 | Features | 0 | 
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  | 
            ||
| 10 | trait Update  | 
            ||
| 11 | { | 
            ||
| 12 | /*  | 
            ||
| 13 | |--------------------------------------------------------------------------  | 
            ||
| 14 | | UPDATE  | 
            ||
| 15 | |--------------------------------------------------------------------------  | 
            ||
| 16 | */  | 
            ||
| 17 | |||
| 18 | /**  | 
            ||
| 19 | * Update a row in the database.  | 
            ||
| 20 | *  | 
            ||
| 21 | * @param int $id The entity's id  | 
            ||
| 22 | * @param array $input All inputs to be updated.  | 
            ||
| 23 | * @return object  | 
            ||
| 24 | */  | 
            ||
| 25 | public function update($id, $input)  | 
            ||
| 26 |     { | 
            ||
| 27 | $item = $this->model->findOrFail($id);  | 
            ||
| 28 | |||
| 29 | [$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($input);  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 30 |         if ($this->get('update.useDatabaseTransactions') ?? config('backpack.base.useDatabaseTransactions', false)) { | 
            ||
| 31 | return DB::transaction(fn () => $this->updateModelAndRelations($item, $directInputs, $relationInputs));  | 
            ||
| 32 | }  | 
            ||
| 33 | |||
| 34 | return $this->updateModelAndRelations($item, $directInputs, $relationInputs);  | 
            ||
| 35 | }  | 
            ||
| 36 | |||
| 37 | private function updateModelAndRelations(Model $item, array $directInputs, array $relationInputs): Model  | 
            ||
| 38 |     { | 
            ||
| 39 | $item->update($directInputs);  | 
            ||
| 40 | $this->createRelationsForItem($item, $relationInputs);  | 
            ||
| 41 | |||
| 42 | return $item;  | 
            ||
| 43 | }  | 
            ||
| 44 | |||
| 45 | /**  | 
            ||
| 46 | * Get all fields needed for the EDIT ENTRY form.  | 
            ||
| 47 | *  | 
            ||
| 48 | * @param int $id The id of the entry that is being edited.  | 
            ||
| 49 | * @return array The fields with attributes, fake attributes and values.  | 
            ||
| 50 | */  | 
            ||
| 51 | public function getUpdateFields($id = false)  | 
            ||
| 70 | }  | 
            ||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * Get the value of the 'name' attribute from the declared relation model in the given field.  | 
            ||
| 74 | *  | 
            ||
| 75 | * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model.  | 
            ||
| 76 | * @param array $field The CRUD field array.  | 
            ||
| 77 | * @return mixed The value of the 'name' attribute from the relation model.  | 
            ||
| 78 | */  | 
            ||
| 79 | private function getModelAttributeValue($model, $field)  | 
            ||
| 99 | }  | 
            ||
| 100 | |||
| 101 | /**  | 
            ||
| 102 | * Returns the value of the given attribute in the relationship.  | 
            ||
| 103 | * It takes into account nested relationships.  | 
            ||
| 104 | *  | 
            ||
| 105 | * @param \Illuminate\Database\Eloquent\Model $model  | 
            ||
| 106 | * @param array $field  | 
            ||
| 107 | * @return mixed  | 
            ||
| 108 | */  | 
            ||
| 109 | private function getModelAttributeValueFromRelationship($model, $field)  | 
            ||
| 110 |     { | 
            ||
| 111 | [$relatedModel, $relationMethod] = $this->getModelAndMethodFromEntity($model, $field);  | 
            ||
| 112 | |||
| 113 |         if (! method_exists($relatedModel, $relationMethod)) { | 
            ||
| 114 |             return $relatedModel->{$relationMethod}; | 
            ||
| 115 | }  | 
            ||
| 116 | |||
| 117 |         $relation = $relatedModel->{$relationMethod}(); | 
            ||
| 118 | $relationType = Str::afterLast(get_class($relation), '\\');  | 
            ||
| 119 | |||
| 120 |         switch ($relationType) { | 
            ||
| 121 | case 'MorphMany':  | 
            ||
| 122 | case 'HasMany':  | 
            ||
| 123 | case 'BelongsToMany':  | 
            ||
| 124 | case 'MorphToMany':  | 
            ||
| 125 | if(  | 
            ||
| 126 | ($relationType == 'BelongsToMany' || $relationType == 'MorphToMany') &&  | 
            ||
| 127 |                     $relatedModel->{$relationMethod}->count() > 0 && | 
            ||
| 128 |                     $relatedModel->{$relationMethod}[0]->{$relatedModel->{$relationMethod}()->getRelatedPivotKeyName()}){ | 
            ||
| 129 |                     $key = $relatedModel->{$relationMethod}[0]->{$relatedModel->{$relationMethod}()->getRelatedPivotKeyName()}->getKeyName(); | 
            ||
| 130 |                     $relationModels = $relatedModel->{$relationMethod}()->withPivot($key)->get(); | 
            ||
| 131 |                 } else { | 
            ||
| 132 |                     $relationModels = $relatedModel->{$relationMethod}; | 
            ||
| 133 | }  | 
            ||
| 134 | $result = collect();  | 
            ||
| 135 | |||
| 136 |                 foreach ($relationModels as $model) { | 
            ||
| 137 | $model = $this->setupRelatedModelLocale($model);  | 
            ||
| 138 | // when subfields are NOT set we don't need to get any more values  | 
            ||
| 139 | // we just return the plain models as we only need the ids  | 
            ||
| 140 |                     if (! isset($field['subfields'])) { | 
            ||
| 141 | $result->push($model);  | 
            ||
| 142 | |||
| 143 | continue;  | 
            ||
| 144 | }  | 
            ||
| 145 | // when subfields are set we need to parse their values so they can be displayed  | 
            ||
| 146 |                     switch ($relationType) { | 
            ||
| 147 | case 'HasMany':  | 
            ||
| 148 | case 'MorphMany':  | 
            ||
| 149 | // we will get model direct attributes and merge with subfields values.  | 
            ||
| 150 | $directAttributes = $this->getModelWithFakes($model)->getAttributes();  | 
            ||
| 151 | $result->push(array_merge($directAttributes, $this->getSubfieldsValues($field['subfields'], $model)));  | 
            ||
| 152 | break;  | 
            ||
| 153 | |||
| 154 | case 'BelongsToMany':  | 
            ||
| 155 | case 'MorphToMany':  | 
            ||
| 156 | // for any given model, we grab the attributes that belong to our pivot table.  | 
            ||
| 157 |                             $item = $model->{$relation->getPivotAccessor()}->getAttributes(); | 
            ||
| 158 | $item[$relationMethod] = $model->getKey();  | 
            ||
| 159 | $result->push($item);  | 
            ||
| 160 | break;  | 
            ||
| 161 | }  | 
            ||
| 162 | }  | 
            ||
| 163 | |||
| 164 | return $result;  | 
            ||
| 165 | break;  | 
            ||
| 166 | case 'HasOne':  | 
            ||
| 167 | case 'MorphOne':  | 
            ||
| 168 |                 if (! method_exists($relatedModel, $relationMethod)) { | 
            ||
| 169 | return;  | 
            ||
| 170 | }  | 
            ||
| 171 | |||
| 172 |                 $model = $relatedModel->{$relationMethod}; | 
            ||
| 173 | |||
| 174 |                 if (! $model) { | 
            ||
| 175 | return;  | 
            ||
| 176 | }  | 
            ||
| 177 | |||
| 178 | $model = $this->setupRelatedModelLocale($model);  | 
            ||
| 179 | $model = $this->getModelWithFakes($model);  | 
            ||
| 180 | |||
| 181 | // if `entity` contains a dot here it means developer added a main HasOne/MorphOne relation with dot notation  | 
            ||
| 182 |                 if (Str::contains($field['entity'], '.')) { | 
            ||
| 183 |                     return $model->{Str::afterLast($field['entity'], '.')}; | 
            ||
| 184 | }  | 
            ||
| 185 | |||
| 186 | // when subfields exists developer used the repeatable interface to manage this relation  | 
            ||
| 187 |                 if (isset($field['subfields'])) { | 
            ||
| 188 | return [$this->getSubfieldsValues($field['subfields'], $model)];  | 
            ||
| 189 | }  | 
            ||
| 190 | |||
| 191 | return $this->getModelWithFakes($model);  | 
            ||
| 192 | |||
| 193 | break;  | 
            ||
| 194 | case 'BelongsTo':  | 
            ||
| 195 |                 if ($relatedModel->{$relationMethod}) { | 
            ||
| 196 |                     return $relatedModel->{$relationMethod}->getKey(); | 
            ||
| 197 | }  | 
            ||
| 198 | |||
| 199 |                 return $relatedModel->{$relationMethod}; | 
            ||
| 200 | break;  | 
            ||
| 201 | default:  | 
            ||
| 202 |                 return $relatedModel->{$relationMethod}; | 
            ||
| 203 | }  | 
            ||
| 204 | }  | 
            ||
| 205 | |||
| 206 | /**  | 
            ||
| 207 | * Set the locale on the related models.  | 
            ||
| 208 | *  | 
            ||
| 209 | * @param \Illuminate\Database\Eloquent\Model $model  | 
            ||
| 210 | * @return \Illuminate\Database\Eloquent\Model  | 
            ||
| 211 | */  | 
            ||
| 212 | private function setupRelatedModelLocale($model)  | 
            ||
| 222 | }  | 
            ||
| 223 | |||
| 224 | /**  | 
            ||
| 225 | * This function checks if the provided model uses the CrudTrait.  | 
            ||
| 226 | * If IT DOES it adds the fakes to the model attributes.  | 
            ||
| 227 | * Otherwise just return the model back.  | 
            ||
| 228 | *  | 
            ||
| 229 | * @param \Illuminate\Database\Eloquent\Model $model  | 
            ||
| 230 | * @return \Illuminate\Database\Eloquent\Model  | 
            ||
| 231 | */  | 
            ||
| 232 | private function getModelWithFakes($model)  | 
            ||
| 239 | }  | 
            ||
| 240 | |||
| 241 | /**  | 
            ||
| 242 | * Returns the model and the method from the relation string starting from the provided model.  | 
            ||
| 243 | *  | 
            ||
| 244 | * @param Illuminate\Database\Eloquent\Model $model  | 
            ||
| 245 | * @param array $field  | 
            ||
| 246 | * @return array  | 
            ||
| 247 | */  | 
            ||
| 248 | private function getModelAndMethodFromEntity($model, $field)  | 
            ||
| 249 |     { | 
            ||
| 250 | // HasOne and MorphOne relations contains the field in the relation string. We want only the relation part.  | 
            ||
| 251 | $relationEntity = $this->getOnlyRelationEntity($field);  | 
            ||
| 252 | |||
| 253 |         $relationArray = explode('.', $relationEntity); | 
            ||
| 254 | |||
| 255 |         $relatedModel = array_reduce(array_splice($relationArray, 0, -1), function ($obj, $method) { | 
            ||
| 256 | // if the string ends with `_id` we strip it out  | 
            ||
| 257 |             $method = Str::endsWith($method, '_id') ? Str::replaceLast('_id', '', $method) : $method; | 
            ||
| 258 | |||
| 259 |             return $obj->{$method} ? $obj->{$method} : $obj; | 
            ||
| 260 | }, $model);  | 
            ||
| 261 | |||
| 262 | $relationMethod = Str::afterLast($relationEntity, '.');  | 
            ||
| 263 | |||
| 264 | return [$relatedModel, $relationMethod];  | 
            ||
| 265 | }  | 
            ||
| 266 | |||
| 267 | /**  | 
            ||
| 268 | * Return the subfields values from the related model.  | 
            ||
| 269 | *  | 
            ||
| 270 | * @param array $subfields  | 
            ||
| 271 | * @param \Illuminate\Database\Eloquent\Model $relatedModel  | 
            ||
| 272 | * @return array  | 
            ||
| 273 | */  | 
            ||
| 274 | private function getSubfieldsValues($subfields, $relatedModel)  | 
            ||
| 309 | }  | 
            ||
| 310 | }  | 
            ||
| 311 |