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
Branch master (9e3162)
by Dave
63:34
created

NamedColumn::getValueFromObject()   C

Complexity

Conditions 13
Paths 18

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 19.2573

Importance

Changes 0
Metric Value
cc 13
eloc 27
nc 18
nop 2
dl 0
loc 49
ccs 6
cts 9
cp 0.6667
crap 19.2573
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 21
32
    /**
33 21
     * NamedColumn constructor.
34 21
     * @param $name
35
     * @param $label string
36 21
     * @param $small string
37
     */
38 21
    public function __construct($name, $label = null, $small = null)
39 15
    {
40 15
        parent::__construct($label);
41 21
        $this->setName($name);
42
        $this->setSmall($small);
43
44
        $this->setHtmlAttribute('class', 'row-'.strtolower(class_basename(get_called_class())));
45
46 17
        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 17
        }
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getName()
55
    {
56 21
        return $this->name;
57
    }
58 21
59
    /**
60 21
     * @param string $name
61
     *
62
     * @return $this
63
     */
64
    public function setName($name)
65
    {
66 7
        $this->name = $name;
67
68 7
        return $this;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getSmall()
75
    {
76 15
        return $this->small;
77
    }
78 15
79 15
    /**
80 15
     * @param string $small
81 15
     *
82 15
     * @return $this
83
     */
84 15
    public function setSmall($small)
85
    {
86
        $this->small = $small;
87
88
        return $this;
89
    }
90
91
    /**
92 1
     * @return mixed
93
     */
94 1
    public function getModelValue()
95 1
    {
96 1
        return $this->getValueFromObject($this->getModel(), $this->getName());
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102
    public function getModelSmallValue()
103
    {
104
        return $this->getValueFromObject($this->getModel(), $this->getSmall());
105
    }
106
107 7
    /**
108
     * @param OrderByClauseInterface|bool $orderable
109 7
     * @deprecated
110
     * @return TableColumn
111
     */
112
    public function setOrderable($orderable = true)
113
    {
114
        if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) {
115
            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...
116 7
                $orderable = $this->getName();
117
            }
118
        }
119
120
        return parent::setOrderable($orderable);
0 ignored issues
show
Deprecated Code introduced by
The function SleepingOwl\Admin\Displa...eColumn::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

120
        return /** @scrutinizer ignore-deprecated */ parent::setOrderable($orderable);
Loading history...
121
    }
122
123
    /**
124
     * Get the instance as an array.
125
     *
126
     * @return array
127
     */
128
    public function toArray()
129
    {
130
        return parent::toArray() + [
131
                'name' => $this->getName(),
132
            ];
133 7
    }
134 7
135
    /**
136 7
     * Get column value from instance.
137 1
     *
138 7
     * @param Collection|Model|Closure $instance
139
     * @param string $name
140
     *
141
     * @return mixed
142
     */
143
    protected function getValueFromObject($instance, $name)
144
    {
145
        if ($name instanceof Closure) {
0 ignored issues
show
introduced by
$name is never a sub-type of Closure.
Loading history...
146
            return $name($instance);
147 7
        }
148 7
149 7
        /*
150
         * Implement json parsing
151 7
         */
152 2
        if (strpos($name, '.') === false && strpos($name, '->') !== false) {
153
            $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

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

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