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

Columns::modifyQuery()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 8
nop 1
dl 0
loc 25
ccs 0
cts 19
cp 0
crap 56
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Display\Extension;
4
5
use Request;
6
use Illuminate\Support\Collection;
7
use Illuminate\Contracts\Support\Renderable;
8
use SleepingOwl\Admin\Display\Column\Control;
9
use SleepingOwl\Admin\Contracts\Initializable;
10
use SleepingOwl\Admin\Contracts\Display\ColumnInterface;
11
12
class Columns extends Extension implements Initializable, Renderable
13
{
14
    use \SleepingOwl\Admin\Traits\Renderable;
15
16
    /**
17
     * @var ColumnInterface[]|Collection
18
     */
19
    protected $columns;
20
21
    /**
22
     * @var bool
23
     */
24
    protected $controlActive = true;
25
26
    /**
27
     * @var string
28
     */
29
    protected $view = 'display.extensions.columns';
30
31
    /**
32
     * @var bool
33
     */
34
    protected $initialize = false;
35
36
    /**
37
     * @var Control
38
     */
39
    protected $controlColumn;
40
41
    public function __construct()
42
    {
43
        $this->columns = new Collection();
44
45
        $this->setControlColumn(app('sleeping_owl.table.column')->control());
46
    }
47
48
    /**
49
     * @param ColumnInterface $controlColumn
50
     *
51
     * @return $this
52
     */
53
    public function setControlColumn(ColumnInterface $controlColumn)
54
    {
55
        $this->controlColumn = $controlColumn;
0 ignored issues
show
Documentation Bug introduced by
$controlColumn is of type object<SleepingOwl\Admin...isplay\ColumnInterface>, but the property $controlColumn was declared to be of type object<SleepingOwl\Admin\Display\Column\Control>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
56
57
        return $this;
58
    }
59
60
    /**
61
     * @return Control
62
     */
63
    public function getControlColumn()
64
    {
65
        return $this->controlColumn;
66
    }
67
68
    /**
69
     * @return bool
70
     */
71
    public function isControlActive()
72
    {
73
        return $this->controlActive;
74
    }
75
76
    /**
77
     * @return $this
78
     */
79
    public function enableControls()
80
    {
81
        $this->controlActive = true;
82
83
        return $this;
84
    }
85
86
    /**
87
     * @return $this
88
     */
89
    public function disableControls()
90
    {
91
        $this->controlActive = false;
92
93
        if ($this->isInitialize()) {
94
            $this->columns = $this->columns->filter(function ($column) {
95
                $class = get_class($this->getControlColumn());
96
97
                return ! ($column instanceof $class);
98
            });
99
        }
100
101
        return $this;
102
    }
103
104
    /**
105
     * @return Collection|\SleepingOwl\Admin\Contracts\Display\ColumnInterface[]
106
     */
107
    public function all()
108
    {
109
        return $this->columns;
110
    }
111
112
    /**
113
     * @param $columns
114
     *
115
     * @return \SleepingOwl\Admin\Contracts\Display\DisplayInterface
116
     */
117
    public function set($columns)
118
    {
119
        if (! is_array($columns)) {
120
            $columns = func_get_args();
121
        }
122
123
        foreach ($columns as $column) {
124
            $this->push($column);
125
        }
126
127
        return $this->getDisplay();
128
    }
129
130
    /**
131
     * @param ColumnInterface $column
132
     *
133
     * @return $this
134
     */
135
    public function push(ColumnInterface $column)
136
    {
137
        $this->columns->push($column);
138
139
        return $this;
140
    }
141
142
    /**
143
     * @return bool
144
     */
145
    public function isInitialize()
146
    {
147
        return $this->initialize;
148
    }
149
150
    /**
151
     * Get the instance as an array.
152
     *
153
     * @return array
154
     */
155
    public function toArray()
156
    {
157
        return [
158
            'columns' => $this->all(),
159
            'attributes' => $this->getDisplay()->htmlAttributesToString(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SleepingOwl\Admin\Contra...isplay\DisplayInterface as the method htmlAttributesToString() does only exist in the following implementations of said interface: SleepingOwl\Admin\Display\Display, SleepingOwl\Admin\Display\DisplayDatatables, SleepingOwl\Admin\Display\DisplayDatatablesAsync, SleepingOwl\Admin\Display\DisplayTable, SleepingOwl\Admin\Display\DisplayTree, SleepingOwl\Admin\Form\FormDefault, SleepingOwl\Admin\Form\FormPanel, SleepingOwl\Admin\Form\FormTabbed.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
160
        ];
161
    }
162
163
    public function initialize()
164
    {
165
        $this->all()->each(function (ColumnInterface $column) {
166
            $column->initialize();
167
        });
168
169
        if ($this->isControlActive()) {
170
            $this->push($this->getControlColumn());
171
        }
172
173
        $this->initialize = true;
174
    }
175
176
    /**
177
     * Get the evaluated contents of the object.
178
     *
179
     * @return string
180
     */
181
    public function render()
182
    {
183
        $params = $this->toArray();
184
        $params['collection'] = $this->getDisplay()->getCollection();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SleepingOwl\Admin\Contra...isplay\DisplayInterface as the method getCollection() does only exist in the following implementations of said interface: SleepingOwl\Admin\Display\DisplayDatatables, SleepingOwl\Admin\Display\DisplayDatatablesAsync, SleepingOwl\Admin\Display\DisplayTable, SleepingOwl\Admin\Display\DisplayTree.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
185
        $params['pagination'] = null;
186
187
        if ($params['collection'] instanceof \Illuminate\Contracts\Pagination\Paginator) {
188
            if (class_exists('Illuminate\Pagination\BootstrapThreePresenter')) {
189
                $params['pagination'] = (new \Illuminate\Pagination\BootstrapThreePresenter($params['collection']))->render();
190
            } else {
191
                $params['pagination'] = $params['collection']->render();
192
            }
193
        }
194
195
        return app('sleeping_owl.template')->view($this->getView(), $params)->render();
196
    }
197
198
    /**
199
     * @param \Illuminate\Database\Eloquent\Builder $query
200
     */
201
    public function modifyQuery(\Illuminate\Database\Eloquent\Builder $query)
202
    {
203
        $orders = Request::input('order', []);
204
205
        $columns = $this->all();
206
207
        if (! is_int(key($orders))) {
208
            $orders = [$orders];
209
        }
210
211
        foreach ($orders as $order) {
212
            $columnIndex = array_get($order, 'column');
213
            $direction = array_get($order, 'dir', 'asc');
214
215
            if (! $columnIndex && $columnIndex !== '0') {
216
                continue;
217
            }
218
219
            $column = $columns->get($columnIndex);
220
221
            if ($column instanceof ColumnInterface && $column->isOrderable()) {
222
                $column->orderBy($query, $direction);
223
            }
224
        }
225
    }
226
}
227