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 — master ( b2510c...ee0bb8 )
by
unknown
12:46
created

DisplayDatatablesAsync   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 312
Duplicated Lines 9.94 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 12.9%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 31
loc 312
c 2
b 0
f 0
ccs 16
cts 124
cp 0.129
rs 9.3999
wmc 33
lcom 2
cbo 9

17 Methods

Rating   Name   Duplication   Size   Complexity  
A registerRoutes() 18 18 3
A __construct() 0 9 1
A initialize() 0 16 2
A setSearching() 0 6 1
A getSearching() 0 4 1
A getName() 0 4 1
A setName() 0 6 1
A getDistinct() 0 4 1
A setDistinct() 0 6 1
A renderAsync() 0 22 3
A applyOffset() 0 11 2
C applySearch() 13 31 8
B prepareDatatablesStructure() 0 32 4
A getCollection() 0 3 1
A setPayload() 0 4 1
A getPayload() 0 4 1
A toArray() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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;
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\ColumnMetaInterface;
15
use SleepingOwl\Admin\Contracts\Display\NamedColumnInterface;
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 array
63
     */
64
    protected $searchableColumns = [
65
        Text::class,
66
        Link::class,
67
        Email::class,
68
    ];
69
70
    /**
71
     * DisplayDatatablesAsync constructor.
72
     *
73
     * @param string|null $name
74
     * @param string|null $distinct
75
     */
76
    public function __construct($name = null, $distinct = null)
77
    {
78
        parent::__construct();
79
80
        $this->setName($name);
81
        $this->setDistinct($distinct);
82
83
        $this->getColumns()->setView('display.extensions.columns_async');
84
    }
85
86
    /**
87
     * Initialize display.
88
     */
89
    public function initialize()
90
    {
91
        parent::initialize();
92
93
        $attributes = Request::all();
94
        array_unshift($attributes, $this->getName());
95
        array_unshift($attributes, $this->getModelConfiguration()->getAlias());
96
97
        $this->setHtmlAttribute('style', 'width:100%');
98
        $this->setHtmlAttribute('data-url', route('admin.display.async', $attributes));
99
        $this->setHtmlAttribute('data-payload', json_encode($this->payload));
100
101
        if ($this->getSearching()) {
102
            $this->setHtmlAttribute('data-display-searching', 1);
103
        }
104
    }
105
106
    /**
107
     * @param $searching
108
     * @return $this
109
     */
110
    public function setSearching($searching)
111
    {
112
        $this->displaySearch = $searching;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @return bool
119
     */
120
    public function getSearching()
121
    {
122
        return $this->displaySearch;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getName()
129
    {
130
        return $this->name;
131
    }
132
133
    /**
134
     * @param string $name
135
     *
136
     * @return $this
137
     */
138
    public function setName($name)
139
    {
140
        $this->name = $name;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return mixed
147
     */
148
    public function getDistinct()
149
    {
150
        return $this->distinct;
151
    }
152
153
    /**
154
     * @param mixed $distinct
155
     *
156
     * @return $this
157
     */
158
    public function setDistinct($distinct)
159
    {
160
        $this->distinct = $distinct;
161
162
        return $this;
163
    }
164
165
    /**
166
     * Render async request.
167
     *
168
     * @param \Illuminate\Http\Request $request
169
     *
170
     * @return array
171
     */
172
    public function renderAsync(\Illuminate\Http\Request $request)
173
    {
174
        $query = $this->getRepository()->getQuery();
175
        $totalCount = $query->count();
176
        $filteredCount = 0;
177
178
        if (! is_null($this->distinct)) {
179
            $filteredCount = $query->distinct()->count($this->getDistinct());
180
        }
181
182
        $this->modifyQuery($query);
183
        $this->applySearch($query, $request);
184
185
        if (is_null($this->distinct)) {
186
            $filteredCount = $query->count();
187
        }
188
189
        $this->applyOffset($query, $request);
190
        $collection = $query->get();
191
192
        return $this->prepareDatatablesStructure($request, $collection, $totalCount, $filteredCount);
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $query->get() on line 190 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...
193
    }
194
195
    /**
196
     * Apply offset and limit to the query.
197
     *
198
     * @param $query
199
     * @param \Illuminate\Http\Request $request
200
     */
201
    protected function applyOffset($query, \Illuminate\Http\Request $request)
202
    {
203
        $offset = $request->input('start', 0);
204
        $limit = $request->input('length', 10);
205
206
        if ($limit == -1) {
207
            return;
208
        }
209
210
        $query->offset((int) $offset)->limit((int) $limit);
211
    }
212
213
    /**
214
     * Apply search to the query.
215
     *
216
     * @param Builder $query
217
     * @param \Illuminate\Http\Request $request
218
     */
219
    protected function applySearch(Builder $query, \Illuminate\Http\Request $request)
220
    {
221
        $search = $request->input('search.value');
222
        if (empty($search)) {
223
            return;
224
        }
225
226
        $query->where(function ($query) use ($search) {
227
            $columns = $this->getColumns()->all();
228
229
            foreach ($columns as $column) {
230
                if (in_array(get_class($column), $this->searchableColumns)) {
231 View Code Duplication
                    if ($column instanceof NamedColumnInterface) {
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...
232
                        if (($metaInstance = $column->getMetaData()) instanceof ColumnMetaInterface) {
233
                            if (method_exists($metaInstance, 'onSearch')) {
234
                                $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...
235
                                continue;
236
                            }
237
                        }
238
239
                        if (is_callable($callback = $column->getSearchCallback())) {
240
                            $callback($column, $query, $search);
241
                            continue;
242
                        }
243
                    }
244
245
                    $query->orWhere($column->getName(), 'like', '%'.$search.'%');
246
                }
247
            }
248
        });
249
    }
250
251
    /**
252
     * Convert collection to the datatables structure.
253
     *
254
     * @param \Illuminate\Http\Request $request
255
     * @param array|Collection $collection
256
     * @param int $totalCount
257
     * @param int $filteredCount
258
     *
259
     * @return array
260
     */
261
    protected function prepareDatatablesStructure(
262
        \Illuminate\Http\Request $request,
263
        Collection $collection,
264
        $totalCount,
265
        $filteredCount
266
    ) {
267
        $columns = $this->getColumns();
268
269
        $result = [];
270
        $result['draw'] = $request->input('draw', 0);
271
        $result['recordsTotal'] = $totalCount;
272
        $result['recordsFiltered'] = $filteredCount;
273
        $result['data'] = [];
274
275
        foreach ($collection as $instance) {
276
            $_row = [];
277
278
            foreach ($columns->all() as $column) {
279
                $column->setModel($instance);
280
281
                if ($column instanceof Control) {
282
                    $column->initialize();
283
                }
284
285
                $_row[] = (string) $column;
286
            }
287
288
            $result['data'][] = $_row;
289
        }
290
291
        return $result;
292
    }
293
294
    /**
295
     * @return Collection
296
     * @throws \Exception
297
     */
298
    public function getCollection()
299
    {
300
    }
301
302
    /**
303
     * @param $payload
304
     */
305
    public function setPayload($payload)
306
    {
307
        $this->payload = $payload;
308
    }
309
310
    /**
311
     * @return mixed
312
     */
313
    public function getPayload()
314
    {
315
        return $this->payload;
316
    }
317
318
    /**
319
     * @return array
320
     */
321
    public function toArray()
322
    {
323
        $params = parent::toArray();
324
        $params['payload'] = $this->payload;
325
326
        return $params;
327
    }
328
}
329