Passed
Push — master ( edd222...b60533 )
by Zing
03:52
created

WithSorts::enableSorts()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 1
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\QueryBuilder\Concerns;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Support\Arr;
9
10
trait WithSorts
11
{
12
    /**
13
     * 排序逻辑.
14
     *
15
     * @param array $sorts
16
     *
17
     * @return mixed
18
     */
19 2
    public function enableSorts($sorts)
20
    {
21 2
        foreach (['desc', 'asc'] as $direction) {
22 2
            $this->when(
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

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