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 (#2940)
by Cristian
14:26
created

overwriteFieldNamesFromDotNotationToArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
4
5
use Illuminate\Support\Str;
6
7
trait FieldsProtectedMethods
8
{
9
    /**
10
     * If field has entity we want to get the relation type from it.
11
     *
12
     * @param  array  $field
13
     * @return array
14
     */
15
    public function makeSureFieldHasRelationType($field)
16
    {
17
        $field['relation_type'] = $field['relation_type'] ?? $this->inferRelationTypeFromRelationship($field);
0 ignored issues
show
Bug introduced by
It seems like inferRelationTypeFromRelationship() 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

17
        $field['relation_type'] = $field['relation_type'] ?? $this->/** @scrutinizer ignore-call */ inferRelationTypeFromRelationship($field);
Loading history...
18
19
        return $field;
20
    }
21
22
    /**
23
     * If field has entity we want to make sure it also has a model for that relation.
24
     *
25
     * @param  array  $field
26
     * @return array
27
     */
28
    public function makeSureFieldHasModel($field)
29
    {
30
        $field['model'] = $field['model'] ?? $this->inferFieldModelFromRelationship($field);
0 ignored issues
show
Bug introduced by
It seems like inferFieldModelFromRelationship() 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

30
        $field['model'] = $field['model'] ?? $this->/** @scrutinizer ignore-call */ inferFieldModelFromRelationship($field);
Loading history...
31
32
        return $field;
33
    }
34
35
    /**
36
     * Based on relation type we can guess if pivot is set.
37
     *
38
     * @param  array  $field
39
     * @return array
40
     */
41
    public function makeSureFieldHasPivot($field)
42
    {
43
        $field['pivot'] = $field['pivot'] ?? $this->guessIfFieldHasPivotFromRelationType($field['relation_type']);
0 ignored issues
show
Bug introduced by
It seems like guessIfFieldHasPivotFromRelationType() 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

43
        $field['pivot'] = $field['pivot'] ?? $this->/** @scrutinizer ignore-call */ guessIfFieldHasPivotFromRelationType($field['relation_type']);
Loading history...
44
45
        return $field;
46
    }
47
48
    /**
49
     * Based on relation type we can try to guess if it is a multiple field.
50
     *
51
     * @param  array  $field
52
     * @return array
53
     */
54
    public function makeSureFieldHasMultiple($field)
55
    {
56
        if (isset($field['relation_type'])) {
57
            $field['multiple'] = $field['multiple'] ?? $this->guessIfFieldHasMultipleFromRelationType($field['relation_type']);
0 ignored issues
show
Bug introduced by
It seems like guessIfFieldHasMultipleFromRelationType() 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

57
            $field['multiple'] = $field['multiple'] ?? $this->/** @scrutinizer ignore-call */ guessIfFieldHasMultipleFromRelationType($field['relation_type']);
Loading history...
58
        }
59
60
        return $field;
61
    }
62
63
    /**
64
     * In case field name is dot notation we want to convert it to a valid HTML array field name for validation purposes.
65
     *
66
     * @param  array  $field
67
     * @return array
68
     */
69
    public function overwriteFieldNameFromDotNotationToArray($field)
70
    {
71
        if (! is_array($field['name']) && strpos($field['name'], '.') !== false) {
72
            $entity_array = explode('.', $field['name']);
73
            $name_string = '';
74
75
            foreach ($entity_array as $key => $array_entity) {
76
                $name_string .= ($key == 0) ? $array_entity : '['.$array_entity.']';
77
            }
78
79
            $field['name'] = $name_string;
80
        }
81
82
        return $field;
83
    }
84
85
    /**
86
     * Run the field name overwrite in multiple fields.
87
     *
88
     * @param  array  $fields
89
     * @return array
90
     */
91
    public function overwriteFieldNamesFromDotNotationToArray($fields)
92
    {
93
        foreach ($fields as $key => $field) {
94
            $fields[$key] = $this->overwriteFieldNameFromDotNotationToArray($field);
95
        }
96
97
        return $fields;
98
    }
99
100
    /**
101
     * If the field_definition_array array is a string, it means the programmer was lazy
102
     * and has only passed the name of the field. Turn that into a proper array.
103
     *
104
     * @param  string|array  $field  The field definition array (or string).
105
     * @return array
106
     */
107
    protected function makeSureFieldHasName($field)
108
    {
109
        if (is_string($field)) {
110
            return ['name' => $field];
111
        }
112
113
        if (is_array($field) && ! isset($field['name'])) {
114
            abort(500, 'All fields must have their name defined');
115
        }
116
117
        return $field;
118
    }
119
120
    /**
121
     * If entity is not present, but it looks like the field SHOULD be a relationship field,
122
     * try to determine the method on the model that defines the relationship, and pass it to
123
     * the field as 'entity'.
124
     *
125
     * @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...
126
     * @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...
127
     */
128
    protected function makeSureFieldHasEntity($field)
129
    {
130
        if (isset($field['entity'])) {
131
            return $field;
132
        }
133
134
        // if the name is an array it's definitely not a relationship
135
        if (is_array($field['name'])) {
136
            return $field;
137
        }
138
139
        //if the name is dot notation we are sure it's a relationship
140
        if (strpos($field['name'], '.') !== false) {
141
            $field['entity'] = $field['name'];
142
143
            return $field;
144
        }
145
146
        // if there's a method on the model with this name
147
        if (method_exists($this->model, $field['name'])) {
148
            $field['entity'] = $field['name'];
149
150
            return $field;
151
        }
152
153
        // if the name ends with _id and that method exists,
154
        // we can probably use it as an entity
155
        if (Str::endsWith($field['name'], '_id')) {
156
            $possibleMethodName = Str::replaceLast('_id', '', $field['name']);
157
158
            if (method_exists($this->model, $possibleMethodName)) {
159
                $field['entity'] = $possibleMethodName;
160
161
                return $field;
162
            }
163
        }
164
165
        return $field;
166
    }
167
168
    protected function makeSureFieldHasAttribute($field)
169
    {
170
        // if there's a model defined, but no attribute
171
        // guess an attribute using the identifiableAttribute functionality in CrudTrait
172
        if (isset($field['model']) && ! isset($field['attribute']) && method_exists($field['model'], 'identifiableAttribute')) {
173
            $field['attribute'] = call_user_func([(new $field['model']), 'identifiableAttribute']);
174
        }
175
176
        return $field;
177
    }
178
179
    /**
180
     * Set the label of a field, if it's missing, by capitalizing the name and replacing
181
     * underscores with spaces.
182
     *
183
     * @param  array  $field  Field definition array.
184
     * @return array Field definition array that contains label too.
185
     */
186
    protected function makeSureFieldHasLabel($field)
187
    {
188
        if (! isset($field['label'])) {
189
            $name = is_array($field['name']) ? $field['name'][0] : $field['name'];
190
            $name = str_replace('_id', '', $name);
191
            $field['label'] = mb_ucfirst(str_replace('_', ' ', $name));
192
        }
193
194
        return $field;
195
    }
196
197
    /**
198
     * Set the type of a field, if it's missing, by inferring it from the
199
     * db column type.
200
     *
201
     * @param  array  $field  Field definition array.
202
     * @return array Field definition array that contains type too.
203
     */
204
    protected function makeSureFieldHasType($field)
205
    {
206
        if (! isset($field['type'])) {
207
            $field['type'] = isset($field['relation_type']) ? $this->inferFieldTypeFromFieldRelation($field) : $this->inferFieldTypeFromDbColumnType($field['name']);
0 ignored issues
show
Bug introduced by
It seems like inferFieldTypeFromFieldRelation() 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

207
            $field['type'] = isset($field['relation_type']) ? $this->/** @scrutinizer ignore-call */ inferFieldTypeFromFieldRelation($field) : $this->inferFieldTypeFromDbColumnType($field['name']);
Loading history...
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

207
            $field['type'] = isset($field['relation_type']) ? $this->inferFieldTypeFromFieldRelation($field) : $this->/** @scrutinizer ignore-call */ inferFieldTypeFromDbColumnType($field['name']);
Loading history...
208
        }
209
210
        return $field;
211
    }
212
213
    /**
214
     * Enable the tabs functionality, if a field has a tab defined.
215
     *
216
     * @param  array  $field  Field definition array.
217
     * @return void
218
     */
219
    protected function enableTabsIfFieldUsesThem($field)
220
    {
221
        // if a tab was mentioned, we should enable it
222
        if (isset($field['tab'])) {
223
            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

223
            if (! $this->/** @scrutinizer ignore-call */ tabsEnabled()) {
Loading history...
224
                $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

224
                $this->/** @scrutinizer ignore-call */ 
225
                       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...
225
            }
226
        }
227
    }
228
229
    /**
230
     * Add a field to the current operation, using the Settings API.
231
     *
232
     * @param  array  $field  Field definition array.
233
     */
234
    protected function addFieldToOperationSettings($field)
235
    {
236
        $fieldKey = $this->getFieldKey($field);
237
238
        $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

238
        /** @scrutinizer ignore-call */ 
239
        $allFields = $this->getOperationSetting('fields');
Loading history...
Unused Code introduced by
The assignment to $allFields is dead and can be removed.
Loading history...
239
        $allFields = array_merge($this->getCleanStateFields(), [$fieldKey => $field]);
0 ignored issues
show
Bug introduced by
It seems like getCleanStateFields() 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

239
        $allFields = array_merge($this->/** @scrutinizer ignore-call */ getCleanStateFields(), [$fieldKey => $field]);
Loading history...
240
241
        $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

241
        $this->/** @scrutinizer ignore-call */ 
242
               setOperationSetting('fields', $allFields);
Loading history...
242
    }
243
244
    /**
245
     * Get the string that should be used as an array key, for the attributive array
246
     * where the fields are stored for the current operation.
247
     *
248
     * The array key for the field should be:
249
     * - name (if the name is a string)
250
     * - name1_name2_name3 (if the name is an array)
251
     *
252
     * @param  array  $field  Field definition array.
253
     * @return string The string that should be used as array key.
254
     */
255
    protected function getFieldKey($field)
256
    {
257
        if (is_array($field['name'])) {
258
            return implode('_', $field['name']);
259
        }
260
261
        return $field['name'];
262
    }
263
}
264