Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Create   B
last analyzed

Complexity

Total Complexity 52

Size/Duplication

Total Lines 335
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 107
dl 0
loc 335
rs 7.44
c 0
b 0
f 0
wmc 52

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getCreateFields() 0 3 1
A create() 0 9 2
A attachManyRelation() 0 35 4
C createRelationsForItem() 0 56 17
A createModelAndRelations() 0 6 1
C getRelationFields() 0 29 12
A createUpdateOrDeleteOneToOneRelation() 0 33 6
A handleManyRelationItemRemoval() 0 35 6
A createManyEntries() 0 29 3

How to fix   Complexity   

Complex Class

Complex classes like Create 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 Create, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Facades\DB;
8
9
trait Create
10
{
11
    /*
12
    |--------------------------------------------------------------------------
13
    |                                   CREATE
14
    |--------------------------------------------------------------------------
15
    */
16
17
    /**
18
     * Insert a row in the database.
19
     *
20
     * @param  array  $input  All input values to be inserted.
21
     * @return \Illuminate\Database\Eloquent\Model
22
     */
23
    public function create($input)
24
    {
25
        [$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($input);
0 ignored issues
show
Bug introduced by
It seems like splitInputIntoDirectAndRelations() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        /** @scrutinizer ignore-call */ 
26
        [$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($input);
Loading history...
26
27
        if ($this->get('create.useDatabaseTransactions') ?? config('backpack.base.useDatabaseTransactions', false)) {
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        if ($this->/** @scrutinizer ignore-call */ get('create.useDatabaseTransactions') ?? config('backpack.base.useDatabaseTransactions', false)) {
Loading history...
28
            return DB::transaction(fn () => $this->createModelAndRelations($directInputs, $relationInputs));
29
        }
30
31
        return $this->createModelAndRelations($directInputs, $relationInputs);
32
    }
33
34
    private function createModelAndRelations(array $directInputs, array $relationInputs): Model
35
    {
36
        $item = $this->model->create($directInputs);
37
        $this->createRelationsForItem($item, $relationInputs);
38
39
        return $item;
40
    }
41
42
    /**
43
     * Get all fields needed for the ADD NEW ENTRY form.
44
     *
45
     * @return array The fields with attributes and fake attributes.
46
     */
47
    public function getCreateFields()
48
    {
49
        return $this->fields();
0 ignored issues
show
Bug introduced by
It seems like fields() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        return $this->/** @scrutinizer ignore-call */ fields();
Loading history...
50
    }
51
52
    /**
53
     * Get all fields with relation set (model key set on field).
54
     *
55
     * @param  array  $fields
56
     * @return array The fields with model key set.
57
     */
58
    public function getRelationFields($fields = [])
59
    {
60
        if (empty($fields)) {
61
            $fields = $this->getCleanStateFields();
0 ignored issues
show
Bug introduced by
It seems like getCleanStateFields() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            /** @scrutinizer ignore-call */ 
62
            $fields = $this->getCleanStateFields();
Loading history...
62
        }
63
64
        $relationFields = [];
65
66
        foreach ($fields as $field) {
67
            if (isset($field['model']) && $field['model'] !== false && $field['entity'] !== false) {
68
                array_push($relationFields, $field);
69
            }
70
71
            // if a field has an array name AND subfields
72
            // then take those fields into account (check if they have relationships);
73
            // this is done in particular for the checklist_dependency field,
74
            // but other fields could use it too, in the future;
75
            if ($this->holdsMultipleInputs($field['name']) &&
0 ignored issues
show
Bug introduced by
It seems like holdsMultipleInputs() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            if ($this->/** @scrutinizer ignore-call */ holdsMultipleInputs($field['name']) &&
Loading history...
76
                isset($field['subfields']) &&
77
                is_array($field['subfields'])) {
78
                foreach ($field['subfields'] as $subfield) {
79
                    if (isset($subfield['model']) && $subfield['model'] !== false) {
80
                        array_push($relationFields, $subfield);
81
                    }
82
                }
83
            }
84
        }
85
86
        return $relationFields;
87
    }
88
89
    /**
90
     * ---------------
91
     * PRIVATE METHODS
92
     * ---------------.
93
     */
94
95
    /**
96
     * Create relations for the provided model.
97
     *
98
     * @param  \Illuminate\Database\Eloquent\Model  $item  The current CRUD model.
99
     * @param  array  $formattedRelations  The form data.
100
     * @return bool|null
101
     */
102
    private function createRelationsForItem($item, $formattedRelations)
103
    {
104
        // no relations to create
105
        if (empty($formattedRelations)) {
106
            return false;
107
        }
108
109
        foreach ($formattedRelations as $relationMethod => $relationDetails) {
110
            $relation = $item->{$relationMethod}();
111
            $relationType = $relationDetails['relation_type'];
112
113
            switch ($relationType) {
114
                case 'HasOne':
115
                case 'MorphOne':
116
                    $this->createUpdateOrDeleteOneToOneRelation($relation, $relationMethod, $relationDetails);
117
                    break;
118
                case 'HasMany':
119
                case 'MorphMany':
120
                    $relationValues = $relationDetails['values'][$relationMethod];
121
                    // if relation values are null we can only attach, also we check if we sent
122
                    // - a single dimensional array: [1,2,3]
123
                    // - an array of arrays: [[1][2][3]]
124
                    // if is as single dimensional array we can only attach.
125
                    if ($relationValues === null || ! is_multidimensional_array($relationValues)) {
126
                        $this->attachManyRelation($item, $relation, $relationDetails, $relationValues);
127
                    } else {
128
                        $this->createManyEntries($item, $relation, $relationMethod, $relationDetails);
129
                    }
130
                    break;
131
                case 'BelongsToMany':
132
                case 'MorphToMany':
133
                    $values = $relationDetails['values'][$relationMethod] ?? [];
134
                    $values = is_string($values) ? json_decode($values, true) : $values;
135
136
                    // disabling ConvertEmptyStringsToNull middleware may return null from json_decode() if an empty string is used.
137
                    // we need to make sure no null value can go foward so we reassure that values is not null after json_decode()
138
                    $values = $values ?? [];
139
140
                    $relationValues = [];
141
142
                    if (is_array($values) && is_multidimensional_array($values)) {
143
                        foreach ($values as $value) {
144
                            if (isset($value[$relationMethod])) {
145
                                $relationValues[$value[$relationMethod]] = Arr::except($value, $relationMethod);
146
                            }
147
                        }
148
                    }
149
150
                    // if there is no relation data, and the values array is single dimensional we have
151
                    // an array of keys with no aditional pivot data. sync those.
152
                    if (empty($relationValues)) {
153
                        $relationValues = array_values($values);
154
                    }
155
156
                    $item->{$relationMethod}()->sync($relationValues);
157
                    break;
158
            }
159
        }
160
    }
161
162
    /**
163
     * Save the attributes of a given HasOne or MorphOne relationship on the
164
     * related entry, create or delete it, depending on what was sent in the form.
165
     *
166
     * For HasOne and MorphOne relationships, the dev might want to a few different things:
167
     * (A) save an attribute on the related entry (eg. passport.number)
168
     * (B) set an attribute on the related entry to NULL (eg. slug.slug)
169
     * (C) save an entire related entry (eg. passport)
170
     * (D) delete the entire related entry (eg. passport)
171
     *
172
     * @param  \Illuminate\Database\Eloquent\Relations\HasOne|\Illuminate\Database\Eloquent\Relations\MorphOne  $relation
173
     * @param  string  $relationMethod  The name of the relationship method on the main Model.
174
     * @param  array  $relationDetails  Details about that relationship. For example:
175
     *                                  [
176
     *                                  'model' => 'App\Models\Passport',
177
     *                                  'parent' => 'App\Models\Pet',
178
     *                                  'entity' => 'passport',
179
     *                                  'attribute' => 'passport',
180
     *                                  'values' => **THE TRICKY BIT**,
181
     *                                  ]
182
     * @return Model|null
183
     */
184
    private function createUpdateOrDeleteOneToOneRelation($relation, $relationMethod, $relationDetails)
185
    {
186
        // Let's see which scenario we're treating, depending on the contents of $relationDetails:
187
        //      - (A) ['number' => 1315, 'name' => 'Something'] (if passed using a text/number/etc field)
188
        //      - (B) ['slug' => null] (if the 'slug' attribute on the 'slug' related entry needs to be cleared)
189
        //      - (C) ['passport' => [['number' => 1314, 'name' => 'Something']]] (if passed using a repeatable field)
190
        //      - (D) ['passport' => null] (if deleted from the repeatable field)
191
192
        // Scenario C or D
193
        if (array_key_exists($relationMethod, $relationDetails['values'])) {
194
            $relationMethodValue = $relationDetails['values'][$relationMethod];
195
196
            // Scenario D
197
            if (is_null($relationMethodValue) && $relationDetails['entity'] === $relationMethod) {
198
                $relation->first()?->delete();
199
200
                return null;
201
            }
202
203
            // Scenario C (when it's an array inside an array, because it's been added as one item inside a repeatable field)
204
            if (gettype($relationMethodValue) == 'array' && is_multidimensional_array($relationMethodValue)) {
205
                $relationMethodValue = $relationMethodValue[0];
206
            }
207
        }
208
        // saving process
209
        $input = $relationMethodValue ?? $relationDetails['values'];
210
        [$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($input, $relationDetails, $relationMethod);
211
212
        $item = $relation->updateOrCreate([], $directInputs);
213
214
        $this->createRelationsForItem($item, $relationInputs);
215
216
        return $item;
217
    }
218
219
    /**
220
     * When using the HasMany/MorphMany relations as selectable elements we use this function to "mimic-sync" in those relations.
221
     * Since HasMany/MorphMany does not have the `sync` method, we manually re-create it.
222
     * Here we add the entries that developer added and remove the ones that are not in the list.
223
     * This removal process happens with the following rules:
224
     * - by default Backpack will behave like a `sync` from M-M relations: it deletes previous entries and add only the current ones.
225
     * - `force_delete` is configurable in the field, it's `true` by default. When false, if connecting column is nullable instead of deleting the row we set the column to null.
226
     * - `fallback_id` could be provided. In this case instead of deleting we set the connecting key to whatever developer gives us.
227
     *
228
     * @return mixed
229
     */
230
    private function attachManyRelation($item, $relation, $relationDetails, $relationValues)
231
    {
232
        $modelInstance = $relation->getRelated();
233
        $relationForeignKey = $relation->getForeignKeyName();
234
        $relationLocalKey = $relation->getLocalKeyName();
235
236
        if (empty($relationValues)) {
237
            // the developer cleared the selection
238
            // we gonna clear all related values by setting up the value to the fallback id, to null or delete.
239
            return $this->handleManyRelationItemRemoval($modelInstance, $relation, $relationDetails, $relationForeignKey);
240
        }
241
        // we add the new values into the relation, if it is HasMany we only update the foreign_key,
242
        // otherwise (it's a MorphMany) we need to update the morphs keys too
243
        $toUpdate[$relationForeignKey] = $item->{$relationLocalKey};
0 ignored issues
show
Comprehensibility Best Practice introduced by
$toUpdate was never initialized. Although not strictly required by PHP, it is generally a good practice to add $toUpdate = array(); before regardless.
Loading history...
244
245
        if ($relationDetails['relation_type'] === 'MorphMany') {
246
            $toUpdate[$relation->getQualifiedMorphType()] = $relation->getMorphClass();
247
        }
248
249
        $modelInstance->whereIn($modelInstance->getKeyName(), $relationValues)
250
            ->update($toUpdate);
251
252
        // we clear up any values that were removed from model relation.
253
        // if developer provided a fallback id, we use it
254
        // if column is nullable we set it to null if developer didn't specify `force_delete => true`
255
        // if none of the above we delete the model from database
256
        $removedEntries = $modelInstance->whereNotIn($modelInstance->getKeyName(), $relationValues)
257
                            ->where($relationForeignKey, $item->{$relationLocalKey});
258
259
        // if relation is MorphMany we also match by morph type.
260
        if ($relationDetails['relation_type'] === 'MorphMany') {
261
            $removedEntries->where($relation->getQualifiedMorphType(), $relation->getMorphClass());
262
        }
263
264
        return $this->handleManyRelationItemRemoval($modelInstance, $removedEntries, $relationDetails, $relationForeignKey);
265
    }
266
267
    private function handleManyRelationItemRemoval($modelInstance, $removedEntries, $relationDetails, $relationForeignKey)
268
    {
269
        $relationColumnIsNullable = $modelInstance->isColumnNullable($relationForeignKey);
270
        $forceDelete = $relationDetails['force_delete'] ?? false;
271
        $fallbackId = $relationDetails['fallback_id'] ?? false;
272
273
        // developer provided a fallback_id he knows what he's doing, just use it.
274
        if ($fallbackId) {
275
            return $removedEntries->update([$relationForeignKey => $fallbackId]);
276
        }
277
278
        // developer set force_delete => true, so we don't care if it's nullable or not,
279
        // we just follow developer's will
280
        if ($forceDelete) {
281
            return $removedEntries->lazy()->each->delete();
282
        }
283
284
        // get the default that could be set at database level.
285
        $dbColumnDefault = $modelInstance->getDbColumnDefault($relationForeignKey);
286
287
        // check if the relation foreign key is in casts, and cast it to the correct type
288
        if ($modelInstance->hasCast($relationForeignKey)) {
289
            $dbColumnDefault = match ($modelInstance->getCasts()[$relationForeignKey]) {
290
                'int', 'integer' => $dbColumnDefault = (int) $dbColumnDefault,
291
                default => $dbColumnDefault = $dbColumnDefault
0 ignored issues
show
Unused Code introduced by
The assignment to $dbColumnDefault is dead and can be removed.
Loading history...
292
            };
293
        }
294
        // if column is not nullable in database, and there is no column default (null),
295
        // we will delete the entry from the database, otherwise it will throw and ugly DB error.
296
        if (! $relationColumnIsNullable && $dbColumnDefault === null) {
297
            return $removedEntries->lazy()->each->delete();
298
        }
299
300
        // if column is nullable we just set it to the column default (null when it does exist, or the default value when it does).
301
        return $removedEntries->update([$relationForeignKey => $dbColumnDefault]);
302
    }
303
304
    /**
305
     * Handle HasMany/MorphMany relations when used as creatable entries in the crud.
306
     * By using repeatable field, developer can allow the creation of such entries
307
     * in the crud forms.
308
     *
309
     * @param  $entry  - eg: story
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
310
     * @param  $relation  - eg  story HasMany monsters
311
     * @param  $relationMethod  - eg: monsters
312
     * @param  $relationDetails  - eg: info about relation including submited values
313
     * @return void
314
     */
315
    private function createManyEntries($entry, $relation, $relationMethod, $relationDetails)
316
    {
317
        $items = $relationDetails['values'][$relationMethod];
318
319
        $relatedModelLocalKey = $relation->getRelated()->getKeyName();
320
321
        $relatedItemsSent = [];
322
323
        foreach ($items as $item) {
324
            [$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($item, $relationDetails, $relationMethod);
325
            // for each item we get the inputs to create and the relations of it.
326
            $relatedModelLocalKeyValue = $item[$relatedModelLocalKey] ?? null;
327
328
            // we either find the matched entry by local_key (usually `id`)
329
            // and update the values from the input
330
            // or create a new item from input
331
            $item = $entry->{$relationMethod}()->updateOrCreate([$relatedModelLocalKey => $relatedModelLocalKeyValue], $directInputs);
332
333
            // we store the item local key so we can match them with database and check if any item was deleted
334
            $relatedItemsSent[] = $item->{$relatedModelLocalKey};
335
336
            // create the item relations if any.
337
            $this->createRelationsForItem($item, $relationInputs);
338
        }
339
340
        // use the collection of sent ids to match against database ids, delete the ones not found in the submitted ids.
341
        if (! empty($relatedItemsSent)) {
342
            // we perform the cleanup of removed database items
343
            $entry->{$relationMethod}()->whereNotIn($relatedModelLocalKey, $relatedItemsSent)->lazy()->each->delete();
344
        }
345
    }
346
}
347