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 ( d2dcf4...7dc89d )
by butschster
06:01
created

DisplayTable::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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\Contracts\Support\Renderable;
8
use SleepingOwl\Admin\Contracts\ColumnInterface;
9
use SleepingOwl\Admin\Display\Extension\Columns;
10
use SleepingOwl\Admin\Display\Extension\ColumnFilters;
11
use SleepingOwl\Admin\Contracts\ColumnFilterInterface;
12
use SleepingOwl\Admin\Contracts\Display\DisplayExtensionInterface;
13
14
/**
15
 * Class DisplayTable.
16
17
 * @method Columns getColumns()
18
 * @method $this setColumns(ColumnInterface $column, ... $columns)
19
 *
20
 * @method ColumnFilters getColumnFilters()
21
 * @method $this setColumnFilters(ColumnFilterInterface $filters = null, ...$filters)
22
 */
23
class DisplayTable extends Display
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $view = 'display.table';
29
30
    /**
31
     * @var array
32
     */
33
    protected $parameters = [];
34
35
    /**
36
     * @var int|null
37
     */
38
    protected $paginate = 25;
39
40
    /**
41
     * @var string
42
     */
43
    protected $pageName = 'page';
44
45
    /**
46
     * @var Collection
47
     */
48
    protected $collection;
49
50
    /**
51
     * @var string|null
52
     */
53
    protected $newEntryButtonText;
54
55
    /**
56
     * Display constructor.
57
     */
58
    public function __construct()
59
    {
60
        parent::__construct();
61
62
        $this->extend('columns', new Columns());
63
        $this->extend('column_filters', new ColumnFilters());
64
    }
65
66
    /**
67
     * Initialize display.
68
     */
69
    public function initialize()
70
    {
71
        parent::initialize();
72
73
        if ($this->getModelConfiguration()->isRestorableModel()) {
74
            $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...
75
                return $q->withTrashed();
76
            });
77
        }
78
79
        $this->setHtmlAttribute('class', 'table table-striped');
80
    }
81
82
    /**
83
     * @return null|string
84
     */
85
    public function getNewEntryButtonText()
86
    {
87
        if (is_null($this->newEntryButtonText)) {
88
            $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<Symfony\Component...on\TranslatorInterface>. 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...
89
        }
90
91
92
        return $this->newEntryButtonText;
93
    }
94
95
    /**
96
     * @param string $newEntryButtonText
97
     *
98
     * @return $this
99
     */
100
    public function setNewEntryButtonText($newEntryButtonText)
101
    {
102
        $this->newEntryButtonText = $newEntryButtonText;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @return array
109
     */
110
    public function getParameters()
111
    {
112
        return $this->parameters;
113
    }
114
115
    /**
116
     * @param array $parameters
117
     *
118
     * @return $this
119
     */
120
    public function setParameters($parameters)
121
    {
122
        $this->parameters = $parameters;
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param string $key
129
     * @param mixed  $value
130
     *
131
     * @return $this
132
     */
133
    public function setParameter($key, $value)
134
    {
135
        $this->parameters[$key] = $value;
136
137
        return $this;
138
    }
139
140
    /**
141
     * @param int    $perPage
142
     * @param string $pageName
143
     *
144
     * @return $this
145
     */
146
    public function paginate($perPage = 25, $pageName = 'page')
147
    {
148
        $this->paginate = (int) $perPage;
149
        $this->pageName = $pageName;
150
151
        return $this;
152
    }
153
154
    /**
155
     * @return $this
156
     */
157
    public function disablePagination()
158
    {
159
        $this->paginate = 0;
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    public function usePagination()
168
    {
169
        return $this->paginate > 0;
170
    }
171
172
    /**
173
     * @return array
174
     */
175
    public function toArray()
176
    {
177
        $model = $this->getModelConfiguration();
178
179
        $params = parent::toArray();
180
181
        $params['creatable'] = $model->isCreatable();
182
        $params['createUrl'] = $model->getCreateUrl($this->getParameters() + Request::all());
183
        $params['collection'] = $this->getCollection();
184
185
        $params['extensions'] = $this->getExtensions()
186
            ->filter(function (DisplayExtensionInterface $ext) {
187
                return $ext instanceof Renderable;
188
            })
189
            ->sortBy(function (DisplayExtensionInterface $extension) {
190
                return $extension->getOrder();
191
            });
192
193
        $params['newEntryButtonText'] = $this->getNewEntryButtonText();
194
195
        return $params;
196
    }
197
198
    /**
199
     * @return Collection
200
     * @throws \Exception
201
     */
202
    public function getCollection()
203
    {
204
        if (! $this->isInitialized()) {
205
            throw new \Exception('Display is not initialized');
206
        }
207
208
        if (! is_null($this->collection)) {
209
            return $this->collection;
210
        }
211
212
        $query = $this->getRepository()->getQuery();
213
214
        $this->modifyQuery($query);
215
216
        return $this->collection = $this->usePagination()
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->usePagination() ?...geName) : $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...eName) : $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...
217
            ? $query->paginate($this->paginate, ['*'], $this->pageName)
218
            : $query->get();
219
    }
220
221
    /**
222
     * @param \Illuminate\Database\Eloquent\Builder|Builder $query
223
     */
224
    protected function modifyQuery(\Illuminate\Database\Eloquent\Builder $query)
225
    {
226
        $this->extensions->each(function (DisplayExtensionInterface $extension) use ($query) {
227
            $extension->modifyQuery($query);
228
        });
229
    }
230
}
231