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.
Test Setup Failed
Push — master ( fd45eb...f91a4c )
by Dave
35:25 queued 16:23
created

NamedColumn::setSmall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SleepingOwl\Admin\Display\Column;
4
5
use Closure;
6
use Illuminate\Support\Arr;
7
use Illuminate\Database\Eloquent\Model;
8
use SleepingOwl\Admin\Display\TableColumn;
9
use Illuminate\Database\Eloquent\Collection;
10
use Illuminate\Support\Collection as SuportCollection;
11
use SleepingOwl\Admin\Contracts\Display\NamedColumnInterface;
12
use SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface;
13
14
abstract class NamedColumn extends TableColumn implements NamedColumnInterface
15
{
16
    /**
17
     * Column field name.
18
     * @var string
19
     */
20
    protected $name;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $orderable = true;
26
27
    /**
28
     * @param Closure|null|string $name
29
     * @param null|string $label
30
     */
31 21
    public function __construct($name, $label = null, $small = null)
32
    {
33 21
        parent::__construct($label);
34 21
        $this->setName($name);
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 31 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...
35
        $this->setSmall($small);
36 21
37
        $this->setHtmlAttribute('class', 'row-'.strtolower(class_basename(get_called_class())));
38 21
39 15
        if ($this->orderable) {
40 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...
41 21
        }
42
    }
43
44
    /**
45
     * @return string
46 17
     */
47
    public function getName()
48 17
    {
49
        return $this->name;
50
    }
51
52
    /**
53
     * @param string $name
54
     *
55
     * @return $this
56 21
     */
57
    public function setName($name)
58 21
    {
59
        $this->name = $name;
60 21
61
        return $this;
62
    }
63
64
    /**
65
     * @return string
66 7
     */
67
    public function getSmall()
68 7
    {
69
        return $this->small;
0 ignored issues
show
Bug introduced by
The property small does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
70
    }
71
72
    /**
73
     * @param string $small
74
     *
75
     * @return $this
76 15
     */
77
    public function setSmall($small)
78 15
    {
79 15
        $this->small = $small;
80 15
81 15
        return $this;
82 15
    }
83
84 15
    /**
85
     * @return mixed
86
     */
87
    public function getModelValue()
88
    {
89
        return $this->getValueFromObject($this->getModel(), $this->getName());
90
    }
91
92 1
    /**
93
     * @return mixed
94 1
     */
95 1
    public function getModelSmallValue()
96 1
    {
97
        return $this->getValueFromObject($this->getModel(), $this->getSmall());
98
    }
99
100
    /**
101
     * @param OrderByClauseInterface|bool $orderable
102
     * @deprecated
103
     * @return TableColumn
104
     */
105
    public function setOrderable($orderable = true)
106
    {
107 7
        if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) {
108
            if (! is_string($orderable) && ! $orderable instanceof Closure) {
109 7
                $orderable = $this->getName();
110
            }
111
        }
112
113
        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...
114
    }
115
116 7
    /**
117
     * Get the instance as an array.
118
     *
119
     * @return array
120
     */
121
    public function toArray()
122
    {
123
        return parent::toArray() + [
124
                'name' => $this->getName(),
125
            ];
126
    }
127
128
    /**
129
     * Get column value from instance.
130
     *
131
     * @param Collection|Model|Closure $instance
132
     * @param string $name
133 7
     *
134 7
     * @return mixed
135
     */
136 7
    protected function getValueFromObject($instance, $name)
137 1
    {
138 7
        if ($name instanceof Closure) {
139
            return $name($instance);
140
        }
141
142
        /*
143
         * Implement json parsing
144
         */
145
        if (strpos($name, '.') === false && strpos($name, '->') !== false) {
146
            $casts = collect($instance->getCasts());
0 ignored issues
show
Bug introduced by
The method getCasts does only exist in Illuminate\Database\Eloquent\Model, but not in Closure and Illuminate\D...ase\Eloquent\Collection.

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...
147 7
            $jsonParts = collect(explode('->', $name));
148 7
149 7
            $jsonAttr = $instance->{$jsonParts->first()};
150
151 7
            $cast = $casts->get($jsonParts->first(), false);
152 2
153 View Code Duplication
            if ($cast == 'object') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
                $jsonAttr = json_decode(json_encode($jsonAttr), true);
155 7
            } elseif ($cast != 'array') {
156
                $jsonAttr = json_decode($jsonAttr);
157
            }
158
159
            return Arr::get($jsonAttr, $jsonParts->slice(1)->implode('.'));
160
        }
161
162
        $parts = explode('.', $name);
163
        $part = array_shift($parts);
164
165
        if ($instance instanceof Collection) {
166
            $instance = $instance->pluck($part);
167
        } elseif ($instance instanceof SuportCollection) {
168
            $instance = $instance->first();
169
            if ($instance instanceof Collection) {
170
                $instance = $instance->pluck($part);
171
            }
172
173
            if ($instance === null) {
174
                $instance = collect();
175
            }
176
        } elseif (! is_null($instance)) {
177
            $instance = $instance->getAttribute($part);
178
        }
179
180
        if (! empty($parts) && ! is_null($instance)) {
181
            return $this->getValueFromObject($instance, implode('.', $parts));
182
        }
183
184
        return $instance;
185
    }
186
}
187