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

Widget::dd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Backpack\CRUD\app\Library;
4
5
use Illuminate\Support\Fluent;
6
7
/**
8
 * Adds fluent syntax to Backpack Widgets.
9
 */
10
class Widget extends Fluent
11
{
12
    protected $attributes = [];
13
14
    public function __construct($attributes)
15
    {
16
        $this->attributes = $attributes;
17
18
        $this->save();
19
    }
20
21
    /**
22
     * Add a new widget to the widgets collection in the Laravel Service Container.
23
     * If a widget with the same name exists, it will update the attributes of that one
24
     * instead of creating a new one.
25
     *
26
     * @param string|array $attributes Either the name of the widget, or an array with the attributes the new widget should hold, including the name attribute.
27
     *
28
     * @return Widget
29
     */
30
    public static function add($attributes = null)
31
    {
32
        // make sure the widget has a name
33
        $attributes = is_string($attributes) ? ['name' => $attributes] : $attributes;
34
        $attributes['name'] = $attributes['name'] ?? 'widget_'.rand(1, 999999999);
35
36
        // if that widget name already exists in the widgets collection
37
        // then pick up all widget attributes from that entry
38
        // and overwrite them with the ones passed in $attributes
39
        if ($existingItem = self::collection()->firstWhere('name', $attributes['name'])) {
40
            $attributes = array_merge($existingItem->attributes, $attributes);
41
        }
42
43
        // set defaults for other mandatory attributes
44
        $attributes['section'] = $attributes['section'] ?? 'before_content';
45
        $attributes['type'] = $attributes['type'] ?? 'card';
46
47
        return new static($attributes);
48
    }
49
50
    /**
51
     * This method allows one to creat a widget without attaching it to any 'real'
52
     * widget section, by moving it to a 'hidden' section.
53
     *
54
     * It exists for one reason: so that developers can add widgets to a custom array, without
55
     * adding them to one of the widget sections.
56
     *
57
     * Ex: when developers need to pass multiple widgets as contents of the
58
     * div widget. But they don't want them added to the before_content of after_content
59
     * sections. So what they do is basically add them to a 'hidden' section, that nobody will ever see.
60
     *
61
     * @return Widget
62
     */
63
    public static function make($attributes = null)
64
    {
65
        $widget = static::add($attributes);
66
        $widget->section('hidden');
0 ignored issues
show
Bug introduced by
The method section() does not exist on Backpack\CRUD\app\Library\Widget. 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

66
        $widget->/** @scrutinizer ignore-call */ 
67
                 section('hidden');
Loading history...
67
68
        return $widget;
69
    }
70
71
    /**
72
     * Remove an attribute from the current definition array.
73
     *
74
     * @param  string $attribute Name of the attribute to forget (ex: class)
75
     * @return Widget
76
     */
77
    public function forget($attribute)
78
    {
79
        $this->offsetUnset($attribute);
80
81
        return $this;
82
    }
83
84
    // TODO: add ability to push a widget right after another widget
85
    public function after($destination)
0 ignored issues
show
Unused Code introduced by
The parameter $destination is not used and could be removed. ( Ignorable by Annotation )

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

85
    public function after(/** @scrutinizer ignore-unused */ $destination)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87
    }
88
89
    // TODO: add ability to push a widget right before another widget
90
    public function before($destionation)
0 ignored issues
show
Unused Code introduced by
The parameter $destionation is not used and could be removed. ( Ignorable by Annotation )

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

90
    public function before(/** @scrutinizer ignore-unused */ $destionation)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
    {
92
    }
93
94
    /**
95
     * Make this widget the first one in its section.
96
     *
97
     * @return Widget
98
     */
99
    public function makeFirst()
100
    {
101
        $this->collection()->pull($this->name);
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Backpack\CRUD\app\Library\Widget. Since you implemented __get, consider adding a @property annotation.
Loading history...
102
        $this->collection()->prepend($this);
103
104
        return $this;
105
    }
106
107
    /**
108
     * Make this widget the last one in its section.
109
     *
110
     * @return Widget
111
     */
112
    public function makeLast()
113
    {
114
        $this->collection()->pull($this->name);
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Backpack\CRUD\app\Library\Widget. Since you implemented __get, consider adding a @property annotation.
Loading history...
115
        $this->collection()->push($this);
116
117
        return $this;
118
    }
119
120
    // -------
121
    // ALIASES
122
    // -------
