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
Push — v4dot1 ( d0147e...2219a0 )
by
unknown
05:44
created

FieldsProtectedMethods::makeSureFieldHasName()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
nc 3
nop 1
dl 0
loc 11
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
8
trait FieldsProtectedMethods
9
{
10
    /**
11
     * The only REALLY MANDATORY attribute when defining a field is the 'name'.
12
     * Everything else Backpack can probably guess. This method makes sure  the
13
     * field definition array is complete, by guessing missing attributes.
14
     *
15
     * @param  string|array $field  The definition of a field (string or array).
16
     * @return array                The correct definition of that field.
17
     */
18
    protected function makeSureFieldHasNecessaryAttributes($field)
19
    {
20
        $field = $this->makeSureFieldHasName($field);
21
        $field = $this->makeSureFieldHasModel($field);
22
        $field = $this->makeSureFieldHasLabel($field);
23
        $field = $this->makeSureFieldHasEntity($field);
24
        $field = $this->makeSureFieldHasAttribute($field);
25
        $field = $this->makeSureFieldHasRelationshipData($field);
26
        $field = $this->makeSureFieldHasType($field);
27
28
        return $field;
29
    }
30
31
    /**
32
     * If the field_definition_array array is a string, it means the programmer was lazy
33
     * and has only passed the name of the field. Turn that into a proper array.
34
     *
35
     * @param  string|array $field The field definition array (or string).
36
     * @return array
37
     */
38
    protected function makeSureFieldHasName($field)
39
    {
40
        if (is_string($field)) {
41
            return ['name' => $field];
42
        }
43
44
        if (is_array($field) && ! isset($field['name'])) {
45
            abort(500, 'All fields must have their name defined');
46
        }
47
48
        return $field;
49
    }
50
51
    /**
52
     * If entity is not present, but it looks like the field SHOULD be a relationship field,
53
     * try to determine the method on the model that defines the relationship, and pass it to
54
     * the field as 'entity'.
55
     *
56
     * @param  [type] $field [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
57
     * @return [type]        [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
58
     */
59
    protected function makeSureFieldHasEntity($field)
60
    {
61
        if (isset($field['entity'])) {
62
            return $field;
63
        }
64
65
        // if the name is an array it's definitely not a relationship
66
        if (is_array($field['name'])) {
67
            return $field;
68
        }
69
70
        // if there's a method on the model with this name
71
        if (method_exists($this->model, $field['name'])) {
72
73
            if($this->model->checkIfMethodReturnRelation($field['name'])) {
74
                $field['entity'] = $field['name'];
75
            }
76
            return $field;
77
78
        }
79
80
        // if the name ends with _id and that method exists,
81
        // we can probably use it as an entity
82
        if (Str::endsWith($field['name'], '_id')) {
83
            $possibleMethodName = Str::replaceLast('_id', '', $field['name']);
84
85
            if (method_exists($this->model, $possibleMethodName)) {
86
                if($this->model->checkIfMethodReturnRelation($field['name'])) {
87
                    $field['entity'] = $possibleMethodName;
88
                }
89
                return $field;
90
            }
91
        }
92
93
        // if there's a column in the db for this field name
94
        // most likely it doesn't need 'entity', UNLESS it's a foreign key
95
        // TODO: make this work
96
         if ($relation_method = $this->checkIfFieldNameBelongsToAnyRelation($field['name'])) {
0 ignored issues
show
Bug introduced by
It seems like checkIfFieldNameBelongsToAnyRelation() 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

96
         if ($relation_method = $this->/** @scrutinizer ignore-call */ checkIfFieldNameBelongsToAnyRelation($field['name'])) {
Loading history...
97
             $field['entity'] = $relation_method['entity'];
98
         }
99
100
        return $field;
101
    }
102
103
    protected function makeSureFieldHasRelationshipData($field)
104
    {
105
        // only do this if "entity" is defined on the field
106
        if (! isset($field['entity'])) {
107
            return $field;
108
        }
109
110
        $extraFieldAttributes = $this->inferFieldAttributesFromRelationship($field['entity']);
0 ignored issues
show
Bug introduced by
It seems like inferFieldAttributesFromRelationship() 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

110
        /** @scrutinizer ignore-call */ 
111
        $extraFieldAttributes = $this->inferFieldAttributesFromRelationship($field['entity']);
Loading history...
111
112
        if ($extraFieldAttributes !== false) {
113
            $field = array_merge($extraFieldAttributes, $field);
114
        } else {
115
            abort(500, 'Unable to process relationship data: '.$field['name']);
116
        }
117
118
        return $field;
119
    }
120
121
    protected function makeSureFieldHasModel($field)
122
    {
123
        // if this is a relation type field and no corresponding model was specified,
124
        // get it from the relation method defined in the main model
125
        if (isset($field['entity']) && ! isset($field['model'])) {
126
            $field['model'] = $this->getRelationModel($field['entity']);
0 ignored issues
show
Bug introduced by
It seems like getRelationModel() 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

126
            /** @scrutinizer ignore-call */ 
127
            $field['model'] = $this->getRelationModel($field['entity']);
Loading history...
127
        }
128
129
        return $field;
130
    }
131
132
    protected function makeSureFieldHasAttribute($field)
133
    {
134
        // if there's a model defined, but no attribute
135
        // guess an attribute using the indentifiableAttribute functionality in CrudTrait
136
        if (isset($field['model']) && ! isset($field['attribute'])) {
137
            $field['attribute'] = call_user_func([(new $field['model']),'identifiableAttribute']);
138
        }
139
140
        return $field;
141
    }
142
143
    /**
144
     * Set the label of a field, if it's missing, by capitalizing the name and replacing
145
     * underscores with spaces.
146
     *
147
     * @param  array $field Field definition array.
148
     * @return array        Field definition array that contains label too.
149
     */
150
    protected function makeSureFieldHasLabel($field)
151
    {
152
        if (! isset($field['label'])) {
153
            $name = is_array($field['name']) ? $field['name'][0] : $field['name'];
154
            $name = str_replace('_id', '', $name);
155
            $field['label'] = mb_ucfirst(str_replace('_', ' ', $name));
156
        }
157
158
        return $field;
159
    }
160
161
    /**
162
     * Set the type of a field, if it's missing, by inferring it from the
163
     * db column type.
164
     *
165
     * @param  array $field Field definition array.
166
     * @return array        Field definition array that contains type too.
167
     */
168
    protected function makeSureFieldHasType($field)
169
    {
170
        if (! isset($field['type'])) {
171
            $field['type'] = $this->inferFieldTypeFromDbColumnType($field['name']);
0 ignored issues
show
Bug introduced by
It seems like inferFieldTypeFromDbColumnType() 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

171
            /** @scrutinizer ignore-call */ 
172
            $field['type'] = $this->inferFieldTypeFromDbColumnType($field['name']);
Loading history...
172
        }
173
174
        return $field;
175
    }
176
177
    /**
178
     * Enable the tabs functionality, if a field has a tab defined.
179
     *
180
     * @param  array $field Field definition array.
181
     * @return void
182
     */
183
    protected function enableTabsIfFieldUsesThem($field)
184
    {
185
        // if a tab was mentioned, we should enable it
186
        if (isset($field['tab'])) {
187
            if (! $this->tabsEnabled()) {
0 ignored issues
show
Bug introduced by
It seems like tabsEnabled() 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

187
            if (! $this->/** @scrutinizer ignore-call */ tabsEnabled()) {
Loading history...
188
                $this->enableTabs();
0 ignored issues
show
Bug introduced by
The method enableTabs() does not exist on Backpack\CRUD\app\Librar...\FieldsProtectedMethods. Did you maybe mean enableTabsIfFieldUsesThem()? ( Ignorable by Annotation )

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

188
                $this->/** @scrutinizer ignore-call */ 
189
                       enableTabs();

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...
189
            }
190
        }
191
    }
192
193
    /**
194
     * Add a field to the current operation, using the Settings API.
195
     *
196
     * @param  array $field Field definition array.
197
     */
198
    protected function addFieldToOperationSettings($field)
199
    {
200
        $fieldKey = $this->getFieldKey($field);
201
202
        $allFields = $this->getOperationSetting('fields');
0 ignored issues
show
Bug introduced by
It seems like getOperationSetting() 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

202
        /** @scrutinizer ignore-call */ 
203
        $allFields = $this->getOperationSetting('fields');
Loading history...
Unused Code introduced by
The assignment to $allFields is dead and can be removed.
Loading history...
203
        $allFields = Arr::add($this->fields(), $fieldKey, $field);
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

203
        $allFields = Arr::add($this->/** @scrutinizer ignore-call */ fields(), $fieldKey, $field);
Loading history...
204
205
        $this->setOperationSetting('fields', $allFields);
0 ignored issues
show
Bug introduced by
It seems like setOperationSetting() 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

205
        $this->/** @scrutinizer ignore-call */ 
206
               setOperationSetting('fields', $allFields);
Loading history...
206
    }
207
208
    /**
209
     * Get the string that should be used as an array key, for the attributive array
210
     * where the fields are stored for the current operation.
211
     *
212
     * The array key for the field should be:
213
     * - name (if the name is a string)
214
     * - name1_name2_name3 (if the name is an array)
215
     *
216
     * @param  array $field Field definition array.
217
     * @return string       The string that should be used as array key.
218
     */
219
    protected function getFieldKey($field)
220
    {
221
        if (is_array($field['name'])) {
222
            return implode('_', $field['name']);
223
        }
224
225
        return $field['name'];
226
    }
227
}
228