RecursiveIterator::key()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace SimpleAcl\Object;
4
5
use RecursiveIterator as ArrayIterator;
6
7
/**
8
 * Used to iterate by Roles and Resources hierarchy.
9
 *
10
 * @package SimpleAcl\Object
11
 */
12
class RecursiveIterator implements ArrayIterator
13
{
14
  /**
15
   * @var Object[]
16
   */
17
  protected $objects = [];
18
19
  /**
20
   * @param Object[] $objects
21
   */
22 11
  public function __construct($objects)
23
  {
24 11
    $this->objects = $objects;
25 11
  }
26
27 11
  public function current()
28
  {
29 11
    return current($this->objects);
30
  }
31
32 9
  public function next()
33
  {
34 9
    return next($this->objects);
35
  }
36
37 10
  public function key()
38
  {
39 10
    if (null === key($this->objects)) {
40 10
      return null;
41
    }
42
43 10
    return $this->current()->getName();
44
  }
45
46 9
  public function valid()
47
  {
48 9
    return $this->key() !== null;
49
  }
50
51 8
  public function rewind()
52
  {
53 8
    return reset($this->objects);
54
  }
55
56
  public function hasChildren()
57
  {
58
    return false;
59
  }
60
61
  public function getChildren()
62
  {
63
    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 RecursiveIterator::getChildren of type RecursiveIterator.

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...
64
  }
65
66
67
}
68