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
Pull Request — new (#864)
by Dave
07:45
created

DisplayTree::getCollection()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 0
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Display;
4
5
use Request;
6
use Illuminate\Routing\Router;
7
use Illuminate\Database\Eloquent\Collection;
8
use SleepingOwl\Admin\Display\Tree\OrderTreeType;
9
use SleepingOwl\Admin\Repositories\TreeRepository;
10
use SleepingOwl\Admin\Contracts\WithRoutesInterface;
11
use SleepingOwl\Admin\Contracts\Repositories\TreeRepositoryInterface;
12
13
/**
14
 * @method TreeRepositoryInterface getRepository()
15
 * @property TreeRepositoryInterface $repository
16
 */
17
class DisplayTree extends Display implements WithRoutesInterface
18
{
19
    /**
20
     * @param Router $router
21
     */
22
    public static function registerRoutes(Router $router)
23
    {
24
        $routeName = 'admin.display.tree.reorder';
25
        if (! $router->has($routeName)) {
26
            $router->post('{adminModel}/reorder',
27
                ['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...
28
        }
29
    }
30
31
    /**
32
     * @var int
33
     */
34
    protected $max_depth = 20;
35
    /**
36
     * @var string
37
     */
38
    protected $view = 'display.tree';
39
40
    /**
41
     * @var array
42
     */
43
    protected $parameters = [];
44
45
    /**
46
     * @var bool
47
     */
48
    protected $reorderable = true;
49
50
    /**
51
     * @var string|callable
52
     */
53
    protected $value = 'title';
54
55
    /**
56
     * @var string
57
     */
58
    protected $parentField = 'parent_id';
59
60
    /**
61
     * @var string
62
     */
63
    protected $orderField = 'order';
64
65
    /**
66
     * @var string|null
67
     */
68
    protected $rootParentId = null;
69
70
    /**
71
     * @var string
72
     */
73
    protected $repositoryClass = TreeRepository::class;
74
75
    /**
76
     * @var Column\TreeControl
77
     */
78
    protected $controlColumn;
79
80
    /**
81
     * @var Collection
82
     */
83
    protected $collection;
84
85
    /**
86
     * @var string|null
87
     */
88
    protected $newEntryButtonText;
89
90
    /**
91
     * @var string
92
     */
93
    protected $treeType;
94
95
    /**
96
     * DisplayTree constructor.
97
     *
98
     * @param string|null $treeType
99
     */
100
    public function __construct($treeType = null)
101
    {
102
        parent::__construct();
103
104
        $this->treeType = $treeType;
105
    }
106
107
    public function initialize()
108
    {
109
        parent::initialize();
110
111
        $repository = $this->getRepository()
112
            ->setOrderField($this->getOrderField())
113
            ->setRootParentId($this->getRootParentId());
114
115
        if ($this->getParentField()) {
116
            $repository = $repository->setParentField($this->getParentField());
117
        }
118
        if (! is_null($this->treeType)) {
0 ignored issues
show
introduced by
The condition is_null($this->treeType) is always false.
Loading history...
119
            $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

119
            $repository->/** @scrutinizer ignore-call */ 
120
                         setTreeType($this->treeType);
Loading history...
120
        }
121
122
        if ($this->treeType == OrderTreeType::class) {
123
            $this->setMaxDepth(1);
124
        }
125
126
        $this->setHtmlAttribute('data-max-depth', $this->getMaxDepth());
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getMaxDepth()
133
    {
134
        return $this->max_depth;
135
    }
136
137
    /**
138
     * @param string|callable $value
139
     *
140
     * @return $this
141
     */
142
    public function setMaxDepth($value)
143
    {
144
        $this->max_depth = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type string or callable 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...
145
146
        return $this;
147
    }
148
149
    /**
150
     * @return callable|string
151
     */
152
    public function getValue()
153
    {
154
        return $this->value;
155
    }
156
157
    /**
158
     * @param string|callable $value
159
     *
160
     * @return $this
161
     */
162
    public function setValue($value)
163
    {
164
        $this->value = $value;
165
166
        return $this;
167
    }
168
169
    /**
170
     * @return string
171
     */
172
    public function getParentField()
173
    {
174
        return $this->parentField;
175
    }
176
177
    /**
178
     * @param string $parentField
179
     *
180
     * @return $this
181
     */
182
    public function setParentField($parentField)
183
    {
184
        $this->parentField = $parentField;
185
186
        return $this;
187
    }
188
189
    /**
190
     * @return array|\Illuminate\Contracts\Translation\Translator|null|string
191
     */
192
    public function getNewEntryButtonText()
193
    {
194
        if (is_null($this->newEntryButtonText)) {
195
            $this->newEntryButtonText = trans('sleeping_owl::lang.table.new-entry');
0 ignored issues
show
Documentation Bug introduced by
It seems like trans('sleeping_owl::lang.table.new-entry') can also be of type 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...
196
        }
197
198
        return $this->newEntryButtonText;
199
    }
200
201
    /**
202
     * @param string $newEntryButtonText
203
     *
204
     * @return $this
205
     */
206
    public function setNewEntryButtonText($newEntryButtonText)
207
    {
208
        $this->newEntryButtonText = $newEntryButtonText;
209
210
        return $this;
211
    }
212
213
    /**
214
     * @return string
215
     */
216
    public function getOrderField()
217
    {
218
        return $this->orderField;
219
    }
220
221
    /**
222
     * @param string $orderField
223
     *
224
     * @return $this
225
     */
226
    public function setOrderField($orderField)
227
    {
228
        $this->orderField = $orderField;
229
230
        return $this;
231
    }
232
233
    /**
234
     * @return null|string
235
     */
236
    public function getRootParentId()
237
    {
238
        return $this->rootParentId;
239
    }
240
241
    /**
242
     * @param null|string $rootParentId
243
     *
244
     * @return $this
245
     */
246
    public function setRootParentId($rootParentId)
247
    {
248
        $this->rootParentId = $rootParentId;
249
250
        return $this;
251
    }
252
253
    /**
254
     * @return array
255
     */
256
    public function getParameters()
257
    {
258
        return $this->parameters;
259
    }
260
261
    /**
262
     * @param array $parameters
263
     *
264
     * @return $this
265
     */
266
    public function setParameters($parameters)
267
    {
268
        $this->parameters = $parameters;
269
270
        return $this;
271
    }
272
273
    /**
274
     * @param string $key
275
     * @param mixed $value
276
     *
277
     * @return $this
278
     */
279
    public function setParameter($key, $value)
280
    {
281
        $this->parameters[$key] = $value;
282
283
        return $this;
284
    }
285
286
    /**
287
     * @return bool
288
     */
289
    public function isReorderable()
290
    {
291
        return $this->reorderable;
292
    }
293
294
    /**
295
     * @param bool $reorderable
296
     *
297
     * @return $this
298
     */
299
    public function setReorderable($reorderable)
300
    {
301
        $this->reorderable = (bool) $reorderable;
302
303
        return $this;
304
    }
305
306
    /**
307
     * @return array
308
     * @throws \Exception
309
     */
310
    public function toArray()
311
    {
312
        $model = $this->getModelConfiguration();
313
314
        return parent::toArray() + [
315
                'items'              => $this->getRepository()->getTree($this->getCollection()),
316
                'reorderable'        => $this->isReorderable(),
317
                'url'                => $model->getDisplayUrl(),
318
                'value'              => $this->getValue(),
319
                'creatable'          => $model->isCreatable(),
320
                'createUrl'          => $model->getCreateUrl($this->getParameters() + Request::all()),
321
                'controls'           => [app('sleeping_owl.table.column')->treeControl()],
0 ignored issues
show
Bug introduced by
The method treeControl() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

321
                'controls'           => [app('sleeping_owl.table.column')->/** @scrutinizer ignore-call */ treeControl()],

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...
322
                'newEntryButtonText' => $this->getNewEntryButtonText(),
323
                'max_depth'          => $this->getMaxDepth(),
324
            ];
325
    }
326
327
    /**
328
     * @return Collection
329
     * @throws \Exception
330
     */
331
    public function getCollection()
332
    {
333
        if (! $this->isInitialized()) {
334
            throw new \Exception('Display is not initialized');
335
        }
336
337
        if (! is_null($this->collection)) {
338
            return $this->collection;
339
        }
340
341
        $query = $this->getRepository()->getQuery();
342
343
        $this->modifyQuery($query);
344
345
        if (method_exists($query, 'defaultOrder')) {
346
            return $query->defaultOrder()->get();
347
        }
348
349
        return $query->get();
350
    }
351
352
    /**
353
     * @param \Illuminate\Database\Eloquent\Builder|Builder $query
354
     */
355
    protected function modifyQuery(\Illuminate\Database\Eloquent\Builder $query)
356
    {
357
        $this->extensions->modifyQuery($query);
358
    }
359
360
    /**
361
     * @return \Illuminate\Foundation\Application|mixed
362
     * @throws \Exception
363
     */
364
    protected function makeRepository()
365
    {
366
        $repository = parent::makeRepository();
367
368
        if (! ($repository instanceof TreeRepositoryInterface)) {
369
            throw new \Exception('Repository class must be instanced of [TreeRepositoryInterface]');
370
        }
371
372
        return $repository;
373
    }
374
}
375