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 — development-bs4 ( bc8456...b9684a )
by butschster
29:24 queued 19:50
created

NamedColumn::getModelSmallValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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
     * @var bool
25
     */
26
    protected $small;
27
    protected $smallString = false;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $orderable = true;
33
34
    /**
35
     * NamedColumn constructor.
36
     * @param $name
37
     * @param $label string
38
     * @param $small string
39
     */
40
    public function __construct($name, $label = null, $small = null)
41
    {
42
        parent::__construct($label);
43
        $this->setName($name);
44
        $this->setSmall($small);
45
46
        $this->setHtmlAttribute('class', 'row-'.strtolower(class_basename(get_called_class())));
47
48
        if ($this->orderable) {
49
            $this->setOrderable();
50
        }
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getName()
57
    {
58
        return $this->name;
59
    }
60
61
    /**
62
     * @param string $name
63
     *
64
     * @return $this
65
     */
66
    public function setName($name)
67
    {
68
        $this->name = $name;
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getSmall()
77
    {
78
        if ($this->smallString) {
79
            return $this->small;
80
        }
81
82
        return $this->getValueFromObject($this->getModel(), $this->small);
83
    }
84
85
    /**
86
     * @param string $small
87
     *
88
     * @return $this
89
     */
90
    public function setSmall($small, $smallString = false)
91
    {
92
        $this->small = $small;
93
        $this->smallString = $smallString;
94
95
        return $this;
96
    }
97
98
    /**
99
     * @return mixed
100
     */
101
    public function getModelValue()
102
    {
103
        return $this->getValueFromObject($this->getModel(), $this->getName());
104
    }
105
106
    /**
107
     * @param OrderByClauseInterface|bool $orderable
108
     * @return TableColumn
109
     */
110
    public function setOrderable($orderable = true)
111
    {
112
        if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) {
113
            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...
114
                $orderable = $this->getName();
115
            }
116
        }
117
118
        return parent::setOrderable($orderable);
119
    }
120
121
    /**
122
     * Get the instance as an array.
123
     *
124
     * @return array
125
     */
126
    public function toArray()
127
    {
128
        return parent::toArray() + [
129
            'name' => $this->getName(),
130
            'small' => htmlspecialchars($this->getSmall()),
131
        ];
132
    }
133
134
    /**
135
     * Get column value from instance.
136
     *
137
     * @param Collection|Model|Closure $instance
138
     * @param string $name
139
     *
140
     * @return mixed
141
     */
142
    protected function getValueFromObject($instance, $name)
143
    {
144
        if ($name instanceof Closure) {
0 ignored issues
show
introduced by
$name is never a sub-type of Closure.
Loading history...
145
            return $name($instance);
146
        }
147
148
        /*
149
         * Implement json parsing
150
         */
151
        if (strpos($name, '.') === false && strpos($name, '->') !== false) {
152
            $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

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

183
            /** @scrutinizer ignore-call */ 
184
            $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...
184
        }
185
186
        if (! empty($parts) && ! is_null($instance)) {
187
            return $this->getValueFromObject($instance, implode('.', $parts));
188
        }
189
190
        return $instance;
191
    }
192
}
193