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.
Passed
Push — master ( b2510c...ee0bb8 )
by
unknown
12:46
created

NamedColumn::getOrderCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace SleepingOwl\Admin\Display\Column;
4
5
use Closure;
6
use Illuminate\Database\Eloquent\Model;
7
use SleepingOwl\Admin\Display\TableColumn;
8
use Illuminate\Database\Eloquent\Collection;
9
use SleepingOwl\Admin\Contracts\Display\NamedColumnInterface;
10
use SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface;
11
12
abstract class NamedColumn extends TableColumn implements NamedColumnInterface
13
{
14
    /**
15
     * @var \Closure
16
     */
17
    protected $searchCallback = null;
18
19
    /**
20
     * @var \Closure
21
     */
22
    protected $orderCallback = null;
23
24
    /**
25
     * @var \Closure
26
     */
27
    protected $filterCallback = null;
28
29
    /**
30
     * @var null
31
     */
32
    protected $columMetaClass = null;
33
34
    /**
35
     * Column field name.
36
     * @var string
37
     */
38
    protected $name;
39
40
    /**
41
     * @var bool
42
     */
43
    protected $orderable = true;
44
45
    /**
46
     * @param Closure|null|string $name
47
     * @param null|string $label
48
     */
49 21
    public function __construct($name, $label = null)
50
    {
51 21
        parent::__construct($label);
52 21
        $this->setName($name);
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 49 can also be of type null or object<Closure>; however, SleepingOwl\Admin\Displa...\NamedColumn::setName() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
53
54 21
        $this->setHtmlAttribute('class', 'row-'.strtolower(class_basename(get_called_class())));
55
56 21
        if ($this->orderable) {
57 15
            $this->setOrderable();
0 ignored issues
show
Deprecated Code introduced by
The method SleepingOwl\Admin\Displa...dColumn::setOrderable() has been deprecated.

This method has been deprecated.

Loading history...
58 15
        }
59 21
    }
60
61
    /**
62
     * @return string
63
     */
64 17
    public function getName()
65
    {
66 17
        return $this->name;
67
    }
68
69
    /**
70
     * @param string $name
71
     *
72
     * @return $this
73
     */
74 21
    public function setName($name)
75
    {
76 21
        $this->name = $name;
77
78 21
        return $this;
79
    }
80
81
    /**
82
     * @param $columnMetaClass
83
     * @return $this
84
     */
85
    public function setMetaData($columnMetaClass)
86
    {
87
        $this->columMetaClass = $columnMetaClass;
88
89
        return $this;
90
    }
91
92
    /**
93
     * @return mixed
94
     */
95
    public function getMetaData()
96
    {
97
        return $this->columMetaClass
98
            ? app()->make($this->columMetaClass)
99
            : false;
100
    }
101
102
    /**
103
     * @param \Closure $callable
104
     * @return $this
105
     */
106
    public function setOrderCallback(\Closure $callable)
107
    {
108
        $this->orderCallback = $callable;
109
110
        return $this->setOrderable($callable);
0 ignored issues
show
Documentation introduced by
$callable is of type object<Closure>, but the function expects a boolean|object<SleepingO...OrderByClauseInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method SleepingOwl\Admin\Displa...dColumn::setOrderable() has been deprecated.

This method has been deprecated.

Loading history...
111
    }
112
113
    /**
114
     * @param \Closure $callable
115
     * @return $this
116
     */
117
    public function setSearchCallback(\Closure $callable)
118
    {
119
        $this->searchCallback = $callable;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param \Closure $callable
126
     * @return $this
127
     */
128
    public function setFilterCallback(\Closure $callable)
129
    {
130
        $this->filterCallback = $callable;
131
132
        return $this;
133
    }
134
135
    /**
136
     * @return \Closure
137
     */
138
    public function getOrderCallback()
139
    {
140
        return $this->orderCallback;
141
    }
142
143
    /**
144
     * @return \Closure
145
     */
146
    public function getSearchCallback()
147
    {
148
        return $this->searchCallback;
149
    }
150
151
    /**
152
     * @return \Closure
153
     */
154
    public function getFilterCallback()
155
    {
156
        return $this->filterCallback;
157
    }
158
159
    /**
160
     * @return mixed
161
     */
162 7
    public function getModelValue()
163
    {
164 7
        return $this->getValueFromObject($this->getModel(), $this->getName());
165
    }
166
167
    /**
168
     * @param OrderByClauseInterface|bool $orderable
169
     * @deprecated
170
     * @return $this
171
     */
172 15
    public function setOrderable($orderable = true)
173
    {
174 15
        if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) {
175 15
            if (! is_string($orderable) && ! $orderable instanceof Closure) {
176 15
                $orderable = $this->getName();
177 15
            }
178 15
        }
179
180 15
        return parent::setOrderable($orderable);
0 ignored issues
show
Deprecated Code introduced by
The method SleepingOwl\Admin\Displa...eColumn::setOrderable() has been deprecated.

This method has been deprecated.

Loading history...
181
    }
182
183
    /**
184
     * Get the instance as an array.
185
     *
186
     * @return array
187
     */
188 1
    public function toArray()
189
    {
190 1
        return parent::toArray() + [
191 1
                'name' => $this->getName(),
192 1
            ];
193
    }
194
195
    /**
196
     * Get column value from instance.
197
     *
198
     * @param Collection|Model|Closure $instance
199
     * @param string           $name
200
     *
201
     * @return mixed
202
     */
203 7
    protected function getValueFromObject($instance, $name)
204
    {
205 7
        if ($name instanceof Closure) {
206
            return $name($instance);
207
        }
208
209 7
        $parts = explode('.', $name);
210 7
        $part = array_shift($parts);
211
212 7
        if ($instance instanceof Collection) {
213 1
            $instance = $instance->pluck($part);
214 7
        } elseif (! is_null($instance)) {
215 7
            $instance = $instance->getAttribute($part);
0 ignored issues
show
Bug introduced by
The method getAttribute does only exist in Illuminate\Database\Eloquent\Model, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
216 7
        }
217
218 7
        if (! empty($parts) && ! is_null($instance)) {
219 2
            return $this->getValueFromObject($instance, implode('.', $parts));
220
        }
221
222 7
        return $instance;
223
    }
224
}
225