Completed
Pull Request — master (#18)
by
unknown
05:56 queued 04:19
created

InvalidSortQuery::sortsNotAllowed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Spatie\QueryBuilder\Exceptions;
4
5
use Illuminate\Http\Response;
6
use Illuminate\Support\Collection;
7
8
class InvalidSortQuery extends InvalidQuery
9
{
10
    /** @var string */
11
    protected $unknownSort;
12
13
    /** @var \Illuminate\Support\Collection */
14
    protected $allowedSorts;
15
16
    public function __construct(string $unknownSort, Collection $allowedSorts)
17
    {
18
        $this->unknownSort = $unknownSort;
19
        $this->allowedSorts = $allowedSorts;
20
21
        $allowedSorts = $allowedSorts->implode(', ');
22
        $message = "Given sort `{$this->unknownSort}` is not allowed. Allowed sorts are `{$allowedSorts}`.";
23
24
        parent::__construct(Response::HTTP_BAD_REQUEST, $message);
25
    }
26
27
    public static function sortsNotAllowed(string $unknownSort, Collection $allowedSorts)
0 ignored issues
show
Unused Code introduced by
The parameter $unknownSort is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $allowedSorts is not used and could be removed.

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

Loading history...
28
    {
29
        return new static(...func_get_args());
0 ignored issues
show
Bug introduced by
The call to InvalidSortQuery::__construct() misses a required argument $allowedSorts.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
func_get_args() is of type array, but the function expects a string.

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...
30
    }
31
32
    public function getUnknownSort(): string
33
    {
34
        return $this->unknownSort;
35
    }
36
37
    public function getAllowedSorts(): Collection
38
    {
39
        return $this->allowedSorts;
40
    }
41
}
42