Passed
Pull Request — master (#1)
by Zing
08:22 queued 04:05
created

WithSorts::applySort()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 15
ccs 0
cts 8
cp 0
crap 12
rs 10
1
<?php
2
3
namespace Zing\QueryBuilder\Concerns;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Support\Arr;
7
8
trait WithSorts
9
{
10
    /**
11
     * 排序逻辑.
12
     *
13
     * @param array $inputs
14
     * @param array $sorts
15
     *
16
     * @return mixed
17
     */
18
    protected function applySort($inputs, $sorts)
0 ignored issues
show
Unused Code introduced by
The parameter $inputs is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

18
    protected function applySort(/** @scrutinizer ignore-unused */ $inputs, $sorts)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
    {
20
        foreach (['desc', 'asc'] as $direction) {
21
            $this->when($this->request->input($direction),
0 ignored issues
show
Bug introduced by
It seems like when() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
            $this->/** @scrutinizer ignore-call */ 
22
                   when($this->request->input($direction),
Loading history...
22
                function (Builder $query, $descAttribute) use ($sorts, $direction) {
23
                    if (array_key_exists($descAttribute, $sorts)) {
24
                        $descAttribute = Arr::get($sorts, $descAttribute);
25
                    }
26
27
                    return $query->orderBy($descAttribute, $direction);
28
                }
29
            );
30
        }
31
32
        return $this;
33
    }
34
}
35