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.
Passed
Push — merge-dev-to-master ( fdc6fe )
by
unknown
11:18
created

BaseColumnFilter::apply()   C

Complexity

Conditions 13
Paths 37

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 34
c 1
b 0
f 0
nc 37
nop 4
dl 0
loc 57
rs 6.6166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SleepingOwl\Admin\Display\Column\Filter;
4
5
use Closure;
6
use SleepingOwl\Admin\Traits\Assets;
7
use Illuminate\Database\Eloquent\Builder;
8
use KodiComponents\Support\HtmlAttributes;
9
use Illuminate\Contracts\Support\Arrayable;
10
use Illuminate\Contracts\Support\Renderable;
11
use SleepingOwl\Admin\Traits\SqlQueryOperators;
12
use SleepingOwl\Admin\Contracts\Display\ColumnInterface;
13
use SleepingOwl\Admin\Contracts\Display\ColumnMetaInterface;
14
use SleepingOwl\Admin\Contracts\Display\Extension\ColumnFilterInterface;
15
16
abstract class BaseColumnFilter implements Renderable, ColumnFilterInterface, Arrayable
17
{
18
    use SqlQueryOperators, HtmlAttributes, Assets, \SleepingOwl\Admin\Traits\Renderable;
19
20
    protected $view;
21
22
    /**
23
     * @var \Closure|null
24
     */
25
    protected $callback;
26
27
    /**
28
     * @var string|null
29
     */
30
    protected $columnName;
31
32
    /**
33
     * @var string|null
34
     */
35
    protected $columnRawName;
36
37
    public function __construct()
38
    {
39
        $this->initializePackage();
40
    }
41
42
    /**
43
     * Initialize column filter.
44
     */
45
    public function initialize()
46
    {
47
        $this->includePackage();
48
    }
49
50
    /**
51
     * @return null|string
52
     */
53
    public function getColumnName()
54
    {
55
        return $this->columnName;
56
    }
57
58
    /**
59
     * @param null|string $name
60
     *
61
     * @return $this
62
     */
63
    public function setColumnName($name)
64
    {
65
        $this->columnName = $name;
66
67
        return $this;
68
    }
69
70
    /**
71
     * @return null|string
72
     */
73
    public function getColumnRawName()
74
    {
75
        return $this->columnRawName;
76
    }
77
78
    /**
79
     * @param null|string $name
80
     *
81
     * @return $this
82
     */
83
    public function setColumnRawName($name)
84
    {
85
        $this->columnRawName = $name;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param mixed $value
92
     *
93
     * @return mixed
94
     */
95
    public function parseValue($value)
96
    {
97
        return $value;
98
    }
99
100
    /**
101
     * @return \Closure|null
102
     * @deprecated
103
     */
104
    public function getCallback()
105
    {
106
        return $this->callback;
107
    }
108
109
    /**
110
     * @param \Closure $callback
111
     * @return $this
112
     * @deprecated
113
     */
114
    public function setCallback(Closure $callback)
115
    {
116
        $this->callback = $callback;
117
118
        return $this;
119
    }
120
121
    /**
122
     * @param ColumnInterface $column
123
     * @param Builder $query
124
     * @param string $queryString
125
     * @param array|string $queryParams
126
     *
127
     * @return void
128
     */
129
    public function apply(ColumnInterface $column, Builder $query, $queryString, $queryParams)
130
    {
131
        $queryString = $this->parseValue($queryString);
132
133
        if (($metaInstance = $column->getMetaData()) instanceof ColumnMetaInterface) {
0 ignored issues
show
Bug introduced by
The method getMetaData() does not exist on SleepingOwl\Admin\Contra...Display\ColumnInterface. It seems like you code against a sub-type of said class. However, the method does not exist in SleepingOwl\Admin\Contra...ColumnEditableInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

133
        if (($metaInstance = $column->/** @scrutinizer ignore-call */ getMetaData()) instanceof ColumnMetaInterface) {
Loading history...
134
            if (method_exists($metaInstance, 'onFilterSearch')) {
135
                $metaInstance->onFilterSearch($column, $query, $queryString, $queryParams);
136
137
                return;
138
            }
139
        }
140
141
        if (is_callable($callback = $column->getFilterCallback())) {
0 ignored issues
show
Bug introduced by
The method getFilterCallback() does not exist on SleepingOwl\Admin\Contra...Display\ColumnInterface. It seems like you code against a sub-type of said class. However, the method does not exist in SleepingOwl\Admin\Contra...ColumnEditableInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

141
        if (is_callable($callback = $column->/** @scrutinizer ignore-call */ getFilterCallback())) {
Loading history...
142
            $callback($column, $query, $queryString, $queryParams);
143
144
            return;
145
        }
146
147
        if (is_callable($callback = $this->getCallback())) {
0 ignored issues
show
Deprecated Code introduced by
The function SleepingOwl\Admin\Displa...mnFilter::getCallback() has been deprecated. ( Ignorable by Annotation )

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

147
        if (is_callable($callback = /** @scrutinizer ignore-deprecated */ $this->getCallback())) {
Loading history...
148
            $callback($column, $query, $queryString, $queryParams);
149
150
            return;
151
        }
152
153
        if (empty($queryString) && strlen($queryString) == 0) {
154
            return;
155
        }
156
157
        if (is_null($name = $this->getColumnRawName())) {
158
            if (is_null($name = $this->getColumnName())) {
159
                $name = $column->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on SleepingOwl\Admin\Contra...Display\ColumnInterface. It seems like you code against a sub-type of SleepingOwl\Admin\Contra...Display\ColumnInterface such as SleepingOwl\Admin\Display\Column\NamedColumn or SleepingOwl\Admin\Display\Column\Editable\DateTime or SleepingOwl\Admin\Display\Column\Editable\Select or SleepingOwl\Admin\Display\Column\Editable\Textarea or SleepingOwl\Admin\Display\Column\Editable\Checkbox or SleepingOwl\Admin\Display\Column\Editable\Text. ( Ignorable by Annotation )

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

159
                /** @scrutinizer ignore-call */ 
160
                $name = $column->getName();
Loading history...
160
            }
161
        }
162
163
        if (strpos($name, '.') !== false && is_null($this->getColumnRawName())) {
164
            $parts = explode('.', $name);
165
            $fieldName = array_pop($parts);
166
            $relationName = implode('.', $parts);
167
            try {
168
                $relation = $query->getModel()->{$relationName}();
169
                $isMorphTo = $relation instanceof \Illuminate\Database\Eloquent\Relations\MorphTo;
170
            } catch (\Exception $e) {
171
                $isMorphTo = false;
172
            }
173
174
            if ($isMorphTo) {
175
                $query->whereHasMorph($relationName, '*', function ($q) use ($queryString, $fieldName) {
176
                    $this->buildQuery($q, $fieldName, $queryString);
177
                });
178
            } else {
179
                $query->whereHas($relationName, function ($q) use ($queryString, $fieldName) {
180
                    $this->buildQuery($q, $fieldName, $queryString);
181
                });
182
            }
183
            unset($relation);
184
        } else {
185
            $this->buildQuery($query, $name, $queryString);
186
        }
187
    }
188
189
    /**
190
     * Get the instance as an array.
191
     *
192
     * @return array
193
     */
194
    public function toArray()
195
    {
196
        return [
197
            'attributes' => $this->htmlAttributesToString(),
198
            'attributesArray' => $this->getHtmlAttributes(),
199
        ];
200
    }
201
}
202