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 — 1.0 (#19)
by Sebastian
04:35
created

Sortable::sortBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
crap 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\HasOneOrMany;
9
use Illuminate\Database\Eloquent\Relations\Relation;
10
11
/**
12
 * Class Sortable.
13
 *
14
 * @author    Sebastian Berć <[email protected]>
15
 * @copyright Copyright (c) Sebastian Berć
16
 */
17
trait Sortable
18
{
19
    /**
20
     * Append many column sorting to query builder.
21
     *
22
     * @param array $columns
23
     *
24
     * @return $this
25
     */
26 14
    public function multiSortBy(array $columns)
27
    {
28 14
        foreach ($columns as $column => $direction) {
29 4
            if (strpos($column, '.')) {
30 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...
31 2
            } else {
32 2
                $this->sortBy($column, $direction);
33
            }
34 14
        }
35
36 14
        return $this;
37
    }
38
39
    /**
40
     * Append relation column sorting to query builder.
41
     *
42
     * @param array  $column
43
     * @param string $direction
44
     *
45
     * @return $this
46
     */
47 2
    public function sortByRelation($column, $direction = 'ASC')
48
    {
49 2
        $relations = explode('.', $column);
50 2
        $column    = array_pop($relations);
51
52
        /** @var \Illuminate\Database\Eloquent\Model $model */
53 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...
54
55 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...
56
57 2
        foreach ($relations as $relation) {
58
            /* @var Relation $relationClass */
59 2
            $this->joinRelation($relationClass = $model->{camel_case($relation)}());
60
61 2
            $model = $relationClass->getRelated();
62 2
        }
63
64 2
        $this->instance->select(DB::raw("{$this->repository->makeModel()->getTable()}.*"))
65 2
            ->orderBy("{$model->getTable()}.{$column}", $direction);
66
67 2
        return $this;
68
    }
69
70
    /**
71
     * Add joining the tables to query based on the type of relationship.
72
     *
73
     * @param Relation $relationClass
74
     */
75 2
    protected function joinRelation(Relation $relationClass)
76
    {
77 2
        switch (get_class($relationClass)) {
78 2
            case BelongsToMany::class:
79
                /* @var BelongsToMany $relationClass */
80
                $this->instance = $this->joinBelongsToMany($relationClass);
81
                break;
82 2
            case BelongsTo::class:
83
                /* @var BelongsTo $relationClass */
84
                $this->instance = $this->joinBelongsTo($relationClass);
85
                break;
86 2
            case HasOneOrMany::class:
87
                /* @var HasOneOrMany $relationClass */
88
                $this->instance = $this->joinHasOne($relationClass);
89
                break;
90 2
        }
91 2
    }
92
93
    /**
94
     * Join a belongs to many relationship.
95
     *
96
     * @param BelongsToMany $relation
97
     *
98
     * @return mixed
99
     */
100
    protected function joinBelongsToMany(BelongsToMany $relation)
101
    {
102
        return $this->instance->join($relation->getTable(),
103
            $relation->getParent()->getTable() . '.' . $relation->getParent()->getKeyName(), '=',
104
            $relation->getForeignKey())->join($relation->getRelated()->getTable(),
105
            $relation->getRelated()->getTable() . '.' . $relation->getRelated()->getKeyName(), '=',
106
            $relation->getOtherKey());
107
    }
108
109
    /**
110
     * Join a belongs to relationship.
111
     *
112
     * @param BelongsTo $relation
113
     *
114
     * @return mixed
115
     */
116
    protected function joinBelongsTo(BelongsTo $relation)
117
    {
118
        return $this->instance->join($relation->getRelated()->getTable(), $relation->getQualifiedOtherKeyName(), '=',
119
            $relation->getQualifiedForeignKey());
120
    }
121
122
    /**
123
     * Join a has one relationship.
124
     *
125
     * @param HasOneOrMany $relation
126
     *
127
     * @return mixed
128
     */
129
    protected function joinHasOne(HasOneOrMany $relation)
130
    {
131
        return $this->instance->join($relation->getRelated()->getTable(),
132
            $relation->getRelated()->getTable() . '.' . $relation->getParent()->getForeignKey(), '=',
133
            $relation->getParent()->getTable() . '.' . $relation->getParent()->getKeyName());
134
    }
135
136
    /**
137
     * Append column sorting to query builder.
138
     *
139
     * @param string|array $column
140
     * @param string       $direction
141
     *
142
     * @return $this
143
     */
144 2
    public function sortBy($column, $direction = 'ASC')
145
    {
146 2
        $this->instance = $this->instance->orderBy($column, $direction);
147
148 2
        return $this;
149
    }
150
}
151