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

Test Setup Failed
Pull Request — master (#3250)
by
unknown
15:41
created

Create::associateBelongsToRelations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\HasOne;
7
use Illuminate\Support\Arr;
8
9
trait Create
10
{
11
    /*
12
    |--------------------------------------------------------------------------
13
    |                                   CREATE
14
    |--------------------------------------------------------------------------
15
    */
16
17
    /**
18
     * Insert a row in the database.
19
     *
20
     * @param array $data All input values to be inserted.
21
     *
22
     * @return \Illuminate\Database\Eloquent\Model
23
     */
24
    public function create($data)
25
    {
26
        $data = $this->decodeJsonCastedAttributes($data);
0 ignored issues
show
Bug introduced by
It seems like decodeJsonCastedAttributes() 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

26
        /** @scrutinizer ignore-call */ 
27
        $data = $this->decodeJsonCastedAttributes($data);
Loading history...
27
        $data = $this->compactFakeFields($data);
0 ignored issues
show
Bug introduced by
It seems like compactFakeFields() 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
        /** @scrutinizer ignore-call */ 
28
        $data = $this->compactFakeFields($data);
Loading history...
28
29
        // omit the n-n relationships when updating the eloquent item
30
        $nn_relationships = Arr::pluck($this->getRelationFieldsWithPivot(), 'name');
31
32
        // init and fill model
33
        $item = $this->model->make(Arr::except($data, $nn_relationships));
34
35
        // handle BelongsTo 1:1 relations
36
        $item = $this->associateBelongsToRelations($item, $data);
37
        $item->save();
38
39
        // if there are any relationships available, also sync those
40
        $this->createRelations($item, $data);
41
42
        return $item;
43
    }
44
45
    /**
46
     * Get all fields needed for the ADD NEW ENTRY form.
47
     *
48
     * @return array The fields with attributes and fake attributes.
49
     */
50
    public function getCreateFields()
51
    {
52
        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

52
        return $this->/** @scrutinizer ignore-call */ fields();
Loading history...
53
    }
54
55
    /**
56
     * Get all fields with relation set (model key set on field).
57
     *
58
     * @return array The fields with model key set.
59
     */
60
    public function getRelationFields()
61
    {
62
        $fields = $this->fields();
63
        $relationFields = [];
64
65
        foreach ($fields as $field) {
66
            if (isset($field['model']) && $field['model'] !== false) {
67
                array_push($relationFields, $field);
68
            }
69
70
            if (isset($field['subfields']) &&
71
                is_array($field['subfields']) &&
72
                count($field['subfields'])) {
73
                foreach ($field['subfields'] as $subfield) {
74
                    array_push($relationFields, $subfield);
75
                }
76
            }
77
        }
78
79
        return $relationFields;
80
    }
81
82
    /**
83
     * Associate and dissociate.
84
     *
85
     * @param  Model
86
     * @param  array The form data.
0 ignored issues
show
Bug introduced by
The type Backpack\CRUD\app\Library\CrudPanel\Traits\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
87
     * @return Model Model with relationships set up.
88
     */
89
    public function associateBelongsToRelations($item, array $data)
90
    {
91
        $belongsToFields = $this->getFieldsWithRelationType('BelongsTo');
0 ignored issues
show
Bug introduced by
It seems like getFieldsWithRelationType() 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

91
        /** @scrutinizer ignore-call */ 
92
        $belongsToFields = $this->getFieldsWithRelationType('BelongsTo');
Loading history...
92
93
        foreach ($belongsToFields as $relationField) {
94
            $item->{$this->getOnlyRelationEntity($relationField)}()->associate($relationField['model']::find(Arr::get($data, $relationField['name'])));
95
        }
96
        return $item;
97
    }
98
99
    /**
100
     * Get all fields with n-n relation set (pivot table is true).
101
     *
102
     * @return array The fields with n-n relationships.
103
     */
104
    public function getRelationFieldsWithPivot()
105
    {
106
        $all_relation_fields = $this->getRelationFields();
107
108
        return Arr::where($all_relation_fields, function ($value, $key) {
109
            return isset($value['pivot']) && $value['pivot'];
110
        });
111
    }
112
113
    /**
114
     * Create the relations for the current model.
115
     *
116
     * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model.
117
     * @param array                               $data The form data.
118
     */
119
    public function createRelations($item, $data)
120
    {
121
        $this->syncPivot($item, $data);
122
        $this->createOneToOneRelations($item, $data);
123
    }
124
125
    /**
126
     * Sync the declared many-to-many associations through the pivot field.
127
     *
128
     * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model.
129
     * @param array                               $data  The form data.
130
     */
131
    public function syncPivot($model, $data)
132
    {
133
        $fields_with_relationships = $this->getRelationFields();
134
        foreach ($fields_with_relationships as $key => $field) {
135
            if (isset($field['pivot']) && $field['pivot']) {
136
                $values = isset($data[$field['name']]) ? $data[$field['name']] : [];
137
138
                // if a JSON was passed instead of an array, turn it into an array
139
                if (is_string($values)) {
140
                    $values = json_decode($values);
141
                }
142
143
                $relation_data = [];
144
                foreach ($values as $pivot_id) {
145
                    $pivot_data = [];
146
147
                    if (isset($field['pivotFields'])) {
148
                        foreach ($field['pivotFields'] as $pivot_field_name) {
149
                            $pivot_data[$pivot_field_name] = $data[$pivot_field_name][$pivot_id];
150
                        }
151
                    }
152
                    $relation_data[$pivot_id] = $pivot_data;
153
                }
154
155
                $model->{$field['name']}()->sync($relation_data);
156
            }
157
158
            if (isset($field['morph']) && $field['morph'] && isset($data[$field['name']])) {
159
                $values = $data[$field['name']];
160
                $model->{$field['name']}()->sync($values);
161
            }
162
        }
163
    }
164
165
    /**
166
     * Create any existing one to one relations for the current model from the form data.
167
     *
168
     * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model.
169
     * @param array                               $data The form data.
170
     */
171
    private function createOneToOneRelations($item, $data)
172
    {
173
        $relationData = $this->getRelationDataFromFormData($data);
174
        $this->createRelationsForItem($item, $relationData);
175
    }
176
177
    /**
178
     * Create any existing one to one relations for the current model from the relation data.
179
     *
180
     * @param \Illuminate\Database\Eloquent\Model $item          The current CRUD model.
181
     * @param array                               $formattedData The form data.
182
     *
183
     * @return bool|null
184
     */
185
    private function createRelationsForItem($item, $formattedData)
186
    {
187
        if (! isset($formattedData['relations'])) {
188
            return false;
189
        }
190
        foreach ($formattedData['relations'] as $relationMethod => $relationData) {
191
            if (! isset($relationData['model'])) {
192
                continue;
193
            }
194
            $model = $relationData['model'];
195
            $relation = $item->{$relationMethod}();
196
197
            if ($relation instanceof HasOne) {
198
                if ($item->{$relationMethod} != null) {
199
                    $item->{$relationMethod}->update($relationData['values']);
200
                    $modelInstance = $item->{$relationMethod};
201
                } else {
202
                    $modelInstance = new $model($relationData['values']);
203
                    $relation->save($modelInstance);
204
                }
205
            }
206
207
            if (isset($relationData['relations'])) {
208
                $this->createRelationsForItem($modelInstance, ['relations' => $relationData['relations']]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $modelInstance does not seem to be defined for all execution paths leading up to this point.
Loading history...
209
            }
210
        }
211
    }
212
213
    /**
214
     * Get a relation data array from the form data.
215
     * For each relation defined in the fields through the entity attribute, set the model, the parent model and the
216
     * attribute values.
217
     *
218
     * We traverse this relation array later to create the relations, for example:
219
     *
220
     * Current model HasOne Address, this Address (line_1, country_id) BelongsTo Country through country_id in Address Model.
221
     *
222
     * So when editing current model crud user have two fields address.line_1 and address.country (we infer country_id from relation)
223
     *
224
     * Those will be nested accordingly in this relation array, so address relation will have a nested relation with country.
225
     *
226
     *
227
     * @param array $data The form data.
228
     *
229
     * @return array The formatted relation data.
230
     */
231
    private function getRelationDataFromFormData($data)
232
    {
233
        $relation_fields = $this->getRelationFields();
234
        $relationData = [];
235
        foreach ($relation_fields as $relation_field) {
236
            $attributeKey = $this->parseRelationFieldNamesFromHtml([$relation_field])[0]['name'];
0 ignored issues
show
Bug introduced by
It seems like parseRelationFieldNamesFromHtml() 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

236
            $attributeKey = $this->/** @scrutinizer ignore-call */ parseRelationFieldNamesFromHtml([$relation_field])[0]['name'];
Loading history...
237
238
            if (! is_null(Arr::get($data, $attributeKey)) && isset($relation_field['pivot']) && $relation_field['pivot'] !== true) {
239
                $key = implode('.relations.', explode('.', $this->getOnlyRelationEntity($relation_field)));
240
                $fieldData = Arr::get($relationData, 'relations.'.$key, []);
241
                if (! array_key_exists('model', $fieldData)) {
242
                    $fieldData['model'] = $relation_field['model'];
243
                }
244
                if (! array_key_exists('parent', $fieldData)) {
245
                    $fieldData['parent'] = $this->getRelationModel($attributeKey, -1);
0 ignored issues
show
Bug introduced by
The method getRelationModel() does not exist on Backpack\CRUD\app\Library\CrudPanel\Traits\Create. Did you maybe mean getRelationFields()? ( Ignorable by Annotation )

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

245
                    /** @scrutinizer ignore-call */ 
246
                    $fieldData['parent'] = $this->getRelationModel($attributeKey, -1);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
246
                }
247
                $relatedAttribute = Arr::last(explode('.', $attributeKey));
248
                $fieldData['values'][$relatedAttribute] = Arr::get($data, $attributeKey);
249
250
                Arr::set($relationData, 'relations.'.$key, $fieldData);
251
            }
252
        }
253
254
        return $relationData;
255
    }
256
257
    public function getOnlyRelationEntity($relation_field)
258
    {
259
        $entity_array = explode('.', $relation_field['entity']);
260
261
        $relation_model = $this->getRelationModel($relation_field['entity'], -1);
262
263
        $related_method = Arr::last($entity_array);
264
265
        if (! method_exists($relation_model, $related_method)) {
266
            if (count($entity_array) <= 1) {
267
                return $relation_field['entity'];
268
            } else {
269
                array_pop($entity_array);
270
            }
271
272
            return implode('.', $entity_array);
273
        }
274
275
        return $relation_field['entity'];
276
    }
277
}
278