Completed
Push — master ( 47778c...ba169d )
by Dan Michael O.
10:36
created

PagedLazyResourceList::current()   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
/**
6
 * The PagedLazyResourceList extends the LazyResourceList class with functionality
7
 * for iteratively fetching paged resources.
8
 */
9
abstract class PagedLazyResourceList extends LazyResourceList
10
{
11
    /* @var integer */
12
    protected $position = 0;
13
14
    /**
15
     * Convert a retrieved resource object to a model object.
16
     *
17
     * @param $data
18
     * @return mixed
19
     */
20
    abstract protected function convertToResource($data);
21
22
    /**
23
     * Fetch more resources.
24
     *
25
     * @return mixed
26
     */
27
    abstract protected function fetchBatch();
28
29
    /**
30
     * Fetch all the data.
31
     */
32
    protected function fetchData()
33
    {
34
        return iterator_to_array($this);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return iterator_to_array($this); (array) is incompatible with the return type of the parent method Scriptotek\Alma\Model\LazyResource::fetchData of type stdClass.

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...
35
    }
36
37
    /**
38
     * Rewind the Iterator to the first element.
39
     *
40
     * @link http://php.net/manual/en/iterator.rewind.php
41
     * @return void
42
     */
43
    public function rewind()
44
    {
45
        $this->position = 0;
46
    }
47
48
    /**
49
     * Checks if current position is valid.
50
     *
51
     * @link http://php.net/manual/en/iterator.valid.php
52
     * @return boolean
53
     */
54
    public function valid()
55
    {
56
        if (!isset($this->resources[$this->position])) {
57
            $this->fetchBatch();
58
        }
59
60
        return isset($this->resources[$this->position]);
61
    }
62
63
    /**
64
     * Return the current element
65
     * @link http://php.net/manual/en/iterator.current.php
66
     * @return mixed
67
     */
68
    public function current()
69
    {
70
        return $this->resources[$this->position];
71
    }
72
73
    /**
74
     * Move forward to next element
75
     * @link http://php.net/manual/en/iterator.next.php
76
     * @return void
77
     */
78
    public function next()
79
    {
80
        $this->position++;
81
    }
82
83
    /**
84
     * Return the key of the current element
85
     * @link http://php.net/manual/en/iterator.key.php
86
     * @return integer|null Scalar on success, or null on failure.
87
     */
88
    public function key()
89
    {
90
        return $this->position;
91
    }
92
}
93