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 — main (#5611)
by Pedro
30:19 queued 15:46
created

CrudColumn::subfields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel;
4
5
use Backpack\CRUD\app\Library\CrudPanel\Traits\Support\MacroableWithAttributes;
6
use Illuminate\Support\Traits\Conditionable;
7
8
/**
9
 * Adds fluent syntax to Backpack CRUD Columns.
10
 *
11
 * In addition to the existing:
12
 * - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
13
 *
14
 * Developers can also do:
15
 * - CRUD::column('price')->type('number');
16
 *
17
 * And if the developer uses CrudColumn as Column in their CrudController:
18
 * - Column::name('price')->type('number');
19
 *
20
 * @method self type(string $value)
21
 * @method self label(string $value)
22
 * @method self searchLogic(mixed $value)
23
 * @method self orderLogic(callable $value)
24
 * @method self orderable(bool $value)
25
 * @method self wrapper(array $value)
26
 * @method self visibleInTable(bool $value)
27
 * @method self visibleInModal(bool $value)
28
 * @method self visibleInExport(bool $value)
29
 * @method self visibleInShow(bool $value)
30
 * @method self priority(int $value)
31
 * @method self key(string $value)
32
 * @method self upload(bool $value)
33
 * @method self linkTo(string $routeName, ?array $parameters = [])
34
 */
35
class CrudColumn
36
{
37
    use Conditionable;
38
    use MacroableWithAttributes;
0 ignored issues
show
Bug introduced by
The trait Backpack\CRUD\app\Librar...MacroableWithAttributes requires the property $name which is not provided by Backpack\CRUD\app\Library\CrudPanel\CrudColumn.
Loading history...
39
40
    protected $attributes;
41
42
    public function __construct($nameOrDefinitionArray)
43
    {
44
        if (is_array($nameOrDefinitionArray)) {
45
            $column = $this->crud()->addAndReturnColumn($nameOrDefinitionArray);
46
            $name = $column->getAttributes()['name'];
47
        } else {
48
            $name = $nameOrDefinitionArray;
49
        }
50
51
        $column = $this->crud()->firstColumnWhere('name', $name);
52
53
        // if column exists
54
        if ((bool) $column) {
55
            // use all existing attributes
56
            $this->setAllAttributeValues($column);
0 ignored issues
show
Bug introduced by
$column of type boolean is incompatible with the type array expected by parameter $array of Backpack\CRUD\app\Librar...setAllAttributeValues(). ( Ignorable by Annotation )

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

56
            $this->setAllAttributeValues(/** @scrutinizer ignore-type */ $column);
Loading history...
57
        } else {
58
            // it means we're creating the column now,
59
            // so at the very least set the name attribute
60
            $this->setAttributeValue('name', $name);
61
        }
62
63
        $this->save();
64
    }
65
66
    public function crud()
67
    {
68
        return app()->make('crud');
69
    }
70
71
    /**
72
     * Create a CrudColumn object with the parameter as its name.
73
     *
74
     * @param  string  $name  Name of the column in the db, or model attribute.
75
     * @return CrudColumn
76
     */
77
    public static function name($name)
78
    {
79
        return new static($name);
80
    }
81
82
    /**
83
     * Change the CrudColumn key.
84
     *
85
     * @param  string  $key  New key for the column
86
     * @return CrudColumn
87
     */
88
    public function key(string $key)
89
    {
90
        if (! isset($this->attributes['name'])) {
91
            abort(500, 'Column name must be defined before changing the key.');
92
        }
93
94
        $columns = $this->crud()->columns();
95
96
        $searchKey = $this->attributes['key'];
97
        $column = $this->attributes;
98
99
        if (isset($columns[$searchKey])) {
100
            unset($columns[$searchKey]);
101
            $column['key'] = $key;
102
        }
103
        $this->attributes = $column;
104
        $this->crud()->setOperationSetting('columns', array_merge($columns, [$key => $column]));
105
106
        return $this;
107
    }
108
109
    /**
110
     * When subfields are defined, pass them through the guessing function
111
     * so that they have label, relationship attributes, etc.
112
     *
113
     * @param  array  $subfields  Subfield definition array
114
     * @return self
115
     */
116
    public function subfields($subfields)
117
    {
118
        $callAttributeMacro = ! isset($this->attributes['subfields']);
119
        $this->attributes['subfields'] = $subfields;
120
        $this->attributes = $this->crud()->makeSureColumnHasNeededAttributes($this->attributes);
121
122
        if ($callAttributeMacro) {
123
            $this->callRegisteredAttributeMacros();
124
        }
125
126
        return $this->save();
127
    }
128
129
    /**
130
     * Remove the current column from the current operation.
131
     *
132
     * @return void
133
     */
134
    public function remove()
135
    {
136
        $this->crud()->removeColumn($this->attributes['name']);
137
    }
138
139
    /**
140
     * Remove an attribute from the column definition array.
141
     *
142
     * @param  string  $attribute  Name of the attribute being removed
143
     * @return CrudColumn
144
     */
145
    public function forget($attribute)
146
    {
147
        $this->crud()->removeColumnAttribute($this->attributes['name'], $attribute);
148
149
        return $this;
150
    }
151
152
    /**
153
     * Move the current column after another column.
154
     *
155
     * @param  string  $destinationColumn  Name of the destination column.
156
     * @return CrudColumn
157
     */
158
    public function after($destinationColumn)
159
    {
160
        $this->crud()->removeColumn($this->attributes['name']);
161
        $this->crud()->addColumn($this->attributes)->afterColumn($destinationColumn);
162
163
        return $this;
164
    }
165
166
    /** Alias of after() */
167
    public function afterColumn(string $destinationColumn)
168
    {
169
        $this->after($destinationColumn);
170
    }
171
172
    /**
173
     * Move the current column before another column.
174
     *
175
     * @param  string  $destinationColumn  Name of the destination column.
176
     * @return CrudColumn
177
     */
178
    public function before($destinationColumn)
179
    {
180
        $this->crud()->removeColumn($this->attributes['name']);
181
        $this->crud()->addColumn($this->attributes)->beforeColumn($destinationColumn);
182
183
        return $this;
184
    }
185
186
    public function upload($upload = true)
187
    {
188
        $this->attributes['upload'] = $upload;
189
190
        return $this->save();
191
    }
192
193
    /**
194
     * Make the current column the first one in the columns list.
195
     *
196
     * @return CrudColumn
197
     */
198
    public function makeFirst()
199
    {
200
        $this->crud()->removeColumn($this->attributes['name']);
201
        $this->crud()->addColumn($this->attributes)->makeFirstColumn();
202
203
        return $this;
204
    }
205
206
    /**
207
     * Make the current column the last one in the columns list.
208
     *
209
     * @return CrudColumn
210
     */
211
    public function makeLast()
212
    {
213
        $this->crud()->removeColumn($this->attributes['name']);
214
        $this->crud()->addColumn($this->attributes);
215
216
        return $this;
217
    }
218
219
    // -----------------
220
    // DEBUGGING METHODS
221
    // -----------------
222
223
    /**
224
     * Dump the current object to the screen,
225
     * so that the developer can see its contents.
226
     *
227
     * @codeCoverageIgnore
228
     *
229
     * @return CrudColumn
230
     */
231
    public function dump()
232
    {
233
        dump($this);
234
235
        return $this;
236
    }
237
238
    /**
239
     * Dump and die. Dumps the current object to the screen,
240
     * so that the developer can see its contents, then stops
241
     * the execution.
242
     *
243
     * @codeCoverageIgnore
244
     *
245
     * @return CrudColumn
246
     */
247
    public function dd()
248
    {
249
        dd($this);
250
251
        return $this;
252
    }
253
254
    public function getAttributes()
255
    {
256
        return $this->attributes;
257
    }
258
259
    // ---------------
260
    // PRIVATE METHODS
261
    // ---------------
262
263
    /**
264
     * Set the value for a certain attribute on the CrudColumn object.
265
     *
266
     * @param  string  $attribute  Name of the attribute.
267
     * @param  mixed  $value  Value of that attribute.
268
     */
269
    private function setAttributeValue($attribute, $value)
270
    {
271
        $this->attributes[$attribute] = $value;
272
    }
273
274
    /**
275
     * Replace all column attributes on the CrudColumn object
276
     * with the given array of attribute-value pairs.
277
     *
278
     * @param  array  $array  Array of attributes and their values.
279
     */
280
    private function setAllAttributeValues($array)
281
    {
282
        $this->attributes = $array;
283
    }
284
285
    /**
286
     * Update the global CrudPanel object with the current column attributes.
287
     *
288
     * @return CrudColumn
289
     */
290
    private function save()
291
    {
292
        $key = $this->attributes['key'] ?? $this->attributes['name'];
293
294
        if ($this->crud()->hasColumnWhere('key', $key)) {
295
            $this->crud()->setColumnDetails($key, $this->attributes);
296
        } else {
297
            $this->crud()->addColumn($this->attributes);
298
            $this->attributes = $this->getFreshAttributes();
299
        }
300
301
        return $this;
302
    }
303
304
    /**
305
     * Get the fresh attributes for the current column.
306
     *
307
     * @return array
308
     */
309
    private function getFreshAttributes()
310
    {
311
        $key = isset($this->attributes['key']) ? 'key' : 'name';
312
        $search = $this->attributes['key'] ?? $this->attributes['name'];
313
314
        return $this->crud()->firstColumnWhere($key, $search);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->crud()->fi...umnWhere($key, $search) returns the type boolean which is incompatible with the documented return type array.
Loading history...
315
    }
316
317
    // -------------
318
    // MAGIC METHODS
319
    // -------------
320
321
    /**
322
     * If a developer calls a method that doesn't exist, assume they want:
323
     * - the CrudColumn object to have an attribute with that value;
324
     * - that column be updated inside the global CrudPanel object;.
325
     *
326
     * Eg: type('number') will set the "type" attribute to "number"
327
     *
328
     * @param  string  $method  The method being called that doesn't exist.
329
     * @param  array  $parameters  The arguments when that method was called.
330
     * @return CrudColumn
331
     */
332
    public function __call($method, $parameters)
333
    {
334
        if (static::hasMacro($method)) {
335
            return $this->macroCall($method, $parameters);
0 ignored issues
show
Bug introduced by
The method macroCall() does not exist on Backpack\CRUD\app\Library\CrudPanel\CrudColumn. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

335
            return $this->/** @scrutinizer ignore-call */ macroCall($method, $parameters);
Loading history...
336
        }
337
338
        $this->setAttributeValue($method, $parameters[0]);
339
340
        return $this->save();
341
    }
342
}
343