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 — add-afterColumn()-alias ( 2d2061 )
by Pedro
14:29
created

CrudColumn::afterColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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
     * 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
     * Make the current column the first one in the columns list.
175
     *
176
     * @return CrudColumn
177
     */
178
    public function makeFirst()
179
    {
180
        $this->crud()->removeColumn($this->attributes['name']);
181
        $this->crud()->addColumn($this->attributes)->makeFirstColumn();
182
183
        return $this;
184
    }
185
186
    /**
187
     * Make the current column the last one in the columns list.
188
     *
189
     * @return CrudColumn
190
     */
191
    public function makeLast()
192
    {
193
        $this->crud()->removeColumn($this->attributes['name']);
194
        $this->crud()->addColumn($this->attributes);
195
196
        return $this;
197
    }
198
199
    // -----------------
200
    // DEBUGGING METHODS
201
    // -----------------
202
203
    /**
204
     * Dump the current object to the screen,
205
     * so that the developer can see its contents.
206
     *
207
     * @codeCoverageIgnore
208
     *
209
     * @return CrudColumn
210
     */
211
    public function dump()
212
    {
213
        dump($this);
214
215
        return $this;
216
    }
217
218
    /**
219
     * Dump and die. Dumps the current object to the screen,
220
     * so that the developer can see its contents, then stops
221
     * the execution.
222
     *
223
     * @codeCoverageIgnore
224
     *
225
     * @return CrudColumn
226
     */
227
    public function dd()
228
    {
229
        dd($this);
230
231
        return $this;
232
    }
233
234
    public function getAttributes()
235
    {
236
        return $this->attributes;
237
    }
238
239
    // ---------------
240
    // PRIVATE METHODS
241
    // ---------------
242
243
    /**
244
     * Set the value for a certain attribute on the CrudColumn object.
245
     *
246
     * @param  string  $attribute  Name of the attribute.
247
     * @param  mixed  $value  Value of that attribute.
248
     */
249
    private function setAttributeValue($attribute, $value)
250
    {
251
        $this->attributes[$attribute] = $value;
252
    }
253
254
    /**
255
     * Replace all column attributes on the CrudColumn object
256
     * with the given array of attribute-value pairs.
257
     *
258
     * @param  array  $array  Array of attributes and their values.
259
     */
260
    private function setAllAttributeValues($array)
261
    {
262
        $this->attributes = $array;
263
    }
264
265
    /**
266
     * Update the global CrudPanel object with the current column attributes.
267
     *
268
     * @return CrudColumn
269
     */
270
    private function save()
271
    {
272
        $key = $this->attributes['key'] ?? $this->attributes['name'];
273
274
        if ($this->crud()->hasColumnWhere('key', $key)) {
275
            $this->crud()->setColumnDetails($key, $this->attributes);
276
        } else {
277
            $this->crud()->addColumn($this->attributes);
278
            $this->attributes = $this->getFreshAttributes();
279
        }
280
281
        return $this;
282
    }
283
284
    /**
285
     * Get the fresh attributes for the current column.
286
     *
287
     * @return array
288
     */
289
    private function getFreshAttributes()
290
    {
291
        $key = isset($this->attributes['key']) ? 'key' : 'name';
292
        $search = $this->attributes['key'] ?? $this->attributes['name'];
293
294
        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...
295
    }
296
297
    // -------------
298
    // MAGIC METHODS
299
    // -------------
300
301
    /**
302
     * If a developer calls a method that doesn't exist, assume they want:
303
     * - the CrudColumn object to have an attribute with that value;
304
     * - that column be updated inside the global CrudPanel object;.
305
     *
306
     * Eg: type('number') will set the "type" attribute to "number"
307
     *
308
     * @param  string  $method  The method being called that doesn't exist.
309
     * @param  array  $parameters  The arguments when that method was called.
310
     * @return CrudColumn
311
     */
312
    public function __call($method, $parameters)
313
    {
314
        if (static::hasMacro($method)) {
315
            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

315
            return $this->/** @scrutinizer ignore-call */ macroCall($method, $parameters);
Loading history...
316
        }
317
318
        $this->setAttributeValue($method, $parameters[0]);
319
320
        return $this->save();
321
    }
322
}
323