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 ( 414922...a9bc98 )
by butschster
12:35
created

NamedColumn::setOrderable()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 3
nop 1
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
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\NamedColumnInterface;
10
use SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface;
11
12
abstract class NamedColumn extends TableColumn implements NamedColumnInterface
13
{
14
    /**
15
     * Column field name.
16
     * @var string
17
     */
18
    protected $name;
19
20
    /**
21
     * @var bool
22
     */
23
    protected $orderable = true;
24
25
    /**
26
     * @param Closure|null|string $name
27
     * @param null|string $label
28
     */
29
    public function __construct($name, $label = null)
30
    {
31
        parent::__construct($label);
32
        $this->setName($name);
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 29 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...
33
34
        $this->setHtmlAttribute('class', 'row-'.strtolower(class_basename(get_called_class())));
35
36
        if ($this->orderable) {
37
            $this->setOrderable();
38
        }
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getName()
45
    {
46
        return $this->name;
47
    }
48
49
    /**
50
     * @param string $name
51
     *
52
     * @return $this
53
     */
54
    public function setName($name)
55
    {
56
        $this->name = $name;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @return mixed
63
     */
64
    public function getModelValue()
65
    {
66
        return $this->getValueFromObject($this->getModel(), $this->getName());
67
    }
68
69
    /**
70
     * @param OrderByClauseInterface|bool $orderable
71
     *
72
     * @return $this
73
     */
74
    public function setOrderable($orderable = true)
75
    {
76
        if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) {
77
            if (! is_string($orderable) && ! $orderable instanceof Closure) {
78
                $orderable = $this->getName();
79
            }
80
81
            $orderable = new OrderByClause($orderable);
82
        }
83
84
        $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...
85
        $this->getHeader()->setOrderable($this->isOrderable());
86
87
        return $this;
88
    }
89
90
    /**
91
     * Get the instance as an array.
92
     *
93
     * @return array
94
     */
95
    public function toArray()
96
    {
97
        return parent::toArray() + [
98
            'name' => $this->getName(),
99
        ];
100
    }
101
102
    /**
103
     * Get column value from instance.
104
     *
105
     * @param Collection|Model|Closure $instance
106
     * @param string           $name
107
     *
108
     * @return mixed
109
     */
110
    protected function getValueFromObject($instance, $name)
111
    {
112
        if ($name instanceof Closure) {
113
            return $name($instance);
114
        }
115
116
        $parts = explode('.', $name);
117
        $part = array_shift($parts);
118
119
        if ($instance instanceof Collection) {
120
            $instance = $instance->pluck($part);
121
        } elseif (! is_null($instance)) {
122
            $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...
123
        }
124
125
        if (! empty($parts) && ! is_null($instance)) {
126
            return $this->getValueFromObject($instance, implode('.', $parts));
127
        }
128
129
        return $instance;
130
    }
131
}
132