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 ( b864c9...ed9166 )
by butschster
12:41
created

DisplayTable::applyOrders()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 8
nop 1
dl 0
loc 25
rs 6.7272
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\Display\Extension\Columns;
9
use SleepingOwl\Admin\Display\Extension\ColumnFilters;
10
use SleepingOwl\Admin\Contracts\Display\ColumnInterface;
11
use SleepingOwl\Admin\Contracts\Display\Extension\ColumnFilterInterface;
12
13
/**
14
 * Class DisplayTable.
15
16
 * @method Columns getColumns()
17
 * @method $this setColumns(ColumnInterface|ColumnInterface[] $column)
18
 *
19
 * @method ColumnFilters getColumnFilters()
20
 * @method $this setColumnFilters(ColumnFilterInterface $filters = null, ...$filters)
21
 */
22
class DisplayTable extends Display
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $view = 'display.table';
28
29
    /**
30
     * @var array
31
     */
32
    protected $parameters = [];
33
34
    /**
35
     * @var int|null
36
     */
37
    protected $paginate = 25;
38
39
    /**
40
     * @var string
41
     */
42
    protected $pageName = 'page';
43
44
    /**
45
     * @var Collection
46
     */
47
    protected $collection;
48
49
    /**
50
     * @var string|null
51
     */
52
    protected $newEntryButtonText;
53
54
    /**
55
     * Display constructor.
56
     */
57
    public function __construct()
58
    {
59
        parent::__construct();
60
61
        $this->extend('columns', new Columns());
62
        $this->extend('column_filters', new ColumnFilters());
63
    }
64
65
    /**
66
     * Initialize display.
67
     */
68
    public function initialize()
69
    {
70
        parent::initialize();
71
72
        if ($this->getModelConfiguration()->isRestorableModel()) {
73
            $this->setApply(function ($q) {
0 ignored issues
show
Bug introduced by
The call to setApply() misses a required argument $...$applies.

This check looks for function calls that miss required arguments.

Loading history...
74
                return $q->withTrashed();
75
            });
76
        }
77
78
        $this->setHtmlAttribute('class', 'table table-striped');
79
    }
80
81
    /**
82
     * @return null|string
83
     */
84
    public function getNewEntryButtonText()
85
    {
86
        if (is_null($this->newEntryButtonText)) {
87
            $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 object<Illuminate\Contra...Translation\Translator>. However, the property $newEntryButtonText is declared as type string|null. 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...
88
        }
89
90
        return $this->newEntryButtonText;
91
    }
92
93
    /**
94
     * @param string $newEntryButtonText
95
     *
96
     * @return $this
97
     */
98
    public function setNewEntryButtonText($newEntryButtonText)
99
    {
100
        $this->newEntryButtonText = $newEntryButtonText;
101
102
        return $this;
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function getParameters()
109
    {
110
        return $this->parameters;
111
    }
112
113
    /**
114
     * @param array $parameters
115
     *
116
     * @return $this
117
     */
118
    public function setParameters($parameters)
119
    {
120
        $this->parameters = $parameters;
121
122
        return $this;
123
    }
124
125
    /**
126
     * @param string $key
127
     * @param mixed  $value
128
     *
129
     * @return $this
130
     */
131
    public function setParameter($key, $value)
132
    {
133
        $this->parameters[$key] = $value;
134
135
        return $this;
136
    }
137
138
    /**
139
     * @param int    $perPage
140
     * @param string $pageName
141
     *
142
     * @return $this
143
     */
144
    public function paginate($perPage = 25, $pageName = 'page')
145
    {
146
        $this->paginate = (int) $perPage;
147
        $this->pageName = $pageName;
148
149
        return $this;
150
    }
151
152
    /**
153
     * @return $this
154
     */
155
    public function disablePagination()
156
    {
157
        $this->paginate = 0;
158
159
        return $this;
160
    }
161
162
    /**
163
     * @return bool
164
     */
165
    public function usePagination()
166
    {
167
        return $this->paginate > 0;
168
    }
169
170
    /**
171
     * @return array
172
     */
173
    public function toArray()
174
    {
175
        $model = $this->getModelConfiguration();
176
177
        $params = parent::toArray();
178
179
        $params['creatable'] = $model->isCreatable();
180
        $params['createUrl'] = $model->getCreateUrl($this->getParameters() + Request::all());
181
        $params['collection'] = $this->getCollection();
182
183
        $params['extensions'] = $this->getExtensions()->renderable()->sortByOrder();
184
        $params['newEntryButtonText'] = $this->getNewEntryButtonText();
185
186
        return $params;
187
    }
188
189
    /**
190
     * @return Collection
191
     * @throws \Exception
192
     */
193
    public function getCollection()
194
    {
195
        if (! $this->isInitialized()) {
196
            throw new \Exception('Display is not initialized');
197
        }
198
199
        if (! is_null($this->collection)) {
200
            return $this->collection;
201
        }
202
203
        $query = $this->getRepository()->getQuery();
204
205
        $this->modifyQuery($query);
206
207
        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 array<integer,object<Ill...base\Eloquent\Builder>> or object<Illuminate\Contra...n\LengthAwarePaginator>. However, the property $collection is declared as type object<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...
Bug Best Practice introduced by
The return type of return $this->collection...Name)) : $query->get(); (Illuminate\Database\Eloq...on\LengthAwarePaginator) is incompatible with the return type documented by SleepingOwl\Admin\Displa...layTable::getCollection of type Illuminate\Support\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
208
            ? $query->paginate($this->paginate, ['*'], $this->pageName)->appends(request()->except($this->pageName))
209
            : $query->get();
210
    }
211
212
    /**
213
     * @param \Illuminate\Database\Eloquent\Builder|Builder $query
214
     */
215
    protected function modifyQuery(\Illuminate\Database\Eloquent\Builder $query)
216
    {
217
        $this->extensions->modifyQuery($query);
218
    }
219
}
220