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
Push — main ( 8a06a8...ac3507 )
by Pedro
21:33 queued 07:25
created

CrudColumn::key()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 19
rs 9.9
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.', ['developer-error-exception']);
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
     * Remove the current column from the current operation.
111
     *
112
     * @return void
113
     */
114
    public function remove()
115
    {
116
        $this->crud()->removeColumn($this->attributes['name']);
117
    }
118
119
    /**
120
     * Remove an attribute from the column definition array.
121
     *
122
     * @param  string  $attribute  Name of the attribute being removed
123
     * @return CrudColumn
124
     */
125
    public function forget($attribute)
126
    {
127
        $this->crud()->removeColumnAttribute($this->attributes['name'], $attribute);
128
129
        return $this;
130
    }
131
132
    /**
133
     * Move the current column after another column.
134
     *
135
     * @param  string  $destinationColumn  Name of the destination column.
136
     * @return CrudColumn
137
     */
138
    public function after($destinationColumn)
139
    {
140
        $this->crud()->removeColumn($this->attributes['name']);
141
        $this->crud()->addColumn($this->attributes)->afterColumn($destinationColumn);
142
143
        return $this;
144
    }
145
146
    /** Alias of after() */
147
    public function afterColumn(string $destinationColumn)
148
    {
149
        $this->after($destinationColumn);
150
    }
151
152
    /**
153
     * Move the current column before another column.
154
     *
155
     * @param  string  $destinationColumn  Name of the destination column.
156
     * @return CrudColumn
157
     */
158
    public function before($destinationColumn)
159
    {
160
        $this->crud()->removeColumn($this->attributes['name']);
161
        $this->crud()->addColumn($this->attributes)->beforeColumn($destinationColumn);
162
163
        return $this;
164
    }
165
166
    public function upload($upload = true)
167
    {
168
        $this->attributes['upload'] = $upload;
169
170
        return $this->save();
171
    }
172
173
    /**
174
     * When subfields are defined, pass them through the guessing function
175
     * so that they have label, relationship attributes, etc.
176
     *
177
     * @param  array  $subfields  Subfield definition array
178
     * @return self
179
     */
180
    public function subfields($subfields)
181
    {
182
        $callAttributeMacro = ! isset($this->attributes['subfields']);
183
        $this->attributes['subfields'] = $subfields;
184
        $this->attributes = $this->crud()->makeSureColumnHasNeededAttributes($this->attributes);
185
        if ($callAttributeMacro) {
186
            $this->callRegisteredAttributeMacros();
187
        }
188
189
        return $this->save();
190
    }
191
192
    /**
193
     * Make the current column the first one in the columns list.
194
     *
195
     * @return CrudColumn
196
     */
197
    public function makeFirst()
198
    {
199
        $this->crud()->removeColumn($this->attributes['name']);
200
        $this->crud()->addColumn($this->attributes)->makeFirstColumn();
201
202
        return $this;
203
    }
204
205
    /**
206
     * Make the current column the last one in the columns list.
207
     *
208
     * @return CrudColumn
209
     */
210
    public function makeLast()
211
    {
212
        $this->crud()->removeColumn($this->attributes['name']);
213
        $this->crud()->addColumn($this->attributes);
214
215
        return $this;
216
    }
217
218
    // -----------------
219
    // DEBUGGING METHODS
220
    // -----------------
221
222
    /**
223
     * Dump the current object to the screen,
224
     * so that the developer can see its contents.
225
     *
226
     * @codeCoverageIgnore
227
     *
228
     * @return CrudColumn
229
     */
230
    public function dump()
231
    {
232
        dump($this);
233
234
        return $this;
235
    }
236
237
    /**
238
     * Dump and die. Dumps the current object to the screen,
239
     * so that the developer can see its contents, then stops
240
     * the execution.
241
     *
242
     * @codeCoverageIgnore
243
     *
244
     * @return CrudColumn
245
     */
246
    public function dd()
247
    {
248
        dd($this);
249
250
        return $this;
251
    }
252
253
    public function getAttributes()
254
    {
255
        return $this->attributes;
256
    }
257
258
    // ---------------
259
    // PRIVATE METHODS
260
    // ---------------
261
262
    /**
263
     * Set the value for a certain attribute on the CrudColumn object.
264
     *
265
     * @param  string  $attribute  Name of the attribute.
266
     * @param  mixed  $value  Value of that attribute.
267
     */
268
    private function setAttributeValue($attribute, $value)
269
    {
270
        $this->attributes[$attribute] = $value;
271
    }
272
273
    /**
274
     * Replace all column attributes on the CrudColumn object
275
     * with the given array of attribute-value pairs.
276
     *
277
     * @param  array  $array  Array of attributes and their values.
278
     */
279
    private function setAllAttributeValues($array)
280
    {
281
        $this->attributes = $array;
282
    }
283
284
    /**
285
     * Update the global CrudPanel object with the current column attributes.
286
     *
287
     * @return CrudColumn
288
     */
289
    private function save()
290
    {
291
        $key = $this->attributes['key'] ?? $this->attributes['name'];
292
293
        if ($this->crud()->hasColumnWhere('key', $key)) {
294
            $this->crud()->setColumnDetails($key, $this->attributes);
295
        } else {
296
            $this->crud()->addColumn($this->attributes);
297
            $this->attributes = $this->getFreshAttributes();
298
        }
299
300
        return $this;
301
    }
302
303
    /**
304
     * Get the fresh attributes for the current column.
305
     *
306
     * @return array
307
     */
308
    private function getFreshAttributes()
309
    {
310
        $key = isset($this->attributes['key']) ? 'key' : 'name';
311
        $search = $this->attributes['key'] ?? $this->attributes['name'];
312
313
        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...
314
    }
315
316
    // -------------
317
    // MAGIC METHODS
318
    // -------------
319
320
    /**
321
     * If a developer calls a method that doesn't exist, assume they want:
322
     * - the CrudColumn object to have an attribute with that value;
323
     * - that column be updated inside the global CrudPanel object;.
324
     *
325
     * Eg: type('number') will set the "type" attribute to "number"
326
     *
327
     * @param  string  $method  The method being called that doesn't exist.
328
     * @param  array  $parameters  The arguments when that method was called.
329
     * @return CrudColumn
330
     */
331
    public function __call($method, $parameters)
332
    {
333
        if (static::hasMacro($method)) {
334
            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

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