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.
Test Setup Failed
Push — master ( fd45eb...f91a4c )
by Dave
35:25 queued 16:23
created

DisplayDatatablesAsync::checkSearchableColumns()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 12
1
<?php
2
3
namespace SleepingOwl\Admin\Display;
4
5
use Request;
6
use Illuminate\Routing\Router;
7
use Illuminate\Support\Collection;
8
use Illuminate\Database\Eloquent\Builder;
9
use SleepingOwl\Admin\Display\Column\Link;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, SleepingOwl\Admin\Display\Link.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use SleepingOwl\Admin\Display\Column\Text;
11
use SleepingOwl\Admin\Display\Column\Email;
12
use SleepingOwl\Admin\Display\Column\Control;
13
use SleepingOwl\Admin\Contracts\WithRoutesInterface;
14
use SleepingOwl\Admin\Contracts\Display\ColumnInterface;
15
use SleepingOwl\Admin\Contracts\Display\ColumnMetaInterface;
16
17
class DisplayDatatablesAsync extends DisplayDatatables implements WithRoutesInterface
18
{
19
    /**
20
     * Register display routes.
21
     *
22
     * @param Router $router
23
     *
24
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
25
     */
26 285 View Code Duplication
    public static function registerRoutes(Router $router)
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...
27
    {
28 285
        $routeName = 'admin.display.async';
29 285
        if (! $router->has($routeName)) {
30 285
            $router->get('{adminModel}/async/{adminDisplayName?}', [
31 285
                'as'   => $routeName,
32 285
                'uses' => 'SleepingOwl\Admin\Http\Controllers\DisplayController@async',
33 285
            ]);
34 285
        }
35
36 285
        $routeName = 'admin.display.async.inlineEdit';
37 285
        if (! $router->has($routeName)) {
38 285
            $router->post('{adminModel}/async/{adminDisplayName?}', [
39 285
                'as'   => $routeName,
40 285
                'uses' => 'SleepingOwl\Admin\Http\Controllers\AdminController@inlineEdit',
41 285
            ]);
42 285
        }
43 285
    }
44
45
    protected $payload;
46
    /**
47
     * @var string
48
     */
49
    protected $name;
50
51
    /**
52
     * @param string|null $name
53
     */
54
    protected $distinct;
55
56
    /**
57
     * @var
58
     */
59
    protected $displaySearch = false;
60
61
    /**
62
     * @var
63
     */
64
    protected $displayLength = false;
65
66
    /**
67
     * @var array
68
     */
69
    protected $searchableColumns = [
70
        Text::class,
71
        Link::class,
72
        Email::class,
73
    ];
74
75
    /**
76
     * DisplayDatatablesAsync constructor.
77
     *
78
     * @param string|null $name
79
     * @param string|null $distinct
80
     */
81
    public function __construct($name = null, $distinct = null)
82
    {
83
        parent::__construct();
84
85
        $this->setName($name);
86
        $this->setDistinct($distinct);
87
88
        $this->getColumns()->setView('display.extensions.columns_async');
89
    }
90
91
    /**
92
     * Initialize display.
93
     */
94
    public function initialize()
95
    {
96
        parent::initialize();
97
98
        $attributes = Request::all();
99
        array_unshift($attributes, $this->getName());
100
        array_unshift($attributes, $this->getModelConfiguration()->getAlias());
101
102
        $this->setHtmlAttribute('style', 'width:100%');
103
        $this->setHtmlAttribute('data-url', route('admin.display.async', $attributes));
104
        $this->setHtmlAttribute('data-payload', json_encode($this->payload));
105
106
        if ($this->getDisplaySearch()) {
107
            $this->setHtmlAttribute('data-display-search', 1);
108
        }
109
110
        if ($this->getDisplayLength()) {
111
            $this->setHtmlAttribute('data-display-dtlength', 1);
112
        }
113
    }
114
115
    /**
116
     * @param bool $length
117
     * @return $this
118
     */
119
    public function setDisplayLength($length)
120
    {
121
        $this->displayLength = $length;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    public function getDisplayLength()
130
    {
131
        return $this->displayLength;
132
    }
133
134
    /**
135
     * @param $search
136
     * @return $this
137
     */
138
    public function setDisplaySearch($search)
139
    {
140
        $this->displaySearch = $search;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function getDisplaySearch()
149
    {
150
        return $this->displaySearch;
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function getName()
157
    {
158
        return $this->name;
159
    }
160
161
    /**
162
     * @param string $name
163
     *
164
     * @return $this
165
     */
166
    public function setName($name)
167
    {
168
        $this->name = $name;
169
170
        return $this;
171
    }
172
173
    /**
174
     * @return mixed
175
     */
176
    public function getDistinct()
177
    {
178
        return $this->distinct;
179
    }
180
181
    /**
182
     * @param mixed $distinct
183
     *
184
     * @return $this
185
     */
186
    public function setDistinct($distinct)
187
    {
188
        $this->distinct = $distinct;
189
190
        return $this;
191
    }
192
193
    /**
194
     * Render async request.
195
     *
196
     * @param \Illuminate\Http\Request $request
197
     *
198
     * @return array
199
     */
200
    public function renderAsync(\Illuminate\Http\Request $request)
201
    {
202
        $query = $this->getRepository()->getQuery();
203
        $totalCount = $query->count();
204
        $filteredCount = 0;
205
206
        if (! is_null($this->distinct)) {
207
            $filteredCount = $query->distinct()->count($this->getDistinct());
208
        }
209
210
        $this->modifyQuery($query);
211
        $this->applySearch($query, $request);
212
213
        if (is_null($this->distinct)) {
214
            $countQuery = clone $query;
215
            $countQuery->getQuery()->orders = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $orders.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
216
            $filteredCount = $countQuery->count();
217
        }
218
219
        $this->applyOffset($query, $request);
220
        $collection = $query->get();
221
222
        return $this->prepareDatatablesStructure($request, $collection, $totalCount, $filteredCount);
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $query->get() on line 220 can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, SleepingOwl\Admin\Displa...reDatatablesStructure() does only seem to accept object<Illuminate\Support\Collection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
223
    }
224
225
    /**
226
     * Apply offset and limit to the query.
227
     *
228
     * @param $query
229
     * @param \Illuminate\Http\Request $request
230
     */
231
    protected function applyOffset($query, \Illuminate\Http\Request $request)
232
    {
233
        $offset = $request->input('start', 0);
234
        $limit = $request->input('length', 10);
235
236
        if ($limit == -1) {
237
            return;
238
        }
239
240
        $query->offset((int) $offset)->limit((int) $limit);
241
    }
242
243
    /**
244
     * Check if column is searchable.
245
     *
246
     * @param ColumnInterface $column
247
     * @return bool
248
     */
249
    public function checkSearchableColumns(ColumnInterface $column)
250
    {
251
        foreach ($this->searchableColumns as $searchableColumn) {
252
            if ($column instanceof $searchableColumn) {
253
                return true;
254
            }
255
        }
256
257
        return false;
258
    }
259
260
    /**
261
     * Apply search to the query.
262
     *
263
     * @param Builder $query
264
     * @param \Illuminate\Http\Request $request
265
     */
266
    protected function applySearch(Builder $query, \Illuminate\Http\Request $request)
267
    {
268
        $search = $request->input('search.value');
269
        if (empty($search)) {
270
            return;
271
        }
272
273
        $query->where(function (Builder $query) use ($search) {
274
            $columns = $this->getColumns()->all();
275
276
            foreach ($columns as $column) {
277
                if ($this->checkSearchableColumns($column)) {
278 View Code Duplication
                    if ($column instanceof ColumnInterface) {
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...
279
                        if (($metaInstance = $column->getMetaData()) instanceof ColumnMetaInterface) {
280
                            if (method_exists($metaInstance, 'onSearch')) {
281
                                $metaInstance->onSearch($column, $query, $search);
0 ignored issues
show
Bug introduced by
The method onSearch() does not seem to exist on object<SleepingOwl\Admin...ay\ColumnMetaInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
282
                                continue;
283
                            }
284
                        }
285
286
                        if (is_callable($callback = $column->getSearchCallback())) {
287
                            $callback($column, $query, $search);
288
                            continue;
289
                        }
290
                    }
291
292
                    $query->orWhere($column->getName(), 'like', '%'.$search.'%');
293
                }
294
            }
295
        });
296
    }
297
298
    /**
299
     * Convert collection to the datatables structure.
300
     *
301
     * @param \Illuminate\Http\Request $request
302
     * @param array|Collection $collection
303
     * @param int $totalCount
304
     * @param int $filteredCount
305
     *
306
     * @return array
307
     */
308
    protected function prepareDatatablesStructure(
309
        \Illuminate\Http\Request $request,
310
        Collection $collection,
311
        $totalCount,
312
        $filteredCount
313
    ) {
314
        $columns = $this->getColumns();
315
316
        $result = [];
317
        $result['draw'] = $request->input('draw', 0);
318
        $result['recordsTotal'] = $totalCount;
319
        $result['recordsFiltered'] = $filteredCount;
320
        $result['data'] = [];
321
322
        foreach ($collection as $instance) {
323
            $_row = [];
324
325
            foreach ($columns->all() as $column) {
326
                $column->setModel($instance);
327
328
                if ($column instanceof Control) {
329
                    $column->initialize();
330
                }
331
332
                $_row[] = (string) $column;
333
            }
334
335
            $result['data'][] = $_row;
336
        }
337
338
        return $result;
339
    }
340
341
    /**
342
     * @return Collection
343
     * @throws \Exception
344
     */
345
    public function getCollection()
346
    {
347
    }
348
349
    /**
350
     * @param $payload
351
     */
352
    public function setPayload($payload)
353
    {
354
        $this->payload = $payload;
355
    }
356
357
    /**
358
     * @return mixed
359
     */
360
    public function getPayload()
361
    {
362
        return $this->payload;
363
    }
364
365
    /**
366
     * @return array
367
     */
368
    public function toArray()
369
    {
370
        $params = parent::toArray();
371
        $params['payload'] = $this->payload;
372
373
        return $params;
374
    }
375
}
376