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
Pull Request — master (#688)
by
unknown
18:52
created

NamedColumn::setFilterCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
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 bool
24
     */
25
    protected $orderable = true;
26
27
    /**
28
     * @param Closure|null|string $name
29
     * @param null|string $label
30
     */
31 21
    public function __construct($name, $label = null)
32
    {
33 21
        parent::__construct($label);
34 21
        $this->setName($name);
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 31 can also be of type null or object<Closure>; however, SleepingOwl\Admin\Displa...\NamedColumn::setName() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
35
36 21
        $this->setHtmlAttribute('class', 'row-'.strtolower(class_basename(get_called_class())));
37
38 21
        if ($this->orderable) {
39 15
            $this->setOrderable();
0 ignored issues
show
Deprecated Code introduced by
The method SleepingOwl\Admin\Displa...dColumn::setOrderable() has been deprecated.

This method has been deprecated.

Loading history...
40 15
        }
41 21
    }
42
43
    /**
44
     * @return string
45
     */
46 17
    public function getName()
47
    {
48 17
        return $this->name;
49
    }
50
51
    /**
52
     * @param string $name
53
     *
54
     * @return $this
55
     */
56 21
    public function setName($name)
57
    {
58 21
        $this->name = $name;
59
60 21
        return $this;
61
    }
62
63
    /**
64
     * @return mixed
65
     */
66 7
    public function getModelValue()
67
    {
68 7
        return $this->getValueFromObject($this->getModel(), $this->getName());
69
    }
70
71
    /**
72
     * @param OrderByClauseInterface|bool $orderable
73
     * @deprecated
74
     * @return TableColumn
75
     */
76 15
    public function setOrderable($orderable = true)
77
    {
78 15
        if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) {
79 15
            if (! is_string($orderable) && ! $orderable instanceof Closure) {
80 15
                $orderable = $this->getName();
81 15
            }
82 15
        }
83
84 15
        return parent::setOrderable($orderable);
0 ignored issues
show
Deprecated Code introduced by
The method SleepingOwl\Admin\Displa...eColumn::setOrderable() has been deprecated.

This method has been deprecated.

Loading history...
85
    }
86
87
    /**
88
     * Get the instance as an array.
89
     *
90
     * @return array
91
     */
92 1
    public function toArray()
93
    {
94 1
        return parent::toArray() + [
95 1
                'name' => $this->getName(),
96 1
            ];
97
    }
98
99
    /**
100
     * Get column value from instance.
101
     *
102
     * @param Collection|Model|Closure $instance
103
     * @param string $name
104
     *
105
     * @return mixed
106
     */
107 7
    protected function getValueFromObject($instance, $name)
108
    {
109 7
        if ($name instanceof Closure) {
110
            return $name($instance);
111
        }
112
113
        /*
114
         * Implement json parsing
115
         */
116 7
        if (strpos($name, '.') === false && strpos($name, '->') !== false) {
117
            $casts = collect($instance->getCasts());
0 ignored issues
show
Bug introduced by
The method getCasts does only exist in Illuminate\Database\Eloquent\Model, but not in Closure and Illuminate\D...ase\Eloquent\Collection.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
118
            $jsonParts = collect(explode('->', $name));
119
120
            $jsonAttr = $instance->{$jsonParts->first()};
121
122
            $cast = $casts->get($jsonParts->first(), false);
123
124 View Code Duplication
            if ($cast == 'object') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
                $jsonAttr = json_decode(json_encode($jsonAttr), true);
126
            } elseif ($cast != 'array') {
127
                $jsonAttr = json_decode($jsonAttr);
128
            }
129
130
            return Arr::get($jsonAttr, $jsonParts->slice(1)->implode('.'));
131
        }
132
133 7
        $parts = explode('.', $name);
134 7
        $part = array_shift($parts);
135
136 7
        if ($instance instanceof Collection) {
137 1
            $instance = $instance->pluck($part);
138 7
        } elseif ($instance instanceof SuportCollection) {
139
            $instance = $instance->first();
140
            if ($instance instanceof Collection) {
141
                $instance = $instance->pluck($part);
142
            }
143
144
            if ($instance === null) {
145
                $instance = collect();
146
            }
147 7
        } elseif (! is_null($instance)) {
148 7
            $instance = $instance->getAttribute($part);
0 ignored issues
show
Bug introduced by
The method getAttribute does only exist in Illuminate\Database\Eloquent\Model, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
149 7
        }
150
151 7
        if (! empty($parts) && ! is_null($instance)) {
152 2
            return $this->getValueFromObject($instance, implode('.', $parts));
153
        }
154
155 7
        return $instance;
156
    }
157
}
158