123
    // Aka convenience methods.
124
    // These method just call other methods.
125
126
    // Alias of add()
127
    public static function name(...$args)
128
    {
129
        return static::add(...$args);
0 ignored issues
show
Bug introduced by
$args is expanded, but the parameter $attributes of Backpack\CRUD\app\Library\Widget::add() does not expect variable arguments. ( Ignorable by Annotation )

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

129
        return static::add(/** @scrutinizer ignore-type */ ...$args);
Loading history...
130
    }
131
132
    // Alias of section()
133
    public function to(...$args)
134
    {
135
        return $this->section(...$args);
136
    }
137
138
    // Alias of section()
139
    public function group(...$args)
140
    {
141
        return $this->section(...$args);
142
    }
143
144
    // Alias of viewNamespace()
145
    public function from(...$args)
146
    {
147
        return $this->viewNamespace(...$args);
0 ignored issues
show
Bug introduced by
The method viewNamespace() does not exist on Backpack\CRUD\app\Library\Widget. 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

147
        return $this->/** @scrutinizer ignore-call */ viewNamespace(...$args);
Loading history...
148
    }
149
150
    // ------------------
151
    // COLLECTION METHODS
152
    // ------------------
153
    // Manipulate the global widget collection.
154
155
    public static function collection()
156
    {
157
        return app('widgets');
158
    }
159
160
    /**
161
     * Remove the widget from its section.
162
     *
163
     * @return Widget
164
     */
165
    public function remove()
166
    {
167
        $this->collection()->pull($this->name);
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Backpack\CRUD\app\Library\Widget. Since you implemented __get, consider adding a @property annotation.
Loading history...
168
169
        return $this;
170
    }
171
172
    /**
173
     * This alias of remove() exists for one reason: so that developers can add
174
     * widgets to a custom array, instead of adding them to one of the widget
175
     * sections. Ex: when developers need to pass multiple widgets as contents of the
176
     * div widget. But they don't want them added to the before_content of after_content
177
     * sections. So what they do is basically add them to a section, then remove them.
178
     * What's left is the widget itself, but without being attached to any section.
179
     *
180
     * @return Widget
181
     */
182
    public function onlyHere(...$args)
183
    {
184
        return $this->remove(...$args);
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Library\Widget::remove() has too many arguments starting with $args. ( Ignorable by Annotation )

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

184
        return $this->/** @scrutinizer ignore-call */ remove(...$args);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
185
    }
186
187
    // ---------------
188
    // PRIVATE METHODS
189
    // ---------------
190
191
    /**
192
     * Update the global CrudPanel object with the current widget attributes.
193
     *
194
     * @return Widget
195
     */
196
    private function save()
197
    {
198
        $itemExists = $this->collection()->contains('name', $this->attributes['name']);
199
200
        if (! $itemExists) {
201
            $this->collection()->put($this->attributes['name'], $this);
202
        } else {
203
            $this->collection()[$this->name] = $this;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Backpack\CRUD\app\Library\Widget. Since you implemented __get, consider adding a @property annotation.
Loading history...
204
        }
205
206
        return $this;
207
    }
208
209
    // -----------------
210
    // DEBUGGING METHODS
211
    // -----------------
212
213
    /**
214
     * Dump the current object to the screen,
215
     * so that the developer can see its contents.
216
     *
217
     * @return Widget
218
     */
219
    public function dump()
220
    {
221
        dump($this);
222
223
        return $this;
224
    }
225
226
    /**
227
     * Dump and die. Duumps the current object to the screen,
228
     * so that the developer can see its contents, then stops
229
     * the execution.
230
     *
231
     * @return Widget
232
     */
233
    public function dd()
234
    {
235
        dd($this);
236
237
        return $this;
238
    }
239
240
    // -------------
241
    // MAGIC METHODS
242
    // -------------
243
244
    /**
245
     * Any call to a non-existing method on this class will be assumed to be
246
     * an attribute that the developer wants to add to that particular widget.
247
     *
248
     * Eg: class('something') will set the "class" attribute to "something"
249
     *
250
     * @param  string $method     The method being called that doesn't exist.
251
     * @param  array $parameters  The arguments when that method was called.
252
     *
253
     * @return Widget
254
     */
255
    public function __call($method, $parameters)
256
    {
257
        $this->attributes[$method] = count($parameters) > 0 ? $parameters[0] : true;
258
259
        return $this->save();
260
    }
261
}
262