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 — analysis-qJKbya ( 9a319d )
by butschster
08:06 queued 17s
created

NamedColumn::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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 string
24
     */
25
    protected $small;
26
27
    /**
28
     * @var bool
29
     */
30
    protected $orderable = true;
31
32
    /**
33
     * NamedColumn constructor.
34
     * @param $name
35
     * @param $label string
36
     * @param $small string
37
     */
38
    public function __construct($name, $label = null, $small = null)
39
    {
40
        parent::__construct($label);
41
        $this->setName($name);
42
        $this->setSmall($small);
43
44
        $this->setHtmlAttribute('class', 'row-'.strtolower(class_basename(get_called_class())));
45
46
        if ($this->orderable) {
47
            $this->setOrderable();
0 ignored issues
show
Deprecated Code introduced by
The function SleepingOwl\Admin\Displa...dColumn::setOrderable() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

47
            /** @scrutinizer ignore-deprecated */ $this->setOrderable();
Loading history...
48
        }
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getName()
55
    {
56
        return $this->name;
57
    }
58
59
    /**
60
     * @param string $name
61
     *
62
     * @return $this
63
     */
64
    public function setName($name)
65
    {
66
        $this->name = $name;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getSmall()
75
    {
76
        if ($this->getValueFromObject($this->getModel(), $this->small)) {
77
            return $this->getValueFromObject($this->getModel(), $this->small);
78
        }
79
80
        return $this->small;
81
    }
82
83
    /**
84
     * @param string $small
85
     *
86
     * @return $this
87
     */
88
    public function setSmall($small)
89
    {
90
        $this->small = $small;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @return mixed
97
     */
98
    public function getModelValue()
99
    {
100
        return $this->getValueFromObject($this->getModel(), $this->getName());
101
    }
102
103
    /**
104
     * @param OrderByClauseInterface|bool $orderable
105
     * @return TableColumn
106
     * @deprecated
107
     */
108
    public function setOrderable($orderable = true)
109
    {
110
        if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) {
111
            if (! is_string($orderable) && ! $orderable instanceof Closure) {
0 ignored issues
show
introduced by
The condition is_string($orderable) is always false.
Loading history...
introduced by
$orderable is never a sub-type of Closure.
Loading history...
112
                $orderable = $this->getName();
113
            }
114
        }
115
116
        return parent::setOrderable($orderable);
117
    }
118
119
    /**
120
     * Get the instance as an array.
121
     *
122
     * @return array
123
     */
124
    public function toArray()
125
    {
126
        return parent::toArray() + [
127
            'name' => $this->getName(),
128
            'small' => htmlspecialchars($this->getSmall()),
129
        ];
130
    }
131
132
    /**
133
     * Get column value from instance.
134
     *
135
     * @param Collection|Model|Closure $instance
136
     * @param string $name
137
     *
138
     * @return mixed
139
     */
140
    protected function getValueFromObject($instance, $name)
141
    {
142
        if ($name instanceof Closure) {
0 ignored issues
show
introduced by
$name is never a sub-type of Closure.
Loading history...
143
            return $name($instance);
144
        }
145
146
        /*
147
         * Implement json parsing
148
         */
149
        if (strpos($name, '.') === false && strpos($name, '->') !== false) {
150
            $casts = collect($instance->getCasts());
0 ignored issues
show
Bug introduced by
The method getCasts() does not exist on Closure. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
            $casts = collect($instance->/** @scrutinizer ignore-call */ getCasts());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
151
            $jsonParts = collect(explode('->', $name));
152
153
            $jsonAttr = $instance->{$jsonParts->first()};
154
155
            $cast = $casts->get($jsonParts->first(), false);
156
157
            if ($cast == 'object') {
158
                $jsonAttr = json_decode(json_encode($jsonAttr), true);
159
            } elseif ($cast != 'array') {
160
                $jsonAttr = json_decode($jsonAttr);
161
            }
162
163
            return Arr::get($jsonAttr, $jsonParts->slice(1)->implode('.'));
164
        }
165
166
        $parts = explode('.', $name);
167
        $part = array_shift($parts);
168
169
        if ($instance instanceof Collection) {
170
            $instance = $instance->pluck($part);
171
        } elseif ($instance instanceof SuportCollection) {
0 ignored issues
show
introduced by
$instance is never a sub-type of Illuminate\Support\Collection.
Loading history...
172
            $instance = $instance->first();
173
            if ($instance instanceof Collection) {
174
                $instance = $instance->pluck($part);
175
            }
176
177
            if ($instance === null) {
178
                $instance = collect();
179
            }
180
        } elseif (! is_null($instance)) {
181
            $instance = $instance->getAttribute($part);
0 ignored issues
show
Bug introduced by
The method getAttribute() does not exist on Closure. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

181
            /** @scrutinizer ignore-call */ 
182
            $instance = $instance->getAttribute($part);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
182
        }
183
184
        if (! empty($parts) && ! is_null($instance)) {
185
            return $this->getValueFromObject($instance, implode('.', $parts));
186
        }
187
188
        return $instance;
189
    }
190
}
191