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 (#2349)
by Mustafa
05:46
created

Update::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
4
5
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
6
7
trait Update
8
{
9
    /*
10
    |--------------------------------------------------------------------------
11
    |                                   UPDATE
12
    |--------------------------------------------------------------------------
13
    */
14
15
    /**
16
     * Update a row in the database.
17
     *
18
     * @param int   $id   The entity's id
19
     * @param array $data All inputs to be updated.
20
     *
21
     * @return object
22
     */
23
    public function update($id, $data)
24
    {
25
        $data = $this->decodeJsonCastedAttributes($data);
26
        $data = $this->compactFakeFields($data);
27
        $item = $this->model->findOrFail($id);
28
29
        $this->createRelations($item, $data);
30
31
        // omit the n-n relationships when updating the eloquent item
32
        $nn_relationships = array_pluck($this->getRelationFieldsWithPivot(), 'name');
33
        $data = array_except($data, $nn_relationships);
34
        $updated = $item->update($data);
35
36
        return $item;
37
    }
38
39
    /**
40
     * Get all fields needed for the EDIT ENTRY form.
41
     *
42
     * @param int $id The id of the entry that is being edited.
43
     *
44
     * @return array The fields with attributes, fake attributes and values.
45
     */
46
    public function getUpdateFields($id = false)
47
    {
48
        $fields = $this->fields();
49
        $entry = ($id != false) ? $this->getEntry($id) : $this->getCurrentEntry();
50
51
        foreach ($fields as &$field) {
52
            // set the value
53
            if (! isset($field['value'])) {
54
                if (isset($field['subfields'])) {
55
                    $field['value'] = [];
56
                    foreach ($field['subfields'] as $subfield) {
57
                        $field['value'][] = $entry->{$subfield['name']};
58
                    }
59
60
                    // handle fake fields
61
                } elseif (! empty($field['fake'])) {
62
                    // determine the stored-in attribute
63
                    $fakeStoredInAttribute = $field['store_in'] ?? 'extras';
64
                    // check if the fake stored-in attribute exists
65
                    if (! empty($entry->{$fakeStoredInAttribute})) {
66
                        $fakeStoredInArray = $entry->{$fakeStoredInAttribute};
67
                        // check if it's a string, decode it
68
                        // otherwise, it should be an array
69
                        if (is_string($fakeStoredInArray)) {
70
                            // decode it
71
                            $fakeStoredInArray = json_decode($fakeStoredInArray, true);
72
                        }
73
74
+                        if (! empty($fakeStoredInArray) && is_array($fakeStoredInArray) && isset($fakeStoredInArray[$field['name']])) {
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_IF
Loading history...
75
                            $field['value'] = $fakeStoredInArray[$field['name']];
76
                        }
0 ignored issues
show
Coding Style introduced by
Closing brace indented incorrectly; expected 0 spaces, found 24
Loading history...
77
                    }
78
                } else {
79
                    $field['value'] = $this->getModelAttributeValue($entry, $field);
80
                }
81
            }
82
        }
83
84
        // always have a hidden input for the entry id
85
        if (! array_key_exists('id', $fields)) {
86
            $fields['id'] = [
87
                'name'  => $entry->getKeyName(),
88
                'value' => $entry->getKey(),
89
                'type'  => 'hidden',
90
            ];
91
        }
92
93
        return $fields;
94
    }
95
96
    /**
97
     * Get the value of the 'name' attribute from the declared relation model in the given field.
98
     *
99
     * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model.
100
     * @param array                               $field The CRUD field array.
101
     *
102
     * @return mixed The value of the 'name' attribute from the relation model.
103
     */
104
    private function getModelAttributeValue($model, $field)
105
    {
106
        if (isset($field['entity'])) {
107
            $relationArray = explode('.', $field['entity']);
108
            $relatedModel = array_reduce(array_splice($relationArray, 0, -1), function ($obj, $method) {
109
                return $obj->{$method} ? $obj->{$method} : $obj;
110
            }, $model);
111
112
            $relationMethod = end($relationArray);
113
            if ($relatedModel->{$relationMethod} && $relatedModel->{$relationMethod}() instanceof HasOneOrMany) {
114
                return $relatedModel->{$relationMethod}->{$field['name']};
115
            } else {
116
                return $relatedModel->{$field['name']};
117
            }
118
        }
119
120
        if (is_string($field['name'])) {
121
            return $model->{$field['name']};
122
        }
123
124
        if (is_array($field['name'])) {
125
            $result = [];
126
            foreach ($field['name'] as $key => $value) {
127
                $result = $model->{$value};
128
            }
129
130
            return $result;
131
        }
132
    }
133
}
134