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

Passed
Pull Request — master (#2938)
by Cristian
08:20
created

Update::getModelAttributeValue()   C

Complexity

Conditions 14
Paths 9

Size

Total Lines 56
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 3 Features 0
Metric Value
cc 14
eloc 29
c 5
b 3
f 0
nc 9
nop 2
dl 0
loc 56
rs 6.2666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
4
5
use Illuminate\Support\Arr;
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);
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

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

26
        /** @scrutinizer ignore-call */ 
27
        $data = $this->compactFakeFields($data);
Loading history...
27
        $item = $this->model->findOrFail($id);
28
29
        $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

29
        $this->/** @scrutinizer ignore-call */ 
30
               createRelations($item, $data);
Loading history...
30
31
        // omit the n-n relationships when updating the eloquent item
32
        $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

32
        $nn_relationships = Arr::pluck($this->/** @scrutinizer ignore-call */ getRelationFieldsWithPivot(), 'name');
Loading history...
33
34
        $data = Arr::except($data, $nn_relationships);
35
36
        $updated = $item->update($data);
0 ignored issues
show
Unused Code introduced by
The assignment to $updated is dead and can be removed.
Loading history...
37
38
        return $item;
39
    }
40
41
    /**
42
     * Get all fields needed for the EDIT ENTRY form.
43
     *
44
     * @param int $id The id of the entry that is being edited.
45
     *
46
     * @return array The fields with attributes, fake attributes and values.
47
     */
48
    public function getUpdateFields($id = false)
49
    {
50
        $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

50
        /** @scrutinizer ignore-call */ 
51
        $fields = $this->fields();
Loading history...
51
        $entry = ($id != false) ? $this->getEntry($id) : $this->getCurrentEntry();
0 ignored issues
show
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 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

51
        $entry = ($id != false) ? $this->/** @scrutinizer ignore-call */ getEntry($id) : $this->getCurrentEntry();
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

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

90
            $relational_entity = $this->/** @scrutinizer ignore-call */ parseRelationFieldNamesFromHtml([$field])[0]['name'];
Loading history...
91
92
            $relation_array = explode('.', $relational_entity);
93
94
            $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...
95
                return $obj->{$method} ? $obj->{$method} : $obj;
96
            }, $model);
97
98
            $relationMethod = Arr::last($relation_array);
99
100
            if (method_exists($relatedModel, $relationMethod) && $relatedModel->{$relationMethod}() instanceof HasOne) {
0 ignored issues
show
Bug introduced by
The type Backpack\CRUD\app\Library\CrudPanel\Traits\HasOne 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...
101
                return $relatedModel->{$relationMethod}->{Arr::last(explode('.', $relational_entity))};
102
            } else {
103
                // if pivot is true and there is `fields` array in this field we are trying to sync a pivot with
104
                // extra attributes on it. It's a Repeatable Field so its values are sent as json.
105
                if (isset($field['pivot']) && $field['pivot'] && isset($field['fields']) && is_array($field['fields'])) {
106
                    //we remove the first field from repeatable because it is our relation.
107
                    $pivot_fields = Arr::where($field['fields'], function ($item) use ($field) {
108
                        return $field['name'] != $item['name'];
109
                    });
110
111
                    //we grab the related models
112
                    $related_models = $relatedModel->{$relationMethod};
113
                    $return = [];
114
115
                    //for any given model, we grab the attributes that belong to our pivot table.
116
                    foreach ($related_models as $related_model) {
117
                        $item[$field['name']] = $related_model->getKey();
118
                        //for any given related model, we attach the pivot fields.
119
                        foreach ($pivot_fields as $pivot_field) {
120
                            $item[$pivot_field['name']] = $related_model->pivot->{$pivot_field['name']};
121
                        }
122
                        $return[] = $item;
123
                    }
124
                    //we return the json encoded result as expected by repeatable field.
125
                    return json_encode($return);
126
                }
127
128
                return $relatedModel->{$relationMethod};
129
            }
130
        }
131
132
        if (is_string($field['name'])) {
133
            return $model->{$field['name']};
134
        }
135
136
        if (is_array($field['name'])) {
137
            $result = [];
138
            foreach ($field['name'] as $key => $value) {
139
                $result = $model->{$value};
140
            }
141
142
            return $result;
143
        }
144
    }
145
}
146