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

Test Failed
Push — master ( 431455...2298dd )
by Cristian
40:32 queued 25:47
created

CrudFilter::label()   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 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel;
4
5
use Closure;
6
use Illuminate\Support\Str;
7
use Symfony\Component\HttpFoundation\ParameterBag;
8
9
class CrudFilter
10
{
11
    public $name; // the name of the filtered variable (db column name)
12
    public $type = 'select2'; // the name of the filter view that will be loaded
13
    public $key; //camelCased version of filter name to use in internal ids, js functions and css classes.
14
    public $label;
15
    public $placeholder;
16
    public $values;
17
    public $options;
18
    public $logic;
19
    public $fallbackLogic;
20
    public $currentValue;
21
    public $view;
22
    public $viewNamespace = 'crud::filters';
23
    public $applied = false;
24
25
    public function __construct($options, $values, $logic, $fallbackLogic)
26
    {
27
        // if filter exists
28
        if ($this->crud()->hasFilterWhere('name', $options['name'])) {
29
            $properties = get_object_vars($this->crud()->firstFilterWhere('name', $options['name']));
0 ignored issues
show
Bug introduced by
$this->crud()->firstFilt...ame', $options['name']) of type boolean is incompatible with the type object expected by parameter $object of get_object_vars(). ( Ignorable by Annotation )

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

29
            $properties = get_object_vars(/** @scrutinizer ignore-type */ $this->crud()->firstFilterWhere('name', $options['name']));
Loading history...
30
            foreach ($properties as $property => $value) {
31
                $this->{$property} = $value;
32
            }
33
        } else {
34
            // it means we're creating the filter now,
35
            $this->name = $options['name'];
36
            $this->key = Str::camel($options['name']);
37
            $this->type = $options['type'] ?? $this->type;
38
            $this->label = $options['label'] ?? $this->crud()->makeLabel($this->name);
39
            $this->viewNamespace = $options['view_namespace'] ?? $this->viewNamespace;
40
            $this->view = $this->type;
41
            $this->placeholder = $options['placeholder'] ?? '';
42
43
            $this->values = is_callable($values) ? $values() : $values;
44
            $this->options = $options;
45
            $this->logic = $logic;
46
            $this->fallbackLogic = $fallbackLogic;
47
        }
48
49
        if (\Request::has($this->name)) {
50
            $this->currentValue = \Request::input($this->name);
51
        }
52
    }
53
54
    /**
55
     * Check if the field is currently active. This happens when there's a GET parameter
56
     * in the current request with the same name as the name of the field.
57
     *
58
     * @return bool
59
     */
60
    public function isActive()
61
    {
62
        if (\Request::has($this->name)) {
63
            return true;
64
        }
65
66
        return false;
67
    }
68
69
    /**
70
     * Check if the filter has already had the apply method called on it.
71
     *
72
     * @return bool
73
     */
74
    public function wasApplied()
75
    {
76
        return $this->applied;
77
    }
78
79
    /**
80
     * Check if the filter has not had the apply method called on it yet.
81
     * This is the inverse of the wasApplied() method.
82
     *
83
     * @return bool
84
     */
85
    public function wasNotApplied()
86
    {
87
        return ! $this->applied;
88
    }
89
90
    /**
91
     * Run the filter logic, default logic and/or fallback logic so that from this point on
92
     * the CRUD query has its results filtered, according to the Request.
93
     *
94
     * @param  array $input The GET parameters for which the filter should be applied.
95
     * @return void
96
     */
97
    public function apply($input = null)
98
    {
99
        // mark the field as already applied
100
        $this->applied(true);
101
102
        if (is_array($input)) {
103
            $input = new ParameterBag($input);
104
        }
105
106
        $input = $input ?? new ParameterBag($this->crud()->getRequest()->all());
107
108
        if (! $input->has($this->name)) {
109
            // if fallback logic was supplied and is a closure
110
            if (is_callable($this->fallbackLogic)) {
111
                return ($this->fallbackLogic)();
112
            }
113
114
            return;
115
        }
116
117
        // if a closure was passed as "filterLogic"
118
        if (is_callable($this->logic)) {
119
            return ($this->logic)($input->get($this->name));
120
        } else {
121
            return $this->applyDefaultLogic($this->name, false);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->applyDefaultLogic($this->name, false) targeting Backpack\CRUD\app\Librar...er::applyDefaultLogic() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
false of type false is incompatible with the type string expected by parameter $operator of Backpack\CRUD\app\Librar...er::applyDefaultLogic(). ( Ignorable by Annotation )

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

121
            return $this->applyDefaultLogic($this->name, /** @scrutinizer ignore-type */ false);
Loading history...
122
        }
123
    }
124
125
    /**
126
     * Get the full path of the filter view, including the view namespace.
127
     *
128
     * @return string
129
     */
130
    public function getViewWithNamespace()
131
    {
132
        return $this->viewNamespace.'.'.$this->view;
133
    }
134
135
    // ---------------------
136
    // FLUENT SYNTAX METHODS
137
    // ---------------------
138
139
    /**
140
     * Create a CrudFilter object with the parameter as its name.
141
     *
142
     * @param  string $name Name of the column in the db, or model attribute.
143
     * @return CrudPanel
144
     */
145
    public static function name($name)
146
    {
147
        return new static(compact('name'), null, null, null);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new static(compac...me'), null, null, null) returns the type Backpack\CRUD\app\Library\CrudPanel\CrudFilter which is incompatible with the documented return type Backpack\CRUD\app\Library\CrudPanel\CrudPanel.
Loading history...
148
    }
149
150
    /**
151
     * Remove the current filter from the current operation.
152
     *
153
     * @return void
154
     */
155
    public function remove()
156
    {
157
        $this->crud()->removeFilter($this->name);
158
    }
159
160
    /**
161
     * Remove an attribute from the current filter definition array.
162
     *
163
     * @param  string $attribute Name of the attribute being removed.
164
     * @return CrudFilter
165
     */
166
    public function forget($attribute)
167
    {
168
        if (property_exists($this, $attribute)) {
169
            $this->{$attribute} = false;
170
        }
171
172
        if (isset($this->options[$attribute])) {
173
            unset($this->options[$attribute]);
174
        }
175
176
        $this->crud()->replaceFilter($this->name, $this);
177
178
        return $this;
179
    }
180
181
    /**
182
     * Remove an attribute from one field's definition array.
183
     * @param  string $field     The name of the field.
184
     * @param  string $attribute The name of the attribute being removed.
185
     */
186
    public function removeFilterAttribute($filter, $attribute)
0 ignored issues
show
Unused Code introduced by
The parameter $filter 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

186
    public function removeFilterAttribute(/** @scrutinizer ignore-unused */ $filter, $attribute)

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...
187
    {
188
        $fields = $this->fields();
0 ignored issues
show
Bug introduced by
The method fields() does not exist on Backpack\CRUD\app\Library\CrudPanel\CrudFilter. 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

188
        /** @scrutinizer ignore-call */ 
189
        $fields = $this->fields();
Loading history...
189
190
        unset($fields[$field][$attribute]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $field does not exist. Did you maybe mean $fields?
Loading history...
191
192
        $this->setOperationSetting('fields', $fields);
0 ignored issues
show
Bug introduced by
The method setOperationSetting() does not exist on Backpack\CRUD\app\Library\CrudPanel\CrudFilter. 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

192
        $this->/** @scrutinizer ignore-call */ 
193
               setOperationSetting('fields', $fields);
Loading history...
193
    }
194
195
    /**
196
     * Move the current filter after another filter.
197
     *
198
     * @param  string $destination Name of the destination filter.
199
     * @return CrudFilter
200
     */
201
    public function after($destination)
202
    {
203
        $this->crud()->moveFilter($this->name, 'after', $destination);
204
205
        return $this;
206
    }
207
208
    /**
209
     * Move the current field before another field.
210
     *
211
     * @param  string $destination Name of the destination field.
212
     * @return CrudFilter
213
     */
214
    public function before($destination)
215
    {
216
        $this->crud()->moveFilter($this->name, 'before', $destination);
217
218
        return $this;
219
    }
220
221
    /**
222
     * Make the current field the first one in the fields list.
223
     *
224
     * @return CrudPanel
225
     */
226
    public function makeFirst()
227
    {
228
        $this->crud()->moveFilter($this->name, 'before', $this->crud()->filters()->first()->name);
229
230
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Backpack\CRUD\app\Library\CrudPanel\CrudFilter which is incompatible with the documented return type Backpack\CRUD\app\Library\CrudPanel\CrudPanel.
Loading history...
231
    }
232
233
    /**
234
     * Make the current field the last one in the fields list.
235
     *
236
     * @return CrudPanel
237
     */
238
    public function makeLast()
239
    {
240
        $this->crud()->removeFilter($this->name);
241
        $this->crud()->addCrudFilter($this);
242
243
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Backpack\CRUD\app\Library\CrudPanel\CrudFilter which is incompatible with the documented return type Backpack\CRUD\app\Library\CrudPanel\CrudPanel.
Loading history...
244
    }
245
246
    // -----------------------
247
    // FILTER-SPECIFIC SETTERS
248
    // -----------------------
249
250
    /**
251
     * Set the type of the filter.
252
     *
253
     * @param  string $value Name of blade view that shows the field.
254
     * @return CrudFilter
255
     */
256
    public function type($value)
257
    {
258
        $this->type = $value;
259
        $this->view = $value;
260
261
        return $this->save();
262
    }
263
264
    /**
265
     * Set the label of the filter - the element that the end-user can see and click
266
     * to activate the filter or an input that will activate the filter.
267
     *
268
     * @param  string $value A name for this filter that the end-user will understand.
269
     * @return CrudFilter
270
     */
271
    public function label($value)
272
    {
273
        $this->label = $value;
274
275
        return $this->save();
276
    }
277
278
    /**
279
     * Set the values for the current filter, for the filters who need values.
280
     * For example, the dropdown, select2 and select2 filters let the user select
281
     * pre-determined values to filter with. This is how to set those values that will be picked up.
282
     *
283
     * @param  array|function $value Key-value array with values for the user to pick from, or a function which also return a Key-value array.
0 ignored issues
show
Bug introduced by
The type Backpack\CRUD\app\Library\CrudPanel\function was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
284
     * @return CrudFilter
285
     */
286
    public function values($value)
287
    {
288
        $this->values = (! is_string($value) && is_callable($value)) ? $value() : $value;
0 ignored issues
show
introduced by
The condition is_string($value) is always false.
Loading history...
289
290
        return $this->save();
291
    }
292
293
    /**
294
     * Set the values for the current filter, for the filters who need values. For example
295
     * the dropdown, select2 and select2 filters let the user select pre-determined
296
     * values to filter with.
297
     *
298
     * Alias of the values() method.
299
     *
300
     * @param  array|function $value Key-value array with values for the user to pick from, or a function which also return a Key-value array.
301
     * @return CrudFilter
302
     */
303
    public function options($value)
304
    {
305
        return $this->values($value);
306
    }
307
308
    /**
309
     * Set the blade view that will be used by the filter.
310
     * Should NOT include the namespace, that's defined separately using 'viewNamespace'.
311
     *
312
     * @param  string $value Path to the blade file, after the view namespace.
313
     * @return CrudFilter
314
     */
315
    public function view($value)
316
    {
317
        $this->view = $value;
318
319
        return $this->save();
320
    }
321
322
    /**
323
     * The path to the blade views directory where the filter file will be found. Ex: 'crud::filters'
324
     * Useful to load filters from a different package or directory.
325
     *
326
     * @param  string $value Blade path to the directory.
327
     * @return CrudFilter
328
     */
329
    public function viewNamespace($value)
330
    {
331
        $this->viewNamespace = $value;
332
333
        return $this->save();
334
    }
335
336
    /**
337
     * Define what happens when the filter is active, through a closure.
338
     *
339
     * @param  Closure $value Closure that will be called when Request has this name as GET parameter.
340
     * @return CrudFilter
341
     */
342
    public function logic($value)
343
    {
344
        $this->logic = $value;
345
346
        return $this->save();
347
    }
348
349
    /**
350
     * Define what happens when the filter is NOT active, through a closure.
351
     *
352
     * @param  Closure $value Closure that will be called when Request does NOT have this name as GET parameter.
353
     * @return CrudFilter
354
     */
355
    public function fallbackLogic($value)
356
    {
357
        $this->fallbackLogic = $value;
358
359
        return $this->save();
360
    }
361
362
    /**
363
     * Define if the filter has already been applied (logic or fallbackLogic called).
364
     *
365
     * @param  bool $value Whether the filter has been run.
366
     * @return CrudFilter
367
     */
368
    public function applied($value)
369
    {
370
        $this->applied = $value;
371
372
        return $this->save();
373
    }
374
375
    /**
376
     * Aliases of the logic() method.
377
     */
378
    public function whenActive($value)
379
    {
380
        return $this->logic($value);
381
    }
382
383
    public function ifActive($value)
384
    {
385
        return $this->logic($value);
386
    }
387
388
    /**
389
     * Alises of the fallbackLogic() method.
390
     */
391
    public function whenInactive($value)
392
    {
393
        return $this->fallbackLogic($value);
394
    }
395
396
    public function whenNotActive($value)
397
    {
398
        return $this->fallbackLogic($value);
399
    }
400
401
    public function ifInactive($value)
402
    {
403
        return $this->fallbackLogic($value);
404
    }
405
406
    public function ifNotActive($value)
407
    {
408
        return $this->fallbackLogic($value);
409
    }
410
411
    public function else($value)
412
    {
413
        return $this->fallbackLogic($value);
414
    }
415
416
    // ---------------
417
    // PRIVATE METHODS
418
    // ---------------
419
420
    private function crud()
421
    {
422
        return app()->make('crud');
423
    }
424
425
    /**
426
     * Set the value for a certain attribute on the CrudFilter object.
427
     *
428
     * @param string $attribute Name of the attribute.
429
     * @param string $value     Value of that attribute.
430
     */
431
    private function setOptionValue($attribute, $value)
432
    {
433
        $this->options[$attribute] = $value;
434
    }
435
436
    /**
437
     * Replace all field options on the CrudFilter object
438
     * with the given array of attribute-value pairs.
439
     *
440
     * @param array $array Array of options and their values.
441
     */
442
    private function setAllOptionsValues($array)
0 ignored issues
show
Unused Code introduced by
The method setAllOptionsValues() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
443
    {
444
        $this->options = $array;
445
    }
446
447
    /**
448
     * Update the global CrudPanel object with the current field options.
449
     *
450
     * @return CrudFilter
451
     */
452
    private function save()
453
    {
454
        $key = $this->name;
455
456
        if ($this->crud()->hasFilterWhere('name', $key)) {
457
            $this->crud()->modifyFilter($key, (array) $this);
458
        } else {
459
            $this->crud()->addCrudFilter($this);
460
        }
461
462
        return $this;
463
    }
464
465
    /**
466
     * @param string $name
467
     * @param string $operator
468
     * @param array  $input
469
     */
470
    private function applyDefaultLogic($name, $operator, $input = null)
471
    {
472
        $input = $input ?? $this->crud()->getRequest()->all();
473
474
        // if this filter is active (the URL has it as a GET parameter)
475
        switch ($operator) {
476
            // if no operator was passed, just use the equals operator
477
            case false:
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $operator of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
478
                $this->crud()->addClause('where', $name, $input[$name]);
479
                break;
480
481
            case 'scope':
482
                $this->crud()->addClause($operator);
483
                break;
484
485
            // TODO:
486
            // whereBetween
487
            // whereNotBetween
488
            // whereIn
489
            // whereNotIn
490
            // whereNull
491
            // whereNotNull
492
            // whereDate
493
            // whereMonth
494
            // whereDay
495
            // whereYear
496
            // whereColumn
497
            // like
498
499
            // sql comparison operators
500
            case '=':
501
            case '<=>':
502
            case '<>':
503
            case '!=':
504
            case '>':
505
            case '>=':
506
            case '<':
507
            case '<=':
508
                $this->crud()->addClause('where', $name, $operator, $input[$name]);
509
                break;
510
511
            default:
512
                abort(500, 'Unknown filter operator.');
513
                break;
514
        }
515
    }
516
517
    // -----------------
518
    // DEBUGGING METHODS
519
    // -----------------
520
521
    /**
522
     * Dump the current object to the screen,
523
     * so that the developer can see its contents.
524
     *
525
     * @return CrudFilter
526
     */
527
    public function dump()
528
    {
529
        dump($this);
530
531
        return $this;
532
    }
533
534
    /**
535
     * Dump and die. Duumps the current object to the screen,
536
     * so that the developer can see its contents, then stops
537
     * the execution.
538
     *
539
     * @return CrudFilter
540
     */
541
    public function dd()
542
    {
543
        dd($this);
544
545
        return $this;
546
    }
547
548
    // -------------
549
    // MAGIC METHODS
550
    // -------------
551
552
    /**
553
     * If a developer calls a method that doesn't exist, assume they want:
554
     * - $this->options['whatever'] to be set to that value;
555
     * - that filter be updated inside the global CrudPanel object;.
556
     *
557
     * Eg: type('number') will set the "type" attribute to "number"
558
     *
559
     * @param  string $method     The method being called that doesn't exist.
560
     * @param  array $parameters  The arguments when that method was called.
561
     *
562
     * @return CrudFilter
563
     */
564
    public function __call($method, $parameters)
565
    {
566
        $this->setOptionValue($method, $parameters[0]);
567
568
        return $this->save();
569
    }
570
}
571