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-m4VopE ( 4da4ca )
by butschster
12:05
created

NamedColumn::getIsolated()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SleepingOwl\Admin\Display\Column;
4
5
use Closure;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Collection as SuportCollection;
10
use SleepingOwl\Admin\Contracts\Display\NamedColumnInterface;
11
use SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface;
12
use SleepingOwl\Admin\Display\TableColumn;
13
use SleepingOwl\Admin\Traits\SmallDisplay;
14
use SleepingOwl\Admin\Traits\Visibled;
15
16
abstract class NamedColumn extends TableColumn implements NamedColumnInterface
17
{
18
    use SmallDisplay, Visibled;
19
20
    /**
21
     * Column field name.
22
     * @var string
23
     */
24
    protected $name;
25
26
    /**
27
     * @var bool
28
     */
29
    protected $orderable = true;
30
31
    /**
32
     * @var bool
33
     */
34
    protected $isSearchable = true;
35
36
    /**
37
     * NamedColumn constructor.
38
     * @param $name
39
     * @param $label string
40
     * @param $small string
41
     */
42
    public function __construct($name, $label = null, $small = null)
43
    {
44
        parent::__construct($label);
45
        $this->setName($name);
46
        $this->setSmall($small);
47
48
        $this->setHtmlAttribute('class', 'row-'.strtolower(class_basename(get_called_class())));
49
50
        if ($this->orderable) {
51
            $this->setOrderable();
52
        }
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getName()
59
    {
60
        return $this->name;
61
    }
62
63
    /**
64
     * @param string $name
65
     *
66
     * @return $this
67
     */
68
    public function setName($name)
69
    {
70
        $this->name = $name;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @return mixed
77
     */
78
    public function getModelValue()
79
    {
80
        return $this->getValueFromObject($this->getModel(), $this->getName());
81
    }
82
83
    /**
84
     * @param OrderByClauseInterface|bool $orderable
85
     * @return TableColumn
86
     */
87
    public function setOrderable($orderable = true)
88
    {
89
        if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) {
90
            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...
91
                $orderable = $this->getName();
92
            }
93
        }
94
95
        return parent::setOrderable($orderable);
96
    }
97
98
    /**
99
     * Get the instance as an array.
100
     *
101
     * @return array
102
     */
103
    public function toArray()
104
    {
105
        $model_value_small = $this->getSmall();
106
        if ($this->isolated) {
107
            $model_value_small = htmlspecialchars($model_value_small);
108
        }
109
110
        return parent::toArray() + [
111
            'name' => $this->getName(),
112
            'small' => $model_value_small,
113
            'visibled' => $this->getVisibled(),
114
        ];
115
    }
116
117
    /**
118
     * Get column value from instance.
119
     *
120
     * @param Collection|Model|Closure $instance
121
     * @param string $name
122
     *
123
     * @return mixed
124
     */
125
    protected function getValueFromObject($instance, $name)
126
    {
127
        if ($name instanceof Closure) {
0 ignored issues
show
introduced by
$name is never a sub-type of Closure.
Loading history...
128
            return $name($instance);
129
        }
130
131
        /*
132
         * Implement json parsing
133
         */
134
        if (strpos($name, '.') === false && strpos($name, '->') !== false) {
135
            $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

135
            $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...
136
            $jsonParts = collect(explode('->', $name));
137
138
            $jsonAttr = $instance->{$jsonParts->first()};
139
140
            $cast = $casts->get($jsonParts->first(), false);
141
142
            if ($cast == 'object') {
143
                $jsonAttr = json_decode(json_encode($jsonAttr), true);
144
            } elseif ($cast != 'array') {
145
                $jsonAttr = json_decode($jsonAttr);
146
            }
147
148
            return Arr::get($jsonAttr, $jsonParts->slice(1)->implode('.'));
149
        }
150
151
        $parts = explode('.', $name);
152
        $part = array_shift($parts);
153
154
        if ($instance instanceof Collection) {
155
            $instance = $instance->pluck($part);
156
        } elseif ($instance instanceof SuportCollection) {
0 ignored issues
show
introduced by
$instance is never a sub-type of Illuminate\Support\Collection.
Loading history...
157
            $instance = $instance->first();
158
            if ($instance instanceof Collection) {
159
                $instance = $instance->pluck($part);
160
            }
161
162
            if ($instance === null) {
163
                $instance = collect();
164
            }
165
        } elseif (! is_null($instance)) {
166
            $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

166
            /** @scrutinizer ignore-call */ 
167
            $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...
167
        }
168
169
        if (! empty($parts) && ! is_null($instance)) {
170
            return $this->getValueFromObject($instance, implode('.', $parts));
171
        }
172
173
        return $instance;
174
    }
175
}
176