SliceIterator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 6
A valid() 0 17 5
A next() 0 5 1
A rewind() 0 5 1
1
<?php
2
/**
3
 * @copyright Zicht Online <http://zicht.nl>
4
 */
5
6
namespace Zicht\Itertools\lib;
7
8
use Zicht\Itertools\lib\Interfaces\FiniteIterableInterface;
9
use Zicht\Itertools\lib\Traits\FiniteIterableTrait;
10
11
class SliceIterator extends \IteratorIterator implements FiniteIterableInterface
12
{
13
    use FiniteIterableTrait;
14
15
    /** @var int */
16
    private $index;
17
18
    /** @var int */
19
    private $start;
20
21
    /** @var null|int */
22
    private $end;
23
24
    /**
25
     * @param \Iterator $iterable
26
     * @param int $start
27
     * @param null|int $end
28
     */
29 25
    public function __construct(\Iterator $iterable, $start, $end = null)
30
    {
31 25
        if ($start < 0 || $end < 0) {
32 9
            $length = iterator_count($iterable);
33
        } else {
34
            // length is not needed.  still, we will define it for code cleanliness
35 16
            $length = 0;
36
        }
37
38 25
        $this->index = 0;
39 25
        $this->start = $start < 0 ? $length + $start : $start;
40 25
        $this->end = $end === null ? null : ($end < 0 ? $length + $end : $end);
41 25
        parent::__construct($iterable);
42 25
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47 24
    public function valid()
48
    {
49 24
        while ($this->index < $this->start) {
50 9
            if (parent::valid()) {
51 9
                $this->index += 1;
52 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...
53
            } else {
54 1
                return false;
55
            }
56
        }
57
58 23
        if (null === $this->end || $this->index < $this->end) {
59 20
            return parent::valid();
60
        }
61
62 13
        return false;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 19
    public function next()
69
    {
70 19
        $this->index += 1;
71 19
        parent::next();
72 19
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77 24
    public function rewind()
78
    {
79 24
        $this->index = 0;
80 24
        parent::rewind();
81 24
    }
82
}
83