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 — master ( b8bb45...33d313 )
by Cristian
15:15 queued 08:07
created

CrudColumn::dd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel;
4
5
/**
6
 * Adds fluent syntax to Backpack CRUD Columns.
7
 *
8
 * In addition to the existing:
9
 * - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
10
 *
11
 * Developers can also do:
12
 * - CRUD::column('price')->type('number');
13
 *
14
 * And if the developer uses CrudColumn as Column in their CrudController:
15
 * - Column::name('price')->type('number');
16
 */
17
class CrudColumn
18
{
19
    protected $attributes;
20
21
    public function __construct($name)
22
    {
23
        $column = $this->crud()->firstColumnWhere('name', $name);
24
25
        // if column exists
26
        if ((bool) $column) {
27
            // use all existing attributes
28
            $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

28
            $this->setAllAttributeValues(/** @scrutinizer ignore-type */ $column);
Loading history...
29
        } else {
30
            // it means we're creating the column now,
31
            // so at the very least set the name attribute
32
            $this->setAttributeValue('name', $name);
33
        }
34
35
        // guess all attributes that weren't explicitly defined
36
        $this->attributes = $this->crud()->makeSureColumnHasNeededAttributes($this->attributes);
37
38
        return $this->save();
39
    }
40
41
    public function crud()
42
    {
43
        return app()->make('crud');
44
    }
45
46
    /**
47
     * Create a CrudColumn object with the parameter as its name.
48
     *
49
     * @param  string $name Name of the column in the db, or model attribute.
50
     * @return CrudColumn
51
     */
52
    public static function name($name)
53
    {
54
        return new static($name);
55
    }
56
57
    /**
58
     * Remove the current column from the current operation.
59
     *
60
     * @return void
61
     */
62
    public function remove()
63
    {
64
        $this->crud()->removeColumn($this->attributes['name']);
65
66
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Backpack\CRUD\app\Library\CrudPanel\CrudColumn which is incompatible with the documented return type void.
Loading history...
67
    }
68
69
    /**
70
     * Remove an attribute from the column definition array.
71
     *
72
     * @param  string $attribute  Name of the attribute being removed
73
     * @return CrudColumn
74
     */
75
    public function forget($attribute)
76
    {
77
        $this->crud()->removeColumnAttribute($this->attributes['name'], $attribute);
78
79
        return $this;
80
    }
81
82
    /**
83
     * Move the current column after another column.
84
     *
85
     * @param  string $destinationColumn Name of the destination column.
86
     * @return CrudColumn
87
     */
88
    public function after($destinationColumn)
89
    {
90
        $this->crud()->removeColumn($this->attributes['name']);
91
        $this->crud()->addColumn($this->attributes)->afterColumn($destinationColumn);
92
93
        return $this;
94
    }
95
96
    /**
97
     * Move the current column before another column.
98
     *
99
     * @param  string $destinationColumn Name of the destination column.
100
     * @return CrudColumn
101
     */
102
    public function before($destinationColumn)
103
    {
104
        $this->crud()->removeColumn($this->attributes['name']);
105
        $this->crud()->addColumn($this->attributes)->beforeColumn($destinationColumn);
106
107
        return $this;
108
    }
109
110
    /**
111
     * Make the current column the first one in the columns list.
112
     *
113
     * @return CrudColumn
114
     */
115
    public function makeFirst()
116
    {
117
        $this->crud()->removeColumn($this->attributes['name']);
118
        $this->crud()->addColumn($this->attributes)->makeFirstColumn();
119
120
        return $this;
121
    }
122
123
    /**
124
     * Make the current column the last one in the columns list.
125
     *
126
     * @return CrudColumn
127
     */
128
    public function makeLast()
129
    {
130
        $this->crud()->removeColumn($this->attributes['name']);
131
        $this->crud()->addColumn($this->attributes);
132
133
        return $this;
134
    }
135
136
    // -----------------
137
    // DEBUGGING METHODS
138
    // -----------------
139
140
    /**
141
     * Dump the current object to the screen,
142
     * so that the developer can see its contents.
143
     *
144
     * @return CrudColumn
145
     */
146
    public function dump()
147
    {
148
        dump($this);
149
150
        return $this;
151
    }
152
153
    /**
154
     * Dump and die. Duumps the current object to the screen,
155
     * so that the developer can see its contents, then stops
156
     * the execution.
157
     *
158
     * @return CrudColumn
159
     */
160
    public function dd()
161
    {
162
        dd($this);
163
164
        return $this;
165
    }
166
167
    // ---------------
168
    // PRIVATE METHODS
169
    // ---------------
170
171
    /**
172
     * Set the value for a certain attribute on the CrudColumn object.
173
     *
174
     * @param string $attribute Name of the attribute.
175
     * @param string $value     Value of that attribute.
176
     */
177
    private function setAttributeValue($attribute, $value)
178
    {
179
        $this->attributes[$attribute] = $value;
180
    }
181
182
    /**
183
     * Replace all column attributes on the CrudColumn object
184
     * with the given array of attribute-value pairs.
185
     *
186
     * @param array $array Array of attributes and their values.
187
     */
188
    private function setAllAttributeValues($array)
189
    {
190
        $this->attributes = $array;
191
    }
192
193
    /**
194
     * Update the global CrudPanel object with the current column attributes.
195
     *
196
     * @return CrudColumn
197
     */
198
    private function save()
199
    {
200
        $key = $this->attributes['key'] ?? $this->attributes['name'];
201
202
        if ($this->crud()->hasColumnWhere('key', $key)) {
203
            $this->crud()->setColumnDetails($key, $this->attributes);
204
        } else {
205
            $this->crud()->addColumn($this->attributes);
206
        }
207
208
        return $this;
209
    }
210
211
    // -------------
212
    // MAGIC METHODS
213
    // -------------
214
215
    /**
216
     * If a developer calls a method that doesn't exist, assume they want:
217
     * - the CrudColumn object to have an attribute with that value;
218
     * - that column be updated inside the global CrudPanel object;.
219
     *
220
     * Eg: type('number') will set the "type" attribute to "number"
221
     *
222
     * @param  string $method     The method being called that doesn't exist.
223
     * @param  array $parameters  The arguments when that method was called.
224
     *
225
     * @return CrudColumn
226
     */
227
    public function __call($method, $parameters)
228
    {
229
        $this->setAttributeValue($method, $parameters[0]);
230
231
        return $this->save();
232
    }
233
}
234