Passed
Pull Request — master (#7)
by Chito
01:39
created

PaginationResult::getIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 10
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Lampager\Cake;
4
5
use Cake\Collection\CollectionTrait;
6
use Cake\Datasource\ResultSetInterface;
7
use Iterator;
8
use IteratorAggregate;
9
use Lampager\PaginationResult as LampagerPaginationResult;
10
11
/**
12
 * Class PaginationResult
13
 *
14
 * This class intentionally does not extend \Lampager\PaginationResult
15
 * but has the same signature because \Cake\Datasource\ResultSetInterface
16
 * already implements \Iterator which conflicts with \IteratorAggregate.
17
 */
18
class PaginationResult implements ResultSetInterface
19
{
20
    /** @var LampagerPaginationResult */
21
    protected $result;
22
23
    /** @var Iterator */
24
    protected $iterator;
25
26
    use CollectionTrait;
27
28
    /**
29
     * PaginationResult constructor.
30
     * Merge $meta entries into $this.
31
     *
32
     * @param mixed $rows
33
     * @param array $meta
34
     */
35
    public function __construct($rows, array $meta)
36
    {
37
        $this->result = new LampagerPaginationResult($rows, $meta);
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function current()
44
    {
45
        if (!$this->iterator) {
46
            $this->iterator = $this->getIterator();
47
        }
48
49
        return $this->iterator->current();
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function key()
56
    {
57
        if (!$this->iterator) {
58
            $this->iterator = $this->getIterator();
59
        }
60
61
        return $this->iterator->key();
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function next()
68
    {
69
        if (!$this->iterator) {
70
            $this->iterator = $this->getIterator();
71
        }
72
73
        return $this->iterator->next();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->iterator->next() targeting Iterator::next() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public function rewind()
80
    {
81
        $this->iterator = $this->getIterator();
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    public function valid()
88
    {
89
        if (!$this->iterator) {
90
            $this->iterator = $this->getIterator();
91
        }
92
93
        return $this->iterator->valid();
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function jsonSerialize()
100
    {
101
        return $this->result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->result returns the type Lampager\PaginationResult which is incompatible with the return type mandated by Cake\Collection\Collecti...erface::jsonSerialize() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function serialize()
108
    {
109
        return json_encode($this->result);
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115
    public function unserialize($serialized)
116
    {
117
        $obj = json_decode($serialized, true);
118
        $this->result = new LampagerPaginationResult($obj['records'], $obj['meta']);
119
    }
120
121
    /**
122
     * @return Iterator
123
     */
124
    protected function getIterator()
125
    {
126
        /** @var Iterator|IteratorAggregate */
127
        $iterator = $this->result->getIterator();
128
129
        if ($iterator instanceof IteratorAggregate) {
130
            $iterator = $iterator->getIterator();
131
        }
132
133
        return $iterator;
134
    }
135
}
136