OrderProvider::before()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 6
1
<?php
2
/**
3
 * This file is part of Hydrogen package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace RDS\Hydrogen\Query;
11
12
use RDS\Hydrogen\Criteria\OrderBy;
13
use RDS\Hydrogen\Criteria\Where\Operator;
14
use RDS\Hydrogen\Query;
15
16
/**
17
 * Trait OrderProvider
18
 * @mixin Query
19
 */
20
trait OrderProvider
21
{
22
    /**
23
     * @param string $field
24
     * @param mixed $value
25
     * @param bool $including
26
     * @return Query|$this|self
27
     */
28
    public function after(string $field, $value, bool $including = false): self
29
    {
30
        $operator = $including ? Operator::GT : Operator::GTE;
31
32
        return $this->where($field, $operator, $value)->asc($field);
0 ignored issues
show
Bug introduced by
It seems like where() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
33
    }
34
35
    /**
36
     * @param string ...$fields
37
     * @return Query|$this|self
38
     */
39
    public function asc(string ...$fields): self
40
    {
41
        foreach ($fields as $field) {
42
            $this->orderBy($field);
43
        }
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param string $field
50
     * @param bool $asc
51
     * @return Query|$this|self
52
     */
53 2
    public function orderBy(string $field, bool $asc = true): self
54
    {
55 2
        return $this->add(new OrderBy($this, $field, $asc));
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
56
    }
57
58
    /**
59
     * @param string $field
60
     * @param mixed $value
61
     * @param bool $including
62
     * @return Query|$this|self
63
     */
64
    public function before(string $field, $value, bool $including = false): self
65
    {
66
        $operator = $including ? Operator::LT : Operator::LTE;
67
68
        return $this->where($field, $operator, $value)->desc($field);
0 ignored issues
show
Bug introduced by
It seems like where() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
69
    }
70
71
    /**
72
     * @param string ...$fields
73
     * @return Query|$this|self
74
     */
75 2
    public function desc(string ...$fields): self
76
    {
77 2
        foreach ($fields as $field) {
78 2
            $this->orderBy($field, false);
79
        }
80
81 2
        return $this;
82
    }
83
84
    /**
85
     * @param string $field
86
     * @return Query|$this|self
87
     */
88
    public function latest(string $field = 'createdAt'): self
89
    {
90
        return $this->desc($field);
91
    }
92
93
    /**
94
     * @param string $field
95
     * @return Query|$this|self
96
     */
97
    public function oldest(string $field = 'createdAt'): self
98
    {
99
        return $this->asc($field);
100
    }
101
}
102