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.

Sortable   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 76.47%

Importance

Changes 9
Bugs 3 Features 0
Metric Value
wmc 16
c 9
b 3
f 0
lcom 1
cbo 4
dl 0
loc 159
ccs 52
cts 68
cp 0.7647
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A multiSortBy() 0 12 3
B sortByRelation() 0 31 4
B joinRelation() 0 18 5
A joinBelongsToMany() 0 14 1
A joinBelongsTo() 0 9 1
A joinHasOneOrMany() 0 9 1
A sortBy() 0 6 1
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 2
            } else {
34 2
                $this->sortBy($column, $direction);
35
            }
36 16
        }
37
38 16
        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
        $selectedColumns = $this->instance->getQuery()->columns;
67
68 2
        $addColumn = DB::raw("{$this->repository->makeModel()->getTable()}.*");
69
70 2
        if (is_array($selectedColumns) && array_search('*', $selectedColumns, true) !== null) {
71
            $this->instance->getQuery()->columns[array_search('*', $selectedColumns, true)] = $addColumn;
72
        } else {
73 2
            $this->instance->select($addColumn);
74
        }
75
76 2
        $this->instance->orderBy("{$model->getTable()}.{$column}", $direction);
77
78 2
        return $this;
79
    }
80
81
    /**
82
     * Add joining the tables to query based on the type of relationship.
83
     *
84
     * @param Relation $relationClass
85
     *
86
     * @return $this
87
     */
88 2
    protected function joinRelation(Relation $relationClass)
89
    {
90 2
        switch (get_class($relationClass)) {
91 2
            case BelongsToMany::class:
92
                /* @var BelongsToMany $relationClass */
93
                $this->joinBelongsToMany($relationClass);
94
                break;
95 2
            case BelongsTo::class:
96
                /* @var BelongsTo $relationClass */
97 2
                $this->joinBelongsTo($relationClass);
98 2
                break;
99 2
            case HasOne::class:
100 2
            case HasMany::class:
101
                /* @var HasOneOrMany $relationClass */
102 2
                $this->joinHasOneOrMany($relationClass);
103 2
                break;
104 2
        }
105 2
    }
106
107
    /**
108
     * Join a belongs to many relationship.
109
     *
110
     * @param BelongsToMany $relation
111
     *
112
     * @return mixed
113
     */
114
    protected function joinBelongsToMany(BelongsToMany $relation)
115
    {
116
        return $this->instance->join(
117
            $relation->getTable(),
118
            $relation->getParent()->getTable() . '.' . $relation->getParent()->getKeyName(),
119
            '=',
120
            $relation->getForeignKey()
121
        )->join(
122
            $relation->getRelated()->getTable(),
123
            $relation->getRelated()->getTable() . '.' . $relation->getRelated()->getKeyName(),
124
            '=',
125
            $relation->getOtherKey()
126
        );
127
    }
128
129
    /**
130
     * Join a belongs to relationship.
131
     *
132
     * @param BelongsTo $relation
133
     *
134
     * @return mixed
135
     */
136 2
    protected function joinBelongsTo(BelongsTo $relation)
137
    {
138 2
        return $this->instance->join(
139 2
            $relation->getRelated()->getTable(),
140 2
            $relation->getQualifiedOtherKeyName(),
141 2
            '=',
142 2
            $relation->getQualifiedForeignKey()
143 2
        );
144
    }
145
146
    /**
147
     * Join a has one relationship.
148
     *
149
     * @param HasOneOrMany $relation
150
     *
151
     * @return mixed
152
     */
153 2
    protected function joinHasOneOrMany(HasOneOrMany $relation)
154
    {
155 2
        return $this->instance->join(
156 2
            $relation->getRelated()->getTable(),
157 2
            $relation->getRelated()->getTable() . '.' . $relation->getParent()->getForeignKey(),
158 2
            '=',
159 2
            $relation->getParent()->getTable() . '.' . $relation->getParent()->getKeyName()
160 2
        );
161
    }
162
163
    /**
164
     * Append column sorting to query builder.
165
     *
166
     * @param string|array $column
167
     * @param string       $direction
168
     *
169
     * @return $this
170
     */
171 2
    public function sortBy($column, $direction = 'ASC')
172
    {
173 2
        $this->instance->orderBy($column, $direction);
174
175 2
        return $this;
176
    }
177
}
178