CurrentCachedIterator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A invalidateCache() 0 5 1
A rewind() 0 5 1
A uncachedCurrent() 0 4 1
A current() 0 8 2
A next() 0 5 1
A uncachedValid() 0 4 1
A valid() 0 8 2
1
<?php
2
3
namespace itertools;
4
5
use IteratorIterator;
6
7
8
class CurrentCachedIterator extends IteratorIterator
9
{
10
	protected $cachedCurrent;
11
	protected $currentUpToDate;
12
	protected $cachedValid;
13
	protected $validUpToDate;
14
15
	public function __construct($innerIterator)
16
	{
17
		parent::__construct(IterUtil::asIterator($innerIterator));
18
		$this->currentUpToDate = false;
19
		$this->validUpToDate = false;
20
	}
21
22
	public function invalidateCache()
23
	{
24
		$this->currentUpToDate = false;
25
		$this->validUpToDate = false;
26
	}
27
28
    public function rewind()
29
	{
30
		$this->invalidateCache();
31
		return parent::rewind();
32
    }
33
34
	public function uncachedCurrent()
35
	{
36
		return parent::current();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (current() instead of uncachedCurrent()). 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...
37
	}
38
39
    public function current()
40
	{
41
		if(!$this->currentUpToDate) {
42
			$this->cachedCurrent = $this->uncachedCurrent();
43
			$this->currentUpToDate = true;
44
		}
45
        return $this->cachedCurrent;
46
    }
47
48
    public function next()
49
	{
50
		$this->invalidateCache();
51
		return parent::next();
52
    }
53
54
	public function uncachedValid()
55
	{
56
		return parent::valid();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (valid() instead of uncachedValid()). 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...
57
	}
58
59
    public function valid()
60
	{
61
		if($this->validUpToDate) {
62
			return $this->cachedValid;
63
		}
64
		$this->validUpToDate = true;
65
        return $this->cachedValid = $this->uncachedValid();
66
    }
67
}
68
69