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

WithSorts   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 25
ccs 9
cts 9
cp 1
rs 10
c 1
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A enableSorts() 0 16 3
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