ReverseListIterator::remove()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
namespace Jmw\Collection\Lists;
3
4
use Jmw\Collection\IteratorInterface;
5
use Jmw\Collection\Exception\NoSuchElementException;
6
use Jmw\Collection\Exception\IllegalStateException;
7
/**
8
 * A ReverseListIterator starts at the end of a list and works its way to the beginning
9
 * @author john
10
 *
11
 */
12
class ReverseListIterator implements IteratorInterface
13
{
14
15
	/**
16
	 * 
17
	 * @var ListInterface
18
	 */
19
	protected $list;
20
21
	/**
22
	 * 
23
	 * @var int
24
	 */
25
	protected $index;
26
	
27
	/**
28
	 * 
29
	 * @var boolean
30
	 */
31
	protected $canRemove;
32
33
	/**
34
	 * Construct a ReverseListIterator
35
	 * @param ListInterface $list
36
	 */
37
	public function __construct(ListInterface $list)
38
	{
39
		$this->index = $list->size();
40
		$this->list = $list;
41
	}
42
43
	/**
44
	 * Returns true if this list iterator has more elements when traversing the list in the backward direction.
45
	 * @return boolean
46
	 */
47 View Code Duplication
	public function next()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
	{
49
		if ($this->hasNext())
50
		{
51
			$this->canRemove = true;
52
			return $this->list->get(--$this->index);
53
		}
54
		else
55
		{
56
			throw new NoSuchElementException("Could not find element at index {$this->index}");
57
		}
58
	}
59
60
	/**
61
	 * Returns the next element in the list and advances the cursor position.
62
	 * @return multitype
63
	 * @throws NoSuchElementException
64
	 */
65
	public function hasNext()
66
	{
67
		return $this->index > 0;
68
	}
69
70
	/**
71
	 * Removes from the list the last element that was returned by next()
72
	 * @return void
73
	 * @throws IllegalStateException
74
	 */
75 View Code Duplication
	public function remove()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
	{
77
		if($this->canRemove)
78
		{
79
			$this->list->removeAt($this->index);
80
			$this->canRemove = false;
81
		}
82
		else
83
		{
84
			throw new IllegalStateException("Cannot remove element unless next has been called");
85
		}
86
	}
87
}