ChunkedIterator::valid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Graze\DataDb\Helper;
4
5
use InvalidArgumentException;
6
use IteratorIterator;
7
use Traversable;
8
9
class ChunkedIterator extends IteratorIterator
10
{
11
    /** @var int Size of each chunk */
12
    protected $chunkSize;
13
14
    /** @var array Current chunk */
15
    protected $chunk;
16
17
    /** @var int */
18
    protected $key;
19
20
    /**
21
     * @param Traversable $iterator  Traversable iterator
22
     * @param int         $chunkSize Size to make each chunk
23
     */
24
    public function __construct(Traversable $iterator, $chunkSize)
25
    {
26
        $chunkSize = (int) $chunkSize;
27
        if ($chunkSize < 1) {
28
            throw new InvalidArgumentException("The chunk size must be equal or greater than zero; $chunkSize given");
29
        }
30
        parent::__construct($iterator);
31
        $this->chunkSize = $chunkSize;
32
    }
33
34
    public function rewind()
35
    {
36
        parent::rewind();
37
        $this->key = -1;
38
        $this->next();
39
    }
40
41
    /**
42
     * @return int|mixed
43
     */
44
    public function key()
45
    {
46
        return $this->key;
47
    }
48
49
    public function next()
50
    {
51
        $this->chunk = [];
52
        for ($i = 0; $i < $this->chunkSize && parent::valid(); $i++) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (valid() instead of next()). 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...
53
            $this->chunk[] = parent::current();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (current() instead of next()). Are you sure this is correct? If so, you might want to change this to $this->current().

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...
54
            parent::next();
55
        }
56
        $this->key++;
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function current()
63
    {
64
        return $this->chunk;
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function valid()
71
    {
72
        return (bool) $this->chunk;
73
    }
74
}
75