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

Columns::setView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Display\Extension;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Contracts\Support\Renderable;
7
use SleepingOwl\Admin\Display\Column\Control;
8
use SleepingOwl\Admin\Contracts\Initializable;
9
use SleepingOwl\Admin\Contracts\ColumnInterface;
10
11
class Columns extends Extension implements Initializable, Renderable
12
{
13
    use \SleepingOwl\Admin\Traits\Renderable;
14
15
    /**
16
     * @var ColumnInterface[]|Collection
17
     */
18
    protected $columns;
19
20
    /**
21
     * @var bool
22
     */
23
    protected $controlActive = true;
24
25
    /**
26
     * @var string
27
     */
28
    protected $view = 'display.extensions.columns';
29
30
    /**
31
     * @var bool
32
     */
33
    protected $initialize = false;
34
35
    /**
36
     * @var Control
37
     */
38
    protected $controlColumn;
39
40
    public function __construct()
41
    {
42
        $this->columns = new Collection();
43
44
        $this->setControlColumn(app('sleeping_owl.table.column')->control());
45
    }
46
47
    /**
48
     * @param ColumnInterface $controlColumn
49
     *
50
     * @return $this
51
     */
52
    public function setControlColumn(ColumnInterface $controlColumn)
53
    {
54
        $this->controlColumn = $controlColumn;
0 ignored issues
show
Documentation Bug introduced by
$controlColumn is of type object<SleepingOwl\Admin...tracts\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...
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return Control
61
     */
62
    public function getControlColumn()
63
    {
64
        return $this->controlColumn;
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function isControlActive()
71
    {
72
        return $this->controlActive;
73
    }
74
75
    /**
76
     * @return $this
77
     */
78
    public function enableControls()
79
    {
80
        $this->controlActive = true;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return $this
87
     */
88
    public function disableControls()
89
    {
90
        $this->controlActive = false;
91
92
        if ($this->isInitialize()) {
93
            $this->columns = $this->columns->filter(function ($column) {
94
                $class = get_class($this->getControlColumn());
95
96
                return ! ($column instanceof $class);
97
            });
98
        }
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return Collection|\SleepingOwl\Admin\Contracts\ColumnInterface[]
105
     */
106
    public function all()
107
    {
108
        return $this->columns;
109
    }
110
111
    /**
112
     * @param $columns
113
     *
114
     * @return \SleepingOwl\Admin\Contracts\DisplayInterface
115
     */
116
    public function set($columns)
117
    {
118
        if (! is_array($columns)) {
119
            $columns = func_get_args();
120
        }
121
122
        foreach ($columns as $column) {
123
            $this->push($column);
124
        }
125
126
        return $this->getDisplay();
127
    }
128
129
    /**
130
     * @param ColumnInterface $column
131
     *
132
     * @return $this
133
     */
134
    public function push(ColumnInterface $column)
135
    {
136
        $this->columns->push($column);
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return bool
143
     */
144
    public function isInitialize()
145
    {
146
        return $this->initialize;
147
    }
148
149
    /**
150
     * Get the instance as an array.
151
     *
152
     * @return array
153
     */
154
    public function toArray()
155
    {
156
        return [
157
            'columns' => $this->all(),
158
            '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\Contracts\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...
159
        ];
160
    }
161
162
    public function initialize()
163
    {
164
        $this->all()->each(function (ColumnInterface $column) {
165
            $column->initialize();
166
        });
167
168
        if ($this->isControlActive()) {
169
            $this->push($this->getControlColumn());
170
        }
171
172
        $this->initialize = true;
173
    }
174
175
    /**
176
     * Get the evaluated contents of the object.
177
     *
178
     * @return string
179
     */
180
    public function render()
181
    {
182
        $params = $this->toArray();
183
        $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\Contracts\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...
184
        $params['pagination'] = null;
185
186
        if ($params['collection'] instanceof \Illuminate\Contracts\Pagination\Paginator) {
187
            if (class_exists('Illuminate\Pagination\BootstrapThreePresenter')) {
188
                $params['pagination'] = (new \Illuminate\Pagination\BootstrapThreePresenter($params['collection']))->render();
189
            } else {
190
                $params['pagination'] = $params['collection']->render();
191
            }
192
        }
193
194
        return app('sleeping_owl.template')->view($this->getView(), $params)->render();
195
    }
196
}
197