PaginatedList::next()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Model;
4
5
trait PaginatedList
6
{
7
    /* @var integer */
8
    protected $position = 0;
9
10
    /**
11
     * Rewind the Iterator to the first element.
12
     *
13
     * @link http://php.net/manual/en/iterator.rewind.php
14
     *
15
     * @return void
16
     */
17
    public function rewind()
18
    {
19
        $this->position = 0;
20
    }
21
22
    /**
23
     * Checks if current position is valid.
24
     *
25
     * @link http://php.net/manual/en/iterator.valid.php
26
     *
27
     * @return bool
28
     */
29
    public function valid()
30
    {
31
        if (!isset($this->resources[$this->position])) {
0 ignored issues
show
Bug introduced by
The property resources does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
            $this->fetchBatch();
0 ignored issues
show
Bug introduced by
It seems like fetchBatch() 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
        return isset($this->resources[$this->position]);
36
    }
37
38
    /**
39
     * Return the current element.
40
     *
41
     * @link http://php.net/manual/en/iterator.current.php
42
     *
43
     * @return mixed
44
     */
45
    public function current()
46
    {
47
        return $this->resources[$this->position];
48
    }
49
50
    /**
51
     * Move forward to next element.
52
     *
53
     * @link http://php.net/manual/en/iterator.next.php
54
     *
55
     * @return void
56
     */
57
    public function next()
58
    {
59
        $this->position++;
60
    }
61
62
    /**
63
     * Return the key of the current element.
64
     *
65
     * @link http://php.net/manual/en/iterator.key.php
66
     *
67
     * @return int|null Scalar on success, or null on failure.
68
     */
69
    public function key()
70
    {
71
        return $this->position;
72
    }
73
}
74