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.
Passed
Push — master ( b2510c...ee0bb8 )
by
unknown
12:46
created

OrderByClause::loadRelationOrder()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 30
c 0
b 0
f 0
cc 3
eloc 18
nc 3
nop 2
ccs 0
cts 21
cp 0
crap 12
rs 8.8571
1
<?php
2
3
namespace SleepingOwl\Admin\Display\Column;
4
5
use Illuminate\Support\Str;
6
use Mockery\Matcher\Closure;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Relations\Relation;
9
use SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface;
10
11
class OrderByClause implements OrderByClauseInterface
12
{
13
    /**
14
     * @var string|Closure
15
     */
16
    protected $name;
17
18
    /**
19
     * OrderByClause constructor.
20
     *
21
     * @param string|Closure $name
22
     */
23 17
    public function __construct($name)
24
    {
25 17
        $this->setName($name);
26 17
    }
27
28
    /**
29
     * @param Builder $query
30
     * @param string $direction
31
     */
32
    public function modifyQuery(Builder $query, $direction = 'asc')
33
    {
34
        $this->name instanceof \Closure
35
            ? $this->callCallable($query, $direction)
36
            : $this->callDefaultClause($query, $direction);
37
    }
38
39
    /**
40
     * @param string|Closure $name
41
     *
42
     * @return $this
43
     */
44 17
    public function setName($name)
45
    {
46 17
        $this->name = $name;
47
48 17
        return $this;
49
    }
50
51
    /**
52
     * @param Builder $query
53
     * @param string $direction
54
     */
55
    protected function callCallable(Builder $query, $direction)
56
    {
57
        call_user_func_array($this->name, [$query, $direction]);
58
    }
59
60
    /**
61
     * @param $name
62
     * @return bool
63
     */
64
    protected function isRelationName($name)
65
    {
66
        return Str::contains($name, '.');
67
    }
68
69
    /**
70
     * TODO: EagerLoad.
71
     */
72
    protected function eagerLoad()
73
    {
74
    }
75
76
    /**
77
     * Load Relations by this->name.
78
     * @param Builder $query
79
     * @param $direction
80
     */
81
    protected function loadRelationOrder(Builder $query, $direction)
82
    {
83
        $relations = collect(explode('.', $this->name));
84
85
        //Without Eager Load
86
        //TODO: With Eager Load
87
        if ($relations->count() == 2) {
88
            $model = $query->getModel();
89
            $relation = $relations->first();
90
91
            if (method_exists($model, $relation)) {
92
93
                /** @var Relation $relationClass */
94
                $relationClass = $model->{$relation}();
95
                $relationModel = $relationClass->getRelated();
96
                $foreignKey = $relationClass->getOwnerKey();
97
                $ownerKey = $relationClass->getForeignKey();
98
                $ownerTable = $model->getTable();
99
                $foreignTable = $relationModel->getTable();
100
101
                $ownerColumn = implode('.', [$ownerTable, $ownerKey]);
102
                $foreignColumn = implode('.', [$foreignTable, $foreignKey]);
103
                $sortedColumn = implode('.', [$foreignTable, $relations->last()]);
0 ignored issues
show
Unused Code introduced by
$sortedColumn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
104
105
                $query->select([$ownerTable.'.*', $foreignTable.'.'.$relations->last()])
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
106
                    ->join($foreignTable, $foreignColumn, '=', $ownerColumn, 'left')
107
                    ->orderBy($foreignColumn, $direction);
108
            }
109
        }
110
    }
111
112
    /**
113
     * @param Builder $query
114
     * @param string $direction
115
     */
116
    protected function callDefaultClause(Builder $query, $direction)
117
    {
118
        if ($this->isRelationName($this->name)) {
119
            $this->loadRelationOrder($query, $direction);
120
        } else {
121
            $query->orderBy($this->name, $direction);
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
122
        }
123
    }
124
}
125