Completed
Push — master ( 869e4b...2c9a0d )
by Márk
02:31
created

LazyAppendIterator::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Indigo\Iterator;
4
5
/**
6
 * This is an extension of the SPL AppendIterator.
7
 *
8
 * Lazily append iterators from a traversable object.
9
 * Useful when moving to the next element invokes an expensive resolver logic.
10
 *
11
 * @author Márk Sági-Kazár <[email protected]>
12
 */
13
class LazyAppendIterator extends \AppendIterator
14
{
15
    /**
16
     * @var \Iterator
17
     */
18
    private $iterators;
19
20
    /**
21
     * @param \Iterator $iterators
22
     */
23 3
    public function __construct(\Iterator $iterators)
24
    {
25 3
        parent::__construct();
26
27 3
        $this->iterators = $iterators;
28
29 3
        $iterators->rewind();
30 3
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 2
    public function current()
36
    {
37 2
        $this->lazyAppend();
38
39 2
        return parent::current();
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function key()
46
    {
47 2
        $this->lazyAppend();
48
49 2
        return parent::key();
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 2
    public function next()
56
    {
57 2
        $this->lazyAppend();
58
59 2
        parent::next();
60 2
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function rewind()
66
    {
67 2
        $this->lazyAppend();
68
69 2
        return parent::rewind();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 2
    public function valid()
76
    {
77 2
        $this->lazyAppend();
78
79 2
        return parent::valid();
80
    }
81
82
    /**
83
     * Lazily append the next iterator to the chain
84
     */
85 2
    private function lazyAppend()
86
    {
87 2
        if (!parent::valid() and $this->iterators->valid()) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (valid() instead of lazyAppend()). Are you sure this is correct? If so, you might want to change this to $this->valid().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
88 2
            $this->append($this->iterators->current());
89 2
            $this->iterators->next();
90 2
        }
91 2
    }
92
}
93