Order::inverse()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace mav3rick177\RapidPagination\Base\Query;
4
5
use mav3rick177\RapidPagination\Base\Exceptions\Query\BadKeywordException;
6
7
/**
8
 * Class Order
9
 */
10
class Order
11
{
12
    const ASCENDING = 'asc';
13
    const DESCENDING = 'desc';
14
    const ASC = 'asc';
15
    const DESC = 'desc';
16
17
    /**
18
     * @var string
19
     */
20
    protected $column;
21
22
    /**
23
     * @var string
24
     */
25
    protected $order;
26
27
    /**
28
     * @param  string[][] $orders
29
     * @return Order[]
30
     */
31 17
    public static function createMany(array $orders)
32
    {
33 17
        return array_map(static function (array $order) {
34 17
            return new Order(...$order);
0 ignored issues
show
Bug introduced by
The call to Order::__construct() misses a required argument $order.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$order 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...
35 17
        }, $orders);
36
    }
37
38
    /**
39
     * Order constructor.
40
     *
41
     * @param string $column
42
     * @param string $order
43
     */
44 17
    public function __construct($column, $order)
45
    {
46 17
        $this->column = (string)$column;
47 17
        $this->order = static::validate($order);
48
    }
49
50
    /**
51
     * @param $order
52
     * @return string
53
     */
54 17 View Code Duplication
    protected static function validate($order)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56 17
        $order = strtolower($order);
57 17
        if (!preg_match('/\A(asc|desc)(?:ending)?\z/', $order, $m)) {
58
            throw new BadKeywordException('Order must be "asc", "ascending", "desc" or "descending"');
59
        }
60 17
        return $m[1];
61
    }
62
63
    /**
64
     * @return array
65
     */
66 17
    public function toArray()
67
    {
68 17
        return [$this->column, $this->order];
69
    }
70
71
    /**
72
     * @return string
73
     */
74 10
    public function column()
75
    {
76 10
        return $this->column;
77
    }
78
79
    /**
80
     * @return string
81
     */
82 10
    public function order()
83
    {
84 10
        return $this->order;
85
    }
86
87
    /**
88
     * @return bool
89
     */
90
    public function ascending()
91
    {
92
        return $this->order === static::ASCENDING;
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    public function descending()
99
    {
100
        return $this->order === static::DESCENDING;
101
    }
102
103
    /**
104
     * @return static
105
     */
106 12
    public function inverse()
107
    {
108 12
        return new static($this->column, $this->order === static::ASCENDING ? static::DESCENDING : static::ASCENDING);
109
    }
110
}
111