RangeIterator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 49
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A rewind() 0 5 1
A key() 0 4 1
A current() 0 4 1
A next() 0 4 1
A valid() 0 9 3
1
<?php
2
3
namespace itertools;
4
5
use Iterator;
6
7
/**
8
 * Iterator equivalent of [array_unique](http://be1.php.net/manual/en/function.array-unique.php)
9
 * but only works for sorted input.
10
 * Example:
11
 *     Iterator equivalent of [range](http://be1.php.net/manual/en/function.range.php).
12
 *     $lines = new SliceIterator(new FileLineIterator('file.txt'), 0, 1000); // will iterate the first 1000 lines of the file
13
 */
14
class RangeIterator implements Iterator
15
{
16
	protected $start;
17
	protected $end;
18
	protected $step;
19
	protected $currentValue;
20
	protected $iterationCount;
21
22
	public function __construct($start, $end = null, $step = 1)
23
	{
24
		if(null === $end) {
25
			$end = $step > 0 ? INF : -INF;
26
		}
27
		$this->start = $start;
28
		$this->end = $end;
29
		$this->step = $step;
30
	}
31
32
    public function rewind()
33
	{
34
		$this->currentValue = $this->start;
35
		$this->iterationCount = 0;
36
    }
37
38
    public function key()
39
	{
40
		return $this->iterationCount++;
41
    }
42
43
    public function current()
44
	{
45
		return $this->currentValue;
46
    }
47
48
    public function next()
49
	{
50
		$this->currentValue += $this->step;
51
    }
52
53
    public function valid()
54
	{
55
		if($this->step > 0) {
56
			return $this->currentValue <= $this->end;
57
		} else if($this->step < 0) {
58
			return $this->currentValue >= $this->end;
59
		}
60
		return true;
61
    }
62
}
63
64