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
07:05
created

Update::getUpdateFields()   C

Complexity

Conditions 13
Paths 40

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 40
nop 1
dl 0
loc 49
rs 6.6166
c 0
b 0
f 0

How to fix   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\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);
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
27
        $item = $this->model->findOrFail($id);
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
30
31
        // omit the n-n relationships when updating the eloquent item
32
        $nn_relationships = array_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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Deprecated Code introduced by
The function array_pluck() has been deprecated with message: Arr::pluck() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
33
        $data = array_except($data, $nn_relationships);
0 ignored issues
show
Deprecated Code introduced by
The function array_except() has been deprecated with message: Arr::except() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
34
        $updated = $item->update($data);
0 ignored issues
show
Unused Code introduced by
$updated is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be false|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
43
     *
44
     * @return array The fields with attributes, fake attributes and values.
45
     */
46
    public function getUpdateFields($id = false)
47
    {
48
        $fields = $this->fields();
0 ignored issues
show
Bug introduced by
The method fields() does not exist on Backpack\CRUD\app\Library\CrudPanel\Traits\Update. Did you maybe mean getUpdateFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
49
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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...
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']])) {
75
                            $field['value'] = $fakeStoredInArray[$field['name']];
76
                        }
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