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 Failed
Pull Request — master (#3410)
by
unknown
14:20
created

Update   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 69
dl 0
loc 165
rs 9.92
c 5
b 2
f 0
wmc 31

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getUpdateFields() 0 29 7
A update() 0 18 1
D getModelAttributeValue() 0 83 23
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6
use Illuminate\Database\Eloquent\Relations\HasMany;
7
use Illuminate\Database\Eloquent\Relations\HasOne;
8
use Illuminate\Database\Eloquent\Relations\MorphMany;
9
use Illuminate\Database\Eloquent\Relations\MorphOne;
10
use Illuminate\Database\Eloquent\Relations\MorphToMany;
11
use Illuminate\Support\Arr;
12
13
trait Update
14
{
15
    /*
16
    |--------------------------------------------------------------------------
17
    |                                   UPDATE
18
    |--------------------------------------------------------------------------
19
    */
20
21
    /**
22
     * Update a row in the database.
23
     *
24
     * @param int   $id   The entity's id
25
     * @param array $data All inputs to be updated.
26
     *
27
     * @return object
28
     */
29
    public function update($id, $data)
30
    {
31
        $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

31
        /** @scrutinizer ignore-call */ 
32
        $data = $this->decodeJsonCastedAttributes($data);
Loading history...
32
        $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

32
        /** @scrutinizer ignore-call */ 
33
        $data = $this->compactFakeFields($data);
Loading history...
33
        $item = $this->model->findOrFail($id);
34
35
        // omit the n-n relationships when updating the eloquent item
36
        $nn_relationships = Arr::pluck($this->getRelationFieldsWithPivot(), 'name');
0 ignored issues
show
Bug introduced by
It seems like getRelationFieldsWithPivot() 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

36
        $nn_relationships = Arr::pluck($this->/** @scrutinizer ignore-call */ getRelationFieldsWithPivot(), 'name');
Loading history...
37
38
        // handle BelongsTo 1:1 relations
39
        $item = $this->associateOrDissociateBelongsToRelations($item, $data);
0 ignored issues
show
Bug introduced by
It seems like associateOrDissociateBelongsToRelations() 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

39
        /** @scrutinizer ignore-call */ 
40
        $item = $this->associateOrDissociateBelongsToRelations($item, $data);
Loading history...
40
41
        $item->fill(Arr::except($data, $nn_relationships));
42
        $item->save();
43
44
        $this->createRelations($item, $data);
0 ignored issues
show
Bug introduced by
It seems like createRelations() 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

44
        $this->/** @scrutinizer ignore-call */ 
45
               createRelations($item, $data);
Loading history...
45
46
        return $item;
47
    }
48
49
    /**
50
     * Get all fields needed for the EDIT ENTRY form.
51
     *
52
     * @param int $id The id of the entry that is being edited.
53
     *
54
     * @return array The fields with attributes, fake attributes and values.
55
     */
56
    public function getUpdateFields($id = false)
57
    {
58
        $fields = $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

58
        /** @scrutinizer ignore-call */ 
59
        $fields = $this->fields();
Loading history...
59
        $entry = ($id != false) ? $this->getEntry($id) : $this->getCurrentEntry();
0 ignored issues
show
Bug introduced by
It seems like getEntry() 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

59
        $entry = ($id != false) ? $this->/** @scrutinizer ignore-call */ getEntry($id) : $this->getCurrentEntry();
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing $id of type false|integer against false; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
Bug introduced by
It seems like getCurrentEntry() 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

59
        $entry = ($id != false) ? $this->getEntry($id) : $this->/** @scrutinizer ignore-call */ getCurrentEntry();
Loading history...
60
61
        foreach ($fields as &$field) {
62
            // set the value
63
            if (! isset($field['value'])) {
64
                if (isset($field['subfields'])) {
65
                    $field['value'] = [];
66
                    foreach ($field['subfields'] as $subfield) {
67
                        $field['value'][] = $entry->{$subfield['name']};
68
                    }
69
                } else {
70
                    $field['value'] = $this->getModelAttributeValue($entry, $field);
71
                }
72
            }
73
        }
74
75
        // always have a hidden input for the entry id
76
        if (! array_key_exists('id', $fields)) {
77
            $fields['id'] = [
78
                'name'  => $entry->getKeyName(),
79
                'value' => $entry->getKey(),
80
                'type'  => 'hidden',
81
            ];
82
        }
83
84
        return $fields;
85
    }
86
87
    /**
88
     * Get the value of the 'name' attribute from the declared relation model in the given field.
89
     *
90
     * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model.
91
     * @param array                               $field The CRUD field array.
92
     *
93
     * @return mixed The value of the 'name' attribute from the relation model.
94
     */
95
    private function getModelAttributeValue($model, $field)
96
    {
97
        if (isset($field['entity'])) {
98
            $relational_entity = $this->parseRelationFieldNamesFromHtml([$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

98
            $relational_entity = $this->/** @scrutinizer ignore-call */ parseRelationFieldNamesFromHtml([$field])[0]['name'];
Loading history...
99
100
            $relation_array = explode('.', $relational_entity);
101
102
            $relatedModel = $relatedModel = array_reduce(array_splice($relation_array, 0, -1), function ($obj, $method) {
0 ignored issues
show
Unused Code introduced by
The assignment to $relatedModel is dead and can be removed.
Loading history...
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
                if ($relation instanceof HasOne || $relation instanceof MorphOne) {
110
                    return $relatedModel->{$relationMethod}->{Arr::last(explode('.', $relational_entity))};
111
                }
112
113
                if ($relation instanceof HasMany || $relation instanceof MorphMany) {
114
                    if (isset($field['pivotFields']) && is_array($field['pivotFields'])) {
115
                        $pivot_fields = Arr::where($field['pivotFields'], function ($item) use ($field) {
116
                            return $field['name'] != $item['name'];
117
                        });
118
                        $related_models = $relatedModel->{$relationMethod};
119
                        $return = [];
120
121
                        // for any given model, we grab the attributes that belong to our pivot table.
122
                        foreach ($related_models as $related_model) {
123
                            //for any given related model, we attach the pivot fields.
124
                            foreach ($pivot_fields as $pivot_field) {
125
                                $item[$pivot_field['name']] = $related_model->{$pivot_field['name']};
126
                                $item[$related_model->getKeyName()] = $related_model->getKey();
127
                            }
128
                            $return[] = $item;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $item does not seem to be defined for all execution paths leading up to this point.
Loading history...
129
                        }
130
                        // we return the json encoded result as expected by repeatable field.
131
                        return json_encode($return);
132
                    }
133
                }
134
135
                if ($relation instanceof BelongsToMany || $relation instanceof MorphToMany) {
136
137
                    // if pivot is true and there are `pivotFields` we need to get those pivot values to show on the edit page
138
                    if (isset($field['pivot']) && $field['pivot'] && isset($field['pivotFields']) && is_array($field['pivotFields'])) {
139
140
                        // we remove our current relation from the pivotFields.
141
                        $pivot_fields = Arr::where($field['pivotFields'], function ($item) use ($field) {
142
                            return $field['name'] != $item['name'];
143
                        });
144
145
                        $related_models = $relatedModel->{$relationMethod};
146
                        $return = [];
147
148
                        // for any given model, we grab the attributes that belong to our pivot table.
149
                        foreach ($related_models as $related_model) {
150
                            $item[$field['name']] = $related_model->getKey();
151
                            //for any given related model, we attach the pivot fields.
152
                            foreach ($pivot_fields as $pivot_field) {
153
                                $item[$pivot_field['name']] = $related_model->pivot->{$pivot_field['name']};
154
                            }
155
                            $return[] = $item;
156
                        }
157
158
                        // we return the json encoded result as expected by repeatable field.
159
                        return json_encode($return);
160
                    }
161
                }
162
            }
163
164
            return $relatedModel->{$relationMethod};
165
        }
166
167
        if (is_string($field['name'])) {
168
            return $model->{$field['name']};
169
        }
170
171
        if (is_array($field['name'])) {
172
            $result = [];
173
            foreach ($field['name'] as $key => $value) {
174
                $result = $model->{$value};
175
            }
176
177
            return $result;
178
        }
179
    }
180
}
181