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 — analysis-8bGQN2 ( 8fcae0 )
by butschster
10:20 queued 32s
created

DisplayTable   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 268
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 77
c 4
b 1
f 0
dl 0
loc 268
rs 10
wmc 30

16 Methods

Rating   Name   Duplication   Size   Complexity  
A paginate() 0 6 1
A setParameters() 0 5 1
A disablePagination() 0 5 1
A toArray() 0 15 1
A initialize() 0 11 2
A getParameters() 0 3 1
A setNewEntryButtonText() 0 5 1
A usePagination() 0 3 1
A setParameter() 0 5 1
A getNewEntryButtonText() 0 7 2
A __construct() 0 7 1
A modifyQuery() 0 3 1
A getCollection() 0 17 4
A setCollection() 0 3 1
B applySearch() 0 33 9
A applyOffset() 0 10 2
1
<?php
2
3
namespace SleepingOwl\Admin\Display;
4
5
use Exception;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Request;
8
use Illuminate\Database\Eloquent\Builder;
9
use SleepingOwl\Admin\Traits\PanelControl;
10
use Illuminate\Pagination\LengthAwarePaginator;
11
use SleepingOwl\Admin\Display\Extension\Columns;
12
use SleepingOwl\Admin\Display\Extension\ColumnsTotal;
13
use SleepingOwl\Admin\Display\Extension\ColumnFilters;
14
use SleepingOwl\Admin\Contracts\Display\ColumnInterface;
15
use SleepingOwl\Admin\Contracts\Display\Extension\ColumnFilterInterface;
16
17
/**
18
 * Class DisplayTable.
19
 *
20
 * @method Columns getColumns()
21
 * @method $this setColumns(ColumnInterface|ColumnInterface[] $column)
22
 *
23
 * @method ColumnFilters getColumnFilters()
24
 * @method $this setColumnFilters(ColumnFilterInterface|ColumnFilterInterface[] $filters = null, ...$filters)
25
 */
