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

WithSorts   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

1 Method

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