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 ( 8f27f1...172484 )
by Dave
27:44 queued 21:58
created

DisplayTable::getNewEntryButtonText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 1
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Display;
4
5
use Request;
6
use Illuminate\Support\Collection;
7
use Illuminate\Database\Eloquent\Builder;
8
use SleepingOwl\Admin\Traits\PanelControl;
9
use Illuminate\Pagination\LengthAwarePaginator;
10
use SleepingOwl\Admin\Display\Extension\Columns;
11
use SleepingOwl\Admin\Display\Extension\ColumnsTotal;
12
use SleepingOwl\Admin\Display\Extension\ColumnFilters;
13
use SleepingOwl\Admin\Contracts\Display\ColumnInterface;
14
use SleepingOwl\Admin\Contracts\Display\ColumnMetaInterface;
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
     * Initialize display.
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
     * Apply offset and limit to the query.
208
     *
209
     * @param $query
210
     * @param \Illuminate\Http\Request $request
211
     */
212
    public function applyOffset($query, \Illuminate\Http\Request $request)
213
    {
214
        $offset = $request->input('start', 0);
215
        $limit = $request->input('length', 10);
216
217
        if ($limit == -1) {
218
            return;
219
        }
220
221
        $query->offset((int) $offset)->limit((int) $limit);
222
    }
223
224
    /**
225
     * Apply search to the query.
226
     *
227
     * @param Builder $query
228
     * @param \Illuminate\Http\Request $request
229
     */
230
    public function applySearch(Builder $query, \Illuminate\Http\Request $request)
231
    {
232
        $search = $request->input('search.value');
233
        if (empty($search)) {
234
            return;
235
        }
236
237
        $query->where(function (Builder $query) use ($search) {
238
            $columns = $this->getColumns()->all();
239
240
            foreach ($columns as $column) {
241
                if ($column->isSearchable()) {
242
                    if ($column instanceof ColumnInterface) {
243
                        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

243
                        if (($metaInstance = $column->/** @scrutinizer ignore-call */ getMetaData()) instanceof ColumnMetaInterface) {
Loading history...
244
                            if (method_exists($metaInstance, 'onSearch')) {
245
                                $metaInstance->onSearch($column, $query, $search);
246
                                continue;
247
                            }
248
                        }
249
250
                        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

250
                        if (is_callable($callback = $column->/** @scrutinizer ignore-call */ getSearchCallback())) {
Loading history...
251
                            $callback($column, $query, $search);
252
                            continue;
253
                        }
254
                    }
255
256
                    $query->orWhere($column->getName(), 'like', '%'.$search.'%');
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

256
                    $query->orWhere($column->/** @scrutinizer ignore-call */ getName(), 'like', '%'.$search.'%');
Loading history...
257
                }
258
            }
259
        });
260
    }
261
262
    /**
263
     * @return Collection|LengthAwarePaginator|Builder
264
     * @throws \Exception
265
     */
266
    public function getCollection()
267
    {
268
        if (! $this->isInitialized()) {
269
            throw new \Exception('Display is not initialized');
270
        }
271
272
        if (! is_null($this->collection)) {
273
            return $this->collection;
274
        }
275
276
        $query = $this->getRepository()->getQuery();
277
278
        $this->modifyQuery($query);
279
280
        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...
281
            ? $query->paginate($this->paginate, ['*'], $this->pageName)->appends(request()->except($this->pageName))
282
            : $query->get();
283
    }
284
285
    /**
286
     * @param \Illuminate\Database\Eloquent\Builder|Builder $query
287
     */
288
    protected function modifyQuery(\Illuminate\Database\Eloquent\Builder $query)
289
    {
290
        $this->extensions->modifyQuery($query);
291
    }
292
}
293