26
class DisplayTable extends Display
27
{
28
    use PanelControl;
29
30
    /**
31
     * @var string
32
     */
33
    protected $view = 'display.table';
34
35
    /**
36
     * @var array
37
     */
38
    protected $parameters = [];
39
40
    /**
41
     * @var int|null
42
     */
43
    protected $paginate = 25;
44
45
    /**
46
     * @var string
47
     */
48
    protected $pageName = 'page';
49
50
    /**
51
     * @var Collection
52
     */
53
    protected $collection;
54
55
    /**
56
     * @var string|null
57
     */
58
    protected $newEntryButtonText;
59
60
    /**
61
     * Display constructor.
62
     */
63
    public function __construct()
64
    {
65
        parent::__construct();
66
67
        $this->extend('columns', new Columns());
68
        $this->extend('column_filters', new ColumnFilters());
69
        $this->extend('columns_total', new ColumnsTotal());
70
    }
71
72
    /**
73
     * @throws \Exception
74
     */
75
    public function initialize()
76
    {
77
        parent::initialize();
78
79
        if ($this->getModelConfiguration()->isRestorableModel()) {
80
            $this->setApply(function (Builder $q) {
81
                return $q->withTrashed();
82
            });
83
        }
84
85
        $this->setHtmlAttribute('class', 'table table-striped');
86
    }
87
88
    /**
89
     * @return array|\Illuminate\Contracts\Translation\Translator|null|string
90
     */
91
    public function getNewEntryButtonText()
92
    {
93
        if (is_null($this->newEntryButtonText)) {
94
            $this->newEntryButtonText = trans('sleeping_owl::lang.table.new-entry');
95
        }
96
97
        return $this->newEntryButtonText;
98
    }
99
100
    /**
101
     * @param string $newEntryButtonText
102
     *
103
     * @return $this
104
     */
105
    public function setNewEntryButtonText($newEntryButtonText)
106
    {
107
        $this->newEntryButtonText = $newEntryButtonText;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    public function getParameters()
116
    {
117
        return $this->parameters;
118
    }
119
120
    /**
121
     * @param array $parameters
122
     *
123
     * @return $this
124
     */
125
    public function setParameters($parameters)
126
    {
127
        $this->parameters = $parameters;
128
129
        return $this;
130
    }
131
132
    /**
133
     * @param string $key
134
     * @param mixed $value
135
     *
136
     * @return $this
137
     */
138
    public function setParameter($key, $value)
139
    {
140
        $this->parameters[$key] = $value;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @param int $perPage
147
     * @param string $pageName
148
     *
149
     * @return $this
150
     */
151
    public function paginate($perPage = 25, $pageName = 'page')
152
    {
153
        $this->paginate = (int) $perPage;
154
        $this->pageName = $pageName;
155
156
        return $this;
157
    }
158
159
    /**
160
     * @return $this
161
     */
162
    public function disablePagination()
163
    {
164
        $this->paginate = 0;
165
166
        return $this;
167
    }
168
169
    /**
170
     * @return bool
171
     */
172
    public function usePagination()
173
    {
174
        return $this->paginate > 0;
175
    }
176
177
    /**
178
     * @return array
179
     * @throws \Exception
180
     */
181
    public function toArray()
182
    {
183
        $model = $this->getModelConfiguration();
184
185
        $params = parent::toArray();
186
187
        $params['creatable'] = $model->isCreatable();
188
        $params['createUrl'] = $model->getCreateUrl($this->getParameters() + Request::all());
189
        $params['collection'] = $this->getCollection();
190
191
        $params['extensions'] = $this->getExtensions()->renderable()->sortByOrder();
192
        $params['newEntryButtonText'] = $this->getNewEntryButtonText();
193
        $params['panel_class'] = $this->getPanelClass();
194
195
        return $params;
196
    }
197
198
    /**
199
     * $collection Collection|LengthAwarePaginator|Builder.
200
     */
201
    public function setCollection($collection)
202
    {
203
        $this->collection = $collection;
204
    }
205
206
    /**
207
     * @return Collection|LengthAwarePaginator|Builder
208
     * @throws \Exception
209
     */
210
    public function getCollection()
211
    {
212
        if (! $this->isInitialized()) {
213
            throw new Exception('Display is not initialized');
214
        }
215
216
        if (! is_null($this->collection)) {
217
            return $this->collection;
218
        }
219
220
        $query = $this->getRepository()->getQuery();
221
222
        $this->modifyQuery($query);
223
224
        return $this->collection = $this->usePagination()
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->usePagination() ?...eName)) : $query->get() can also be of type Illuminate\Pagination\LengthAwarePaginator. However, the property $collection is declared as type Illuminate\Support\Collection. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
225
            ? $query->paginate($this->paginate, ['*'], $this->pageName)->appends(request()->except($this->pageName))
226
            : $query->get();
227
    }
228
229
    /**
230
     * @param \Illuminate\Database\Eloquent\Builder|Builder $query
231
     */
232
    protected function modifyQuery(Builder $query)
233
    {
234
        $this->extensions->modifyQuery($query);
235
    }
236
237
    /**
238
     * Apply offset and limit to the query.
239
     *
240
     * @param \Illuminate\Database\Query\Builder $query
241
     * @param \Illuminate\Http\Request $request
242
     */
243
    public function applyOffset($query, \Illuminate\Http\Request $request)
244
    {
245
        $offset = $request->input('start', 0);
246
        $limit = $request->input('length', 10);
247
248
        if ($limit == -1) {
249
            return;
250
        }
251
252
        $query->offset((int) $offset)->limit((int) $limit);
253
    }
254
255
    /**
256
     * Apply search to the query.
257
     *
258
     * @param Builder $query
259
     * @param \Illuminate\Http\Request $request
260
     */
261
    public function applySearch(Builder $query, \Illuminate\Http\Request $request)
262
    {
263
        $search = $request->input('search.value');
264
        if (empty($search)) {
265
            return;
266
        }
267
268
        $query->where(function (Builder $query) use ($search) {
269
            $columns = $this->getColumns()->all();
270
271
            $_model = $query->getModel();
272
273
            foreach ($columns as $column) {
274
                if ($column->isSearchable()) {
275
                    if ($column instanceof ColumnInterface) {
276
                        if (($metaInstance = $column->getMetaData()) instanceof ColumnMetaInterface) {
0 ignored issues
show
Bug introduced by
The type SleepingOwl\Admin\Display\ColumnMetaInterface 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...
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

276
                        if (($metaInstance = $column->/** @scrutinizer ignore-call */ getMetaData()) instanceof ColumnMetaInterface) {
Loading history...
277
                            if (method_exists($metaInstance, 'onSearch')) {
278
                                $metaInstance->onSearch($column, $query, $search);
279
                                continue;
280
                            }
281
                        }
282
283
                        if (is_callable($callback = $column->getSearchCallback())) {
0 ignored issues
show
Bug introduced by
The method getSearchCallback() 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

283
                        if (is_callable($callback = $column->/** @scrutinizer ignore-call */ getSearchCallback())) {
Loading history...
284
                            $callback($column, $query, $search);
285
                            continue;
286
                        }
287
                    }
288
289
                    if ($_model->getAttribute($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

289
                    if ($_model->getAttribute($column->/** @scrutinizer ignore-call */ getName())) {
Loading history...
290
                        continue;
291
                    }
292
293
                    $query->orWhere($column->getName(), 'like', '%'.$search.'%');
294
                }
295
            }
296
        });
297
    }
298
}
299