LimitAndOffsetProvider::offset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\Limit;
13
use RDS\Hydrogen\Criteria\Offset;
14
use RDS\Hydrogen\Query;
15
16
/**
17
 * Class ExecutionsProvider
18
 * @mixin Query
19
 */
20
trait LimitAndOffsetProvider
21
{
22
    /**
23
     * An alias of "limit(...)"
24
     *
25
     * @param int $count
26
     * @return Query|$this|self
27
     */
28 4
    public function take(int $count): self
29
    {
30 4
        return $this->limit($count);
31
    }
32
33
    /**
34
     * @param int $count
35
     * @return Query|$this|self
36
     */
37 4
    public function limit(int $count): self
38
    {
39 4
        return $this->add(new Limit($this, $count));
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...
40
    }
41
42
    /**
43
     * An alias of "offset(...)"
44
     *
45
     * @param int $count
46
     * @return Query|$this|self
47
     */
48 2
    public function skip(int $count): self
49
    {
50 2
        return $this->offset($count);
51
    }
52
53
    /**
54
     * @param int $count
55
     * @return Query|$this|self
56
     */
57 2
    public function offset(int $count): self
58
    {
59 2
        return $this->add(new Offset($this, $count));
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...
60
    }
61
62
    /**
63
     * @param int $from
64
     * @param int $to
65
     * @return Query|$this|self
66
     */
67
    public function range(int $from, int $to): self
68
    {
69
        if ($from > $to) {
70
            throw new \InvalidArgumentException('The "$from" value must be less than $to');
71
        }
72
73
        return $this->limit($from)->offset($to - $from);
74
    }
75
}
76