GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b864c9...ed9166 )
by butschster
12:41
created

Select::apply()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 5
nop 5
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Display\Column\Filter;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Select extends BaseColumnFilter
8
{
9
    use \SleepingOwl\Admin\Traits\SelectOptionsFromModel;
10
11
    /**
12
     * @var string
13
     */
14
    protected $view = 'column.filter.select';
15
16
    /**
17
     * @var Model
18
     */
19
    protected $model;
20
21
    /**
22
     * @var array
23
     */
24
    protected $options = [];
25
26
    /**
27
     * @var string
28
     */
29
    protected $placeholder;
30
31
    /**
32
     * @var bool
33
     */
34
    protected $multiple = false;
35
36
    /**
37
     * @var bool
38
     */
39
    protected $sortable = true;
40
41
    /**
42
     * @param array|Model|string|null $options
43
     * @param string|null $title
44
     */
45
    public function __construct($options = null, $title = null)
46
    {
47
        parent::__construct();
48
49 View Code Duplication
        if (is_array($options)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            $this->setOptions($options);
51
        } elseif (($options instanceof Model) or is_string($options)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
52
            $this->setModelForOptions($options);
53
        }
54
55
        if (! is_null($title)) {
56
            $this->setDisplay($title);
57
        }
58
    }
59
60
    /**
61
     * @return $this
62
     */
63
    public function multiple()
64
    {
65
        $this->multiple = true;
66
67
        return $this;
68
    }
69
70
    public function initialize()
71
    {
72
        parent::initialize();
73
74
        $this->setHtmlAttribute('class', 'form-control input-select column-filter');
75
        $this->setHtmlAttribute('data-type', 'select');
76
77
        if ($this->multiple) {
78
            $this->setHtmlAttribute('multiple', 'multiple');
79
80
            if (! in_array($this->operator, ['in', 'not_in'])) {
81
                $this->setOperator('in');
82
            }
83
        } else {
84
            $this->setHtmlAttribute('placeholder', $this->getPlaceholder());
85
        }
86
    }
87
88
    /**
89
     * @param bool $sortable
90
     *
91
     * @return $this
92
     */
93
    public function setSortable($sortable)
94
    {
95
        $this->sortable = (bool) $sortable;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    public function isSortable()
104
    {
105
        return $this->sortable;
106
    }
107
108
    /**
109
     * @param Model|string $model
110
     *
111
     * @deprecated use setModelForOptions
112
     * @return $this
113
     */
114
    public function setModel($model)
115
    {
116
        return $this->setModelForOptions($model);
117
    }
118
119
    /**
120
     * @return array
121
     */
122 View Code Duplication
    public function getOptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        if (! is_null($this->getModelForOptions()) and ! is_null($this->getDisplay())) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
125
            $this->setOptions(
126
                $this->loadOptions()
127
            );
128
        }
129
130
        $options = $this->options;
131
        if ($this->isSortable()) {
132
            asort($options);
133
        }
134
135
        return $options;
136
    }
137
138
    /**
139
     * @param array $options
140
     *
141
     * @return $this
142
     */
143
    public function setOptions(array $options)
144
    {
145
        $this->options = $options;
146
147
        return $this;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getPlaceholder()
154
    {
155
        return $this->placeholder;
156
    }
157
158
    /**
159
     * @param string $placeholder
160
     *
161
     * @return $this
162
     */
163
    public function setPlaceholder($placeholder)
164
    {
165
        $this->placeholder = $placeholder;
166
167
        return $this;
168
    }
169
170
    /**
171
     * @return array
172
     */
173
    public function toArray()
174
    {
175
        return parent::toArray() + [
176
            'options'     => $this->getOptions(),
177
        ];
178
    }
179
180
    /**
181
     * @param mixed $selected
182
     *
183
     * @return array
184
     */
185
    public function parseValue($selected)
186
    {
187
        if (is_string($selected) && strpos($selected, ',') !== false) {
188
            return explode(',', $selected);
189
        }
190
191
        return $selected;
192
    }
193
}
194