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 — analysis-z4nAQe ( c1e622 )
by butschster
10:30
created

NamedColumn::setIsolated()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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
     * @var bool
25
     */
26
    protected $small;
27
    protected $smallString = false;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $orderable = true;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $isolated = true;
38
39
    /**
40
     * @var bool
41
     */
42
    protected $isSearchable = true;
43
44
    /**
45
     * NamedColumn constructor.
46
     * @param $name
47
     * @param $label string
48
     * @param $small string
49
     */
50
    public function __construct($name, $label = null, $small = null)
51
    {
52
        parent::__construct($label);
53
        $this->setName($name);
54
        $this->setSmall($small);
55
56
        $this->setHtmlAttribute('class', 'row-'.strtolower(class_basename(get_called_class())));
57
58
        if ($this->orderable) {
59
            $this->setOrderable();
60
        }
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getName()
67
    {
68
        return $this->name;
69
    }
70
71
    /**
72
     * @param string $name
73
     *
74
     * @return $this
75
     */
76
    public function setName($name)
77
    {
78
        $this->name = $name;
79
80
        return $this;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getSmall()
87
    {
88
        if ($this->smallString) {
89
            return $this->small;
90
        }
91
92
        return $this->getValueFromObject($this->getModel(), $this->small);
93
    }
94
95
    /**
96
     * @param string $small
97
     *
98
     * @return $this
99
     */
100
    public function setSmall($small, $smallString = false)
101
    {
102
        $this->small = $small;
103
        $this->smallString = $smallString;
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return mixed
110
     */
111
    public function getModelValue()
112
    {
113
        return $this->getValueFromObject($this->getModel(), $this->getName());
114
    }
115
116
    /**
117
     * @param OrderByClauseInterface|bool $orderable
118
     * @return TableColumn
119
     */
120
    public function setOrderable($orderable = true)
121
    {
122
        if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) {
123
            if (! is_string($orderable) && ! $orderable instanceof Closure) {
0 ignored issues
show
introduced by
$orderable is never a sub-type of Closure.
Loading history...
introduced by
The condition is_string($orderable) is always false.
Loading history...
124
                $orderable = $this->getName();
125
            }
126
        }
127
128
        return parent::setOrderable($orderable);
129
    }
130
131
    /**
132
     * @param bool $isolated
133
     *
134
     * @return $this
135
     */
136
    public function setIsolated($isolated)
137
    {
138
        $this->isolated = $isolated;
139
140
        return $this;
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function getIsolated()
147
    {
148
        return $this->isolated;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->isolated returns the type boolean which is incompatible with the documented return type string.
Loading history...
149
    }
150
151
    /**
152
     * Get the instance as an array.
153
     *
154
     * @return array
155
     */
156
    public function toArray()
157
    {
158
        $model_value_small = $this->getSmall();
159
        if ($this->isolated) {
160
            $model_value_small = htmlspecialchars($model_value_small);
161
        }
162
163
        return parent::toArray() + [
164
            'name' => $this->getName(),
165
            'small' => $model_value_small,
166
        ];
167
    }
168
169
    /**
170
     * Get column value from instance.
171
     *
172
     * @param Collection|Model|Closure $instance
173
     * @param string $name
174
     *
175
     * @return mixed
176
     */
177
    protected function getValueFromObject($instance, $name)
178
    {
179
        if ($name instanceof Closure) {
0 ignored issues
show
introduced by
$name is never a sub-type of Closure.
Loading history...
180
            return $name($instance);
181
        }
182
183
        /*
184
         * Implement json parsing
185
         */
186
        if (strpos($name, '.') === false && strpos($name, '->') !== false) {
187
            $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

187
            $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...
188
            $jsonParts = collect(explode('->', $name));
189
190
            $jsonAttr = $instance->{$jsonParts->first()};
191
192
            $cast = $casts->get($jsonParts->first(), false);
193
194
            if ($cast == 'object') {
195
                $jsonAttr = json_decode(json_encode($jsonAttr), true);
196
            } elseif ($cast != 'array') {
197
                $jsonAttr = json_decode($jsonAttr);
198
            }
199
200
            return Arr::get($jsonAttr, $jsonParts->slice(1)->implode('.'));
201
        }
202
203
        $parts = explode('.', $name);
204
        $part = array_shift($parts);
205
206
        if ($instance instanceof Collection) {
207
            $instance = $instance->pluck($part);
208
        } elseif ($instance instanceof SuportCollection) {
0 ignored issues
show
introduced by
$instance is never a sub-type of Illuminate\Support\Collection.
Loading history...
209
            $instance = $instance->first();
210
            if ($instance instanceof Collection) {
211
                $instance = $instance->pluck($part);
212
            }
213
214
            if ($instance === null) {
215
                $instance = collect();
216
            }
217
        } elseif (! is_null($instance)) {
218
            $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

218
            /** @scrutinizer ignore-call */ 
219
            $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...
219
        }
220
221
        if (! empty($parts) && ! is_null($instance)) {
222
            return $this->getValueFromObject($instance, implode('.', $parts));
223
        }
224
225
        return $instance;
226
    }
227
}
228