Completed
Push — master ( 9d90d4...cee823 )
by Dan Michael O.
03:06
created

PaginatedList::rewind()   A

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
     * Fetch all the data.
12
     */
13
    protected function fetchData()
14
    {
15
        return iterator_to_array($this);
16
    }
17
18
    /**
19
     * Rewind the Iterator to the first element.
20
     *
21
     * @link http://php.net/manual/en/iterator.rewind.php
22
     * @return void
23
     */
24
    public function rewind()
25
    {
26
        $this->position = 0;
27
    }
28
29
    /**
30
     * Checks if current position is valid.
31
     *
32
     * @link http://php.net/manual/en/iterator.valid.php
33
     * @return boolean
34
     */
35
    public function valid()
36
    {
37
        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...
38
            $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...
39
        }
40
41
        return isset($this->resources[$this->position]);
42
    }
43
44
    /**
45
     * Return the current element
46
     * @link http://php.net/manual/en/iterator.current.php
47
     * @return mixed
48
     */
49
    public function current()
50
    {
51
        return $this->resources[$this->position];
52
    }
53
54
    /**
55
     * Move forward to next element
56
     * @link http://php.net/manual/en/iterator.next.php
57
     * @return void
58
     */
59
    public function next()
60
    {
61
        $this->position++;
62
    }
63
64
    /**
65
     * Return the key of the current element
66
     * @link http://php.net/manual/en/iterator.key.php
67
     * @return integer|null Scalar on success, or null on failure.
68
     */
69
    public function key()
70
    {
71
        return $this->position;
72
    }
73
}
74