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 — analysis-VrvoWO ( 0540aa )
by butschster
09:29
created

DisplayTree::getCollapsedLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SleepingOwl\Admin\Display;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Routing\Router;
9
use Illuminate\Support\Facades\Request;
10
use SleepingOwl\Admin\Contracts\Display\ColumnInterface;
11
use SleepingOwl\Admin\Contracts\Repositories\TreeRepositoryInterface;
12
use SleepingOwl\Admin\Contracts\WithRoutesInterface;
13
use SleepingOwl\Admin\Display\Extension\Columns;
14
use SleepingOwl\Admin\Display\Tree\OrderTreeType;
15
use SleepingOwl\Admin\Repositories\TreeRepository;
16
use SleepingOwl\Admin\Traits\CardControl;
17
18
/**
19
 * @method TreeRepositoryInterface getRepository()
20
 * @property TreeRepositoryInterface $repository
21
 * @method Columns getColumns()
22
 * @method $this setColumns(ColumnInterface|ColumnInterface[] $column)
23
 */
24
class DisplayTree extends Display implements WithRoutesInterface
25
{
26
    use CardControl;
27
28
    /**
29
     * @param Router $router
30
     */
31
    public static function registerRoutes(Router $router)
32
    {
33
        $routeName = 'admin.display.tree.reorder';
34
        if (! $router->has($routeName)) {
35
            $router->post('{adminModel}/reorder',
36
                ['as' => $routeName, 'uses' => 'SleepingOwl\Admin\Http\Controllers\DisplayController@treeReorder']);
0 ignored issues
show
Bug introduced by
The controller App\Http\Controllers\Sle...llers\DisplayController does not seem to exist.

If there is a route defined but the controller class cannot be found there are two options: 1. the controller class needs to be implemented or 2. the route is outdated and can be removed.

If ?FooController? was found and ?BarController? is missing for the following example, either the controller should be implemented or the route should be removed:

$app->group(['as' => 'foo', 'prefix' => 'foo', 'namespace' => 'Foo'], function($app) {
    $app->group(['as' => 'foo', 'prefix' => 'foo'], function($app) {
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'FooController@getFoo']);
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'BarController@getBar']);
    });
});
Loading history...
37
        }
38
    }
39
40
    /**
41
     * @var int
42
     */
43
    protected $max_depth = 20;
44
    /**
45
     * @var string
46
     */
47
    protected $view = 'display.tree';
48
49
    /**
50
     * @var array
51
     */
52
    protected $parameters = [];
53
54
    /**
55
     * @var bool
56
     */
57
    protected $reorderable = true;
58
59
    /**
60
     * @var string|callable
61
     */
62
    protected $value = 'title';
63
64
    /**
65
     * @var int
66
     */
67
    protected $collapsedLevel = 5;
68
69
    /**
70
     * @var string
71
     */
72
    protected $parentField = 'parent_id';
73
74
    /**
75
     * @var string
76
     */
77
    protected $orderField = 'order';
78
79
    /**
80
     * @var string|null
81
     */
82
    protected $rootParentId = null;
83
84
    /**
85
     * @var string
86
     */
87
    protected $repositoryClass = TreeRepository::class;
88
89
    /**
90
     * @var Column\TreeControl
91
     */
92
    protected $controlColumn;
93
94
    /**
95
     * @var Collection
96
     */
97
    protected $collection;
98
99
    /**
100
     * @var string|null
101
     */
102
    protected $newEntryButtonText;
103
104
    /**
105
     * @var string
106
     */
107
    protected $treeType;
108
109
    /**
110
     * DisplayTree constructor.
111
     *
112
     * @param string|null $treeType
113
     */
114
    public function __construct($treeType = null)
115
    {
116
        parent::__construct();
117
118
        $this->treeType = $treeType;
119
120
        $this->setCardClass('card-tree');
121
122
        $this->extend('columns', new Columns());
123
    }
124
125
    public function initialize()
