Passed
Push — master ( 9e9b54...8c04f5 )
by
unknown
38s
created

SliceIterator::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 1
b 0
f 1
1
<?php
2
/**
3
 * @author Boudewijn Schoon <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Itertools\lib;
8
9
use Zicht\Itertools\lib\Interfaces\FiniteIterableInterface;
10
use Zicht\Itertools\lib\Traits\FiniteIterableTrait;
11
12
/**
13
 * Class SliceIterator
14
 *
15
 * @package Zicht\Itertools\lib
16
 */
17
class SliceIterator extends \IteratorIterator implements FiniteIterableInterface
18
{
19
    use FiniteIterableTrait;
20
21
    /** @var integer */
22
    private $index;
23
24
    /** @var integer */
25
    private $start;
26
27
    /** @var null|int */
28
    private $end;
29
30
    /**
31
     * SliceIterator constructor.
32
     *
33
     * @param \Iterator $iterable
34
     * @param integer $start
35
     * @param null|integer $end
36
     */
37 25
    public function __construct(\Iterator $iterable, $start, $end = null)
38
    {
39 25
        if ($start < 0 || $end < 0) {
40 9
            $length = iterator_count($iterable);
41
        } else {
42
            // length is not needed.  still, we will define it for code cleanliness
43 16
            $length = 0;
44
        }
45
46 25
        $this->index = 0;
47 25
        $this->start = $start < 0 ? $length + $start : $start;
48 25
        $this->end = $end === null ? null : ($end < 0 ? $length + $end : $end);
49 25
        parent::__construct($iterable);
50 25
    }
51
52
    /**
53
     * @{inheritDoc}
54
     */
55 24
    public function valid()
56
    {
57 24
        while ($this->index < $this->start) {
58 9
            if (parent::valid()) {
59 9
                $this->index += 1;
60 9
                parent::next();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (next() instead of valid()). Are you sure this is correct? If so, you might want to change this to $this->next().

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...
61
            } else {
62 1
                return false;
63
            }
64
        }
65
66 23
        if (null === $this->end || $this->index < $this->end) {
67 20
            return parent::valid();
68
        }
69
70 13
        return false;
71
    }
72
73
    /**
74
     * @{inheritDoc}
75
     */
76 19
    public function next()
77
    {
78 19
        $this->index += 1;
79 19
        parent::next();
80 19
    }
81
82
    /**
83
     * @{inheritDoc}
84
     */
85 24
    public function rewind()
86
    {
87 24
        $this->index = 0;
88 24
        parent::rewind();
89 24
    }
90
}
91