Enum::nextElement()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * \AppserverIo\Collections\Enum
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/collections
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Collections;
22
23
use AppserverIo\Lang\Object;
24
25
/**
26
 * Interface for lists of objects that can be returned in sequence.
27
 *
28
 * Successive  objects are obtained by the nextElement method.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2015 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/appserver-io/collections
34
 * @link      http://www.appserver.io
35
 */
36
class Enum extends Object implements EnumerationInterface
37
{
38
39
    /**
40
     * The possible items.
41
     *
42
     * @var array
43
     */
44
    protected $items = null;
45
46
    /**
47
     * The point to the actual item.
48
     *
49
     * @var integer
50
     */
51
    protected $itemPointer = 0;
52
53
    /**
54
     * Standard constructor that adds the array passed
55
     * as parameter to the internal member variable.
56
     *
57
     * @param array $items An array to initialize the Enumeration
58
     */
59
    public function __construct($items = array())
60
    {
61
        $this->items = $items;
62
    }
63
64
    /**
65
     * Tests whether there are elements remaining in the enumeration.
66
     *
67
     * @return true if there is at least one more element in the enumeration, that is, if the next call to nextElement will not throw a NoSuchElementException.
68
     * @see \AppserverIo\Collections\Enumeration::hasMoreElements()
69
     */
70
    public function hasMoreElements()
71
    {
72
        if (count($this->items) > $this->itemPointer) {
73
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type declared by the interface AppserverIo\Collections\...erface::hasMoreElements of type AppserverIo\Collections\true.

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...
74
        } else {
75
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface AppserverIo\Collections\...erface::hasMoreElements of type AppserverIo\Collections\true.

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...
76
        }
77
    }
78
79
    /**
80
     * Obtain the next element in the enumeration.
81
     *
82
     * @return mixed The next element in the enumeration
83
     * @throws \AppserverIo\Collections\NoSuchElementException if there are no more elements
84
     * @see \AppserverIo\Collections\EnumerationInterface::nextElement()
85
     */
86
    public function nextElement()
87
    {
88
        if (array_key_exists($this->itemPointer, $this->items)) {
89
            return $this->items[$this->itemPointer ++];
90
        } else {
91
            throw new NoSuchElementException('No such element was found');
92
        }
93
    }
94
}
95