126
    {
127
        parent::initialize();
128
129
        $repository = $this->getRepository()
130
            ->setOrderField($this->getOrderField())
131
            ->setRootParentId($this->getRootParentId());
132
133
        if ($this->getParentField()) {
134
            $repository = $repository->setParentField($this->getParentField());
135
        }
136
        if (! is_null($this->treeType)) {
0 ignored issues
show
introduced by
The condition is_null($this->treeType) is always false.
Loading history...
137
            $repository->setTreeType($this->treeType);
0 ignored issues
show
Bug introduced by
The method setTreeType() does not exist on SleepingOwl\Admin\Contra...TreeRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to SleepingOwl\Admin\Contra...TreeRepositoryInterface. ( Ignorable by Annotation )

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

137
            $repository->/** @scrutinizer ignore-call */ 
138
                         setTreeType($this->treeType);
Loading history...
138
        }
139
140
        if ($this->treeType == OrderTreeType::class) {
141
            $this->setMaxDepth(1);
142
        }
143
144
        $this->setHtmlAttribute('data-max-depth', $this->getMaxDepth());
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getMaxDepth()
151
    {
152
        return $this->max_depth;
153
    }
154
155
    /**
156
     * @param string|callable $value
157
     *
158
     * @return $this
159
     */
160
    public function setMaxDepth($value)
161
    {
162
        $this->max_depth = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type callable or string is incompatible with the declared type integer of property $max_depth.

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...
163
164
        return $this;
165
    }
166
167
    /**
168
     * @return callable|string
169
     */
170
    public function getValue()
171
    {
172
        return $this->value;
173
    }
174
175
    /**
176
     * @param string|callable $value
177
     *
178
     * @return $this
179
     */
180
    public function setValue($value)
181
    {
182
        $this->value = $value;
183
184
        return $this;
185
    }
186
187
    /**
188
     * @return int
189
     */
190
    public function getCollapsedLevel()
191
    {
192
        return $this->collapsedLevel;
193
    }
194
195
    /**
196
     * @param int $level
197
     *
198
     * @return $this
199
     */
200
    public function setCollapsedLevel($level)
201
    {
202
        $this->collapsedLevel = $level;
203
204
        return $this;
205
    }
206
207
    /**
208
     * @return string
209
     */
210
    public function getParentField()
211
    {
212
        return $this->parentField;
213
    }
214
215
    /**
216
     * @param string $parentField
217
     *
218
     * @return $this
219
     */
220
    public function setParentField($parentField)
221
    {
222
        $this->parentField = $parentField;
223
224
        return $this;
225
    }
226
227
    /**
228
     * @return array|\Illuminate\Contracts\Translation\Translator|null|string
229
     */
230
    public function getNewEntryButtonText()
231
    {
232
        if (is_null($this->newEntryButtonText)) {
233
            $this->newEntryButtonText = trans('sleeping_owl::lang.button.new-entry');
0 ignored issues
show
Documentation Bug introduced by
It seems like trans('sleeping_owl::lang.button.new-entry') can also be of type array or array. However, the property $newEntryButtonText is declared as type null|string. 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...
234
        }
235
236
        return $this->newEntryButtonText;
237
    }
238
239
    /**
240
     * @param string $newEntryButtonText
241
     *
242
     * @return $this
243
     */
244
    public function setNewEntryButtonText($newEntryButtonText)
245
    {
246
        $this->newEntryButtonText = $newEntryButtonText;
247
248
        return $this;
249
    }
250
251
    /**
252
     * @return string
253
     */
254
    public function getOrderField()
255
    {
256
        return $this->orderField;
257
    }
258
259
    /**
260
     * @param string $orderField
261
     *
262
     * @return $this
263
     */
264
    public function setOrderField($orderField)
265
    {
266
        $this->orderField = $orderField;
267
268
        return $this;
269
    }
270
271
    /**
272
     * @return null|string
273
     */
274
    public function getRootParentId()
275
    {
276
        return $this->rootParentId;
277
    }
278
279
    /**
280
     * @param null|string $rootParentId
281
     *
282
     * @return $this
283
     */
284
    public function setRootParentId($rootParentId)
285
    {
286
        $this->rootParentId = $rootParentId;
287
288
        return $this;
289
    }
290
291
    /**
292
     * @return array
293
     */
294
    public function getParameters()
295
    {
296
        return $this->parameters;
297
    }
298
299
    /**
300
     * @param array $parameters
301
     *
302
     * @return $this
303
     */
304
    public function setParameters($parameters)
305
    {
306
        $this->parameters = $parameters;
307
308
        return $this;
309
    }
310
311
    /**
312
     * @param string $key
313
     * @param mixed $value
314
     *
315
     * @return $this
316
     */
317
    public function setParameter($key, $value)
318
    {
319
        $this->parameters[$key] = $value;
320
321
        return $this;
322
    }
323
324
    /**
325
     * @return bool
326
     */
327
    public function isReorderable()
328
    {
329
        return $this->reorderable;
330
    }
331
332
    /**
333
     * @param bool $reorderable
334
     *
335
     * @return $this
336
     */
337
    public function setReorderable($reorderable)
338
    {
339
        $this->reorderable = (bool) $reorderable;
340
341
        return $this;
342
    }
343
344
    /**
345
     * @return array
346
     * @throws \Exception
347
     */
348
    public function toArray()
349
    {
350
        $model = $this->getModelConfiguration();
351
        $this->setHtmlAttribute('class', 'dd nestable');
352
353
        return parent::toArray() + [
354
                'items' => $this->getRepository()->getTree($this->getCollection()),
355
                'reorderable' => $this->isReorderable(),
356
                'url' => $model->getDisplayUrl(),
357
                'value' => $this->getValue(),
358
                'collapsedLevel' => $this->getCollapsedLevel(),
359
                'creatable' => $model->isCreatable(),
360
                'createUrl' => $model->getCreateUrl($this->getParameters() + Request::all()),
361
                'controls' => [$this->getColumns()->getControlColumn()],
362
                'newEntryButtonText' => $this->getNewEntryButtonText(),
363
                'max_depth' => $this->getMaxDepth(),
364
                'card_class' => $this->getCardClass(),
365
            ];
366
    }
367
368
    /**
369
     * @return Collection
370
     * @throws \Exception
371
     */
372
    public function getCollection()
373
    {
374
        if (! $this->isInitialized()) {
375
            throw new Exception('Display is not initialized');
376
        }
377
378
        if (! is_null($this->collection)) {
379
            return $this->collection;
380
        }
381
382
        $query = $this->getRepository()->getQuery();
383
384
        $this->modifyQuery($query);
385
386
        if (method_exists($query, 'defaultOrder')) {
387
            return $query->defaultOrder()->get();
388
        }
389
390
        return $query->get();
391
    }
392
393
    /**
394
     * @param \Illuminate\Database\Eloquent\Builder $query
395
     */
396
    protected function modifyQuery(Builder $query)
397
    {
398
        $this->extensions->modifyQuery($query);
399
    }
400
401
    /**
402
     * @return \Illuminate\Foundation\Application|mixed
403
     * @throws \Exception
404
     */
405
    protected function makeRepository()
406
    {
407
        $repository = parent::makeRepository();
408
409
        if (! ($repository instanceof TreeRepositoryInterface)) {
410
            throw new Exception('Repository class must be instanced of [TreeRepositoryInterface]');
411
        }
412
413
        return $repository;
414
    }
415
}
416