Completed
Push — master ( 1857e6...d96bec )
by Joao
03:02
created

src/Repository/ArrayDatasetIterator.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace ByJG\AnyDataset\Repository;
4
5
use ByJG\AnyDataset\Repository\AnyDataset;
6
use ByJG\AnyDataset\Repository\GenericIterator;
7
use ByJG\AnyDataset\Repository\IteratorInterface;
8
use ByJG\AnyDataset\Repository\SingleRow;
9
use InvalidArgumentException;
10
11
class ArrayDatasetIterator extends GenericIterator
12
{
13
14
    /**
15
     * @var array
16
     */
17
    protected $_rows;
18
19
    /**
20
     * Enter description here...
21
     *
22
     * @var array
23
     */
24
    protected $_keys;
25
26
    /**
27
      /* @var int
28
     */
29
    protected $_currentRow;
30
31
    /**
32
     * @return IteratorInterface
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
33
     */
34
    public function __construct($rows)
35
    {
36
        if (!is_array($rows)) {
37
            throw new InvalidArgumentException("ArrayDatasetIterator must receive an array");
38
        }
39
        $this->_currentRow = 0;
40
        $this->_rows = $rows;
41
        $this->_keys = array_keys($rows);
42
    }
43
44
    /**
45
     * @return int
46
     */
47
    public function count()
48
    {
49
        return count($this->_rows);
50
    }
51
52
    /**
53
     * @return bool
54
     */
55
    public function hasNext()
56
    {
57
        return ($this->_currentRow < $this->count());
58
    }
59
60
    /**
61
     * @return SingleRow
62
     */
63
    public function moveNext()
64
    {
65
        if ($this->hasNext()) {
66
            $cols = array();
0 ignored issues
show
$cols is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
67
            $key = $this->_keys[$this->_currentRow];
68
            $cols = $this->_rows[$key];
69
70
            $any = new AnyDataset();
71
            $any->appendRow();
72
            $any->addField("__id", $this->_currentRow);
73
            $any->addField("__key", $key);
74
            foreach ($cols as $key => $value) {
75
                $any->addField(strtolower($key), $value);
76
            }
77
            $it = $any->getIterator(null);
78
            $sr = $it->moveNext();
79
            $this->_currentRow++;
80
            return $sr;
81
        } else {
82
            return null;
83
        }
84
    }
85
86
    function key()
87
    {
88
        return $this->_currentRow;
89
    }
90
}
91