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
Branch development (1ea943)
by butschster
05:38
created

TableColumn::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Display;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use KodiComponents\Support\HtmlAttributes;
8
use SleepingOwl\Admin\Contracts\ColumnInterface;
9
use SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface;
10
use SleepingOwl\Admin\Contracts\Display\TableHeaderColumnInterface;
11
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
12
use SleepingOwl\Admin\Contracts\WithModel;
13
use SleepingOwl\Admin\Display\Column\OrderByClause;
14
use SleepingOwl\Admin\Traits\Assets;
15
use SleepingOwl\Admin\Traits\Renderable;
16
17
abstract class TableColumn implements ColumnInterface
18
{
19
    use HtmlAttributes, Assets, Renderable;
20
21
    /**
22
     * Column header.
23
     *
24
     * @var TableHeaderColumnInterface
25
     */
26
    protected $header;
27
28
    /**
29
     * Model instance currently rendering.
30
     *
31
     * @var Model
32
     */
33
    protected $model;
34
35
    /**
36
     * Column appendant.
37
     *
38
     * @var ColumnInterface
39
     */
40
    protected $append;
41
42
    /**
43
     * Column width.
44
     *
45
     * @var string
46
     */
47
    protected $width = null;
48
49
    /**
50
     * @var OrderByClauseInterface
51
     */
52
    protected $orderByClause;
53
54
    /**
55
     * TableColumn constructor.
56
     *
57
     * @param string|null $label
58
     */
59
    public function __construct($label = null)
60
    {
61
        $this->header = app(TableHeaderColumnInterface::class);
62
63
        if (! is_null($label)) {
64
            $this->setLabel($label);
65
        }
66
67
        $this->initializePackage();
68
    }
69
70
    /**
71
     * Initialize column.
72
     */
73
    public function initialize()
74
    {
75
        $this->includePackage();
76
    }
77
78
    /**
79
     * @return TableHeaderColumnInterface
80
     */
81
    public function getHeader()
82
    {
83
        return $this->header;
84
    }
85
86
    /**
87
     * @return int
88
     */
89
    public function getWidth()
90
    {
91
        return $this->width;
92
    }
93
94
    /**
95
     * @param int|string $width
96
     *
97
     * @return $this
98
     */
99
    public function setWidth($width)
100
    {
101
        if (is_int($width)) {
102
            $width = $width.'px';
103
        }
104
105
        $this->width = $width;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return ColumnInterface
112
     */
113
    public function getAppends()
114
    {
115
        return $this->append;
116
    }
117
118
    /**
119
     * @param ColumnInterface $append
120
     *
121
     * @return $this
122
     */
123
    public function append(ColumnInterface $append)
124
    {
125
        $this->append = $append;
126
127
        return $this;
128
    }
129
130
    /**
131
     * @return Model $model
132
     */
133
    public function getModel()
134
    {
135
        return $this->model;
136
    }
137
138
    /**
139
     * @param Model $model
140
     *
141
     * @return $this
142
     */
143
    public function setModel(Model $model)
144
    {
145
        $this->model = $model;
146
        $append = $this->getAppends();
147
148
        if ($append instanceof WithModel) {
149
            $append->setModel($model);
150
        }
151
152
        return $this;
153
    }
154
155
    /**
156
     * Set column header label.
157
     *
158
     * @param string $title
159
     *
160
     * @return $this
161
     */
162
    public function setLabel($title)
163
    {
164
        $this->getHeader()->setTitle($title);
165
166
        return $this;
167
    }
168
169
    /**
170
     * @param OrderByClauseInterface|bool|string|\Closure $orderable
171
     *
172
     * @return $this
173
     */
174
    public function setOrderable($orderable)
175
    {
176
        if ($orderable instanceof \Closure || is_string($orderable)) {
177
            $orderable = new OrderByClause($orderable);
0 ignored issues
show
Bug introduced by
It seems like $orderable defined by new \SleepingOwl\Admin\D...derByClause($orderable) on line 177 can also be of type object<Closure>; however, SleepingOwl\Admin\Displa...ByClause::__construct() does only seem to accept string|object<Mockery\Matcher\Closure>, 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...
178
        }
179
180
        if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) {
181
            throw new \InvalidArgumentException('Argument must be instance of SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface interface');
182
        }
183
184
        $this->orderByClause = $orderable;
0 ignored issues
show
Documentation Bug introduced by
It seems like $orderable can also be of type false. However, the property $orderByClause is declared as type object<SleepingOwl\Admin...OrderByClauseInterface>. 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...
185
        $this->getHeader()->setOrderable($this->isOrderable());
186
187
        return $this;
188
    }
189
190
    /**
191
     * Check if column is orderable.
192
     * @return bool
193
     */
194
    public function isOrderable()
195
    {
196
        return $this->orderByClause instanceof OrderByClauseInterface;
197
    }
198
199
    /**
200
     * @param Builder $query
201
     * @param string $direction
202
     *
203
     * @return $this
204
     */
205
    public function orderBy(Builder $query, $direction)
206
    {
207
        if (! $this->isOrderable()) {
208
            throw new \InvalidArgumentException('Argument must be instance of SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface interface');
209
        }
210
211
        $this->orderByClause->modifyQuery($query, $direction);
212
213
        return $this;
214
    }
215
216
    /**
217
     * Get the instance as an array.
218
     *
219
     * @return array
220
     */
221
    public function toArray()
222
    {
223
        return [
224
            'attributes' => $this->htmlAttributesToString(),
225
            'model'      => $this->getModel(),
226
            'append' => $this->getAppends(),
227
        ];
228
    }
229
230
    /**
231
     * Get related model configuration.
232
     * @return ModelConfigurationInterface
233
     */
234
    protected function getModelConfiguration()
235
    {
236
        return app('sleeping_owl')->getModel(get_class($this->getModel()));
237
    }
238
}
239