Completed
Push — master ( 50808e...33b518 )
by Gianluca
05:07
created

DoctrinePaginator::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModule\Paginator\Adapter;
21
22
use Doctrine\ORM\Tools\Pagination\Paginator;
23
use Zend\Paginator\Adapter\AdapterInterface;
24
25
/**
26
 * Paginator adapter for the Zend\Paginator component
27
 *
28
 * @license MIT
29
 * @link    http://www.doctrine-project.org/
30
 * @since   0.1.0
31
 * @author  Tõnis Tobre <[email protected]>
32
 */
33
class DoctrinePaginator implements AdapterInterface
34
{
35
    /**
36
     * @var Paginator
37
     */
38
    protected $paginator;
39
40
    /**
41
     * Constructor
42
     *
43
     * @param Paginator $paginator
44
     */
45
    public function __construct(Paginator $paginator)
46
    {
47
        $this->paginator = $paginator;
48
    }
49
50
    /**
51
     * @param  Paginator $paginator
52
     * @return self
53
     */
54
    public function setPaginator(Paginator $paginator)
55
    {
56
        $this->paginator = $paginator;
57
    }
58
59
    /**
60
     * @return Paginator
61
     */
62
    public function getPaginator()
63
    {
64
        return $this->paginator;
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    public function getItems($offset, $itemCountPerPage)
71
    {
72
        $this->paginator
73
             ->getQuery()
74
             ->setFirstResult($offset)
75
             ->setMaxResults($itemCountPerPage);
76
77
        return $this->paginator->getIterator();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->paginator->getIterator(); (ArrayIterator) is incompatible with the return type declared by the interface Zend\Paginator\Adapter\AdapterInterface::getItems of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function count()
84
    {
85
        return $this->paginator->count();
86
    }
87
}
88