MapIterator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A current() 0 4 1
1
<?php
2
3
namespace itertools;
4
5
use InvalidArgumentException;
6
use IteratorIterator;
7
use Traversable;
8
use ArrayIterator;
9
10
11
/**
12
 * Iterator equivalent or [array_map](http://be1.php.net/manual/en/function.array-map.php).
13
 * Example:
14
 *     $positiveNumbers = new RangeIterator(0, INF); // all numbers from 0 to infinity
15
 *     $positiveSquareNumbers = new MapIterator($positiveNumbers, function($n) {return $n*$n;}); // all positive square numbers
16
 */
17
class MapIterator extends IteratorIterator
18
{
19
	protected $callback;
20
21
	public function __construct($iterator, $callback)
22
	{
23
		parent::__construct(IterUtil::asTraversable($iterator));
24
		if (!is_callable($callback)) {
25
			throw new InvalidArgumentException('The callback must be callable');
26
		}
27
		$this->callback = $callback;
28
	}
29
30
	public function current()
31
	{
32
		return call_user_func($this->callback, parent::current(), parent::key(), $this->getInnerIterator());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (key() instead of current()). Are you sure this is correct? If so, you might want to change this to $this->key().

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...
33
	}
34
}
35
36