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 — 1.0 ( d76316...d83f8d )
by Sebastian
12:17 queued 07:38
created

Sortable::sortByRelation()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.8438

Importance

Changes 7
Bugs 1 Features 0
Metric Value
c 7
b 1
f 0
dl 0
loc 27
ccs 10
cts 16
cp 0.625
rs 8.5806
cc 4
eloc 13
nc 6
nop 2
crap 4.8438
1
<?php
2
3
namespace SebastianBerc\Repositories\Traits;
4
5
use DB;
6
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
8
use Illuminate\Database\Eloquent\Relations\HasMany;
9
use Illuminate\Database\Eloquent\Relations\HasOne;
10
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
11
use Illuminate\Database\Eloquent\Relations\Relation;
12
13
/**
14
 * Class Sortable.
15
 *
16
 * @author    Sebastian Berć <[email protected]>
17
 * @copyright Copyright (c) Sebastian Berć
18
 */
19
trait Sortable
20
{
21
    /**
22
     * Append many column sorting to query builder.
23
     *
24
     * @param array $columns
25
     *
26
     * @return $this
27
     */
28 16
    public function multiSortBy(array $columns)
29
    {
30 16
        foreach ($columns as $column => $direction) {
31 4
            if (strpos($column, '.')) {
32 2
                $this->sortByRelation($column, $direction);
0 ignored issues
show
Documentation introduced by
$column is of type integer|string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33
            } else {
34 2
                $this->sortBy($column, $direction);
35
            }
36 14
        }
37
38 14
        return $this;
39
    }
40
41
    /**
42
     * Append relation column sorting to query builder.
43
     *
44
     * @param array  $column
45
     * @param string $direction
46
     *
47
     * @return $this
48
     */
49 2
    public function sortByRelation($column, $direction = 'ASC')
50
    {
51 2
        $relations = explode('.', $column);
52 2
        $column    = array_pop($relations);
53
54
        /** @var \Illuminate\Database\Eloquent\Model $model */
55 2
        $model = $this->repository->makeModel();
0 ignored issues
show
Bug introduced by
The property repository does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
57 2
        $this->instance->getQuery()->orders = [];
0 ignored issues
show
Bug introduced by
The property instance does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58
59 2
        foreach ($relations as $relation) {
60
            /* @var Relation $relationClass */
61 2
            $this->joinRelation($relationClass = $model->{camel_case($relation)}());
62
63 2
            $model = $relationClass->getRelated();
64 2
        }
65
66 2
        foreach ($this->instance->getQuery()->columns as $key => $value) {
67
            if ($value === '*') {
68
                $this->instance->getQuery()->columns[$key] = DB::raw("{$this->repository->makeModel()->getTable()}.*");
69
            }
70
        }
71
72
        $this->instance->orderBy("{$model->getTable()}.{$column}", $direction);
73
74
        return $this;
75
    }
76
77
    /**
78
     * Add joining the tables to query based on the type of relationship.
79
     *
80
     * @param Relation $relationClass
81
     *
82
     * @return $this
83
     */
84 2
    protected function joinRelation(Relation $relationClass)
85
    {
86 2
        switch (get_class($relationClass)) {
87 2
            case BelongsToMany::class:
88
                /* @var BelongsToMany $relationClass */
89
                $this->joinBelongsToMany($relationClass);
90
                break;
91 2
            case BelongsTo::class:
92
                /* @var BelongsTo $relationClass */
93
                $this->joinBelongsTo($relationClass);
94
                break;
95 2
            case HasOne::class:
96 2
            case HasMany::class:
97
                /* @var HasOneOrMany $relationClass */
98 2
                $this->joinHasOneOrMany($relationClass);
99 2
                break;
100 2
        }
101 2
    }
102
103
    /**
104
     * Join a belongs to many relationship.
105
     *
106
     * @param BelongsToMany $relation
107
     *
108
     * @return mixed
109
     */
110
    protected function joinBelongsToMany(BelongsToMany $relation)
111
    {
112
        return $this->instance->join(
113
            $relation->getTable(),
114
            $relation->getParent()->getTable() . '.' . $relation->getParent()->getKeyName(),
115
            '=',
116
            $relation->getForeignKey()
117
        )->join(
118
            $relation->getRelated()->getTable(),
119
            $relation->getRelated()->getTable() . '.' . $relation->getRelated()->getKeyName(),
120
            '=',
121
            $relation->getOtherKey()
122
        );
123
    }
124
125
    /**
126
     * Join a belongs to relationship.
127
     *
128
     * @param BelongsTo $relation
129
     *
130
     * @return mixed
131
     */
132
    protected function joinBelongsTo(BelongsTo $relation)
133
    {
134
        return $this->instance->join(
135
            $relation->getRelated()->getTable(),
136
            $relation->getQualifiedOtherKeyName(),
137
            '=',
138
            $relation->getQualifiedForeignKey()
139
        );
140
    }
141
142
    /**
143
     * Join a has one relationship.
144
     *
145
     * @param HasOneOrMany $relation
146
     *
147
     * @return mixed
148
     */
149 2
    protected function joinHasOneOrMany(HasOneOrMany $relation)
150
    {
151 2
        return $this->instance->join(
152 2
            $relation->getRelated()->getTable(),
153 2
            $relation->getRelated()->getTable() . '.' . $relation->getParent()->getForeignKey(),
154 2
            '=',
155 2
            $relation->getParent()->getTable() . '.' . $relation->getParent()->getKeyName()
156 2
        );
157
    }
158
159
    /**
160
     * Append column sorting to query builder.
161
     *
162
     * @param string|array $column
163
     * @param string       $direction
164
     *
165
     * @return $this
166
     */
167 2
    public function sortBy($column, $direction = 'ASC')
168
    {
169 2
        $this->instance->orderBy($column, $direction);
170
171 2
        return $this;
172
    }
173
}
174