MapIterator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 98
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0
wmc 12
lcom 2
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 4 1
A __construct() 0 22 5
A genericKeysToKey() 0 27 4
A current() 0 4 1
A next() 0 4 1
1
<?php
2
/**
3
 * @copyright Zicht Online <http://zicht.nl>
4
 */
5
6
namespace Zicht\Itertools\lib;
7
8
use Zicht\Itertools\lib\Interfaces\FiniteIterableInterface;
9
use Zicht\Itertools\lib\Traits\FiniteIterableTrait;
10
11
class MapIterator extends \MultipleIterator implements FiniteIterableInterface
12
{
13
    use FiniteIterableTrait;
14
15
    /** @var \Closure */
16
    private $valueFunc;
17
18
    /** @var \Closure */
19
    private $keyFunc;
20
21
    /**
22
     * @param \Closure $valueFunc
23
     */
24 90
    public function __construct(\Closure $valueFunc /* [\Closure $keyFunc], \Iterator $iterable1, [\Iterator $iterable2, [...]] */)
25
    {
26 90
        parent::__construct(\MultipleIterator::MIT_NEED_ALL | \MultipleIterator::MIT_KEYS_NUMERIC);
27 90
        $args = func_get_args();
28 90
        $argsContainsKeyFunc = $args[1] instanceof \Closure;
29 90
        $this->valueFunc = $args[0];
30
31 90
        if ($argsContainsKeyFunc) {
32 1
            $this->keyFunc = $args[1];
33
        } else {
34
            $this->keyFunc = function () {
35 81
                return $this->genericKeysToKey(func_get_args());
36
            };
37
        }
38
39 90
        foreach (array_slice($args, $argsContainsKeyFunc ? 2 : 1) as $iterable) {
40 90
            if (!$iterable instanceof \Iterator) {
41 1
                throw new \InvalidArgumentException(sprintf('Not all arguments are iterators'));
42
            }
43 89
            $this->attachIterator($iterable);
44
        }
45 89
    }
46
47
    /**
48
     * Compute the key for an element
49
     *
50
     * When multiple iterables are given, the key for an element is computed by taking
51
     * the keys of the element of all iterables, when they are the same, this key is used,
52
     * otherwise a compound key is created by concatenating the keys together.
53
     *
54
     * @param array $keysAndValues
55
     * @return mixed
56
     */
57 81
    protected function genericKeysToKey($keysAndValues)
58
    {
59 81
        $keys = array_splice($keysAndValues, 0, count($keysAndValues) / 2);
60
61 81
        if (count($keys) == 1) {
62 68
            return $keys[0];
63
        }
64
65 13
        $value = $keys[0];
66 13
        foreach ($keys as $key) {
67 13
            if ($key !== $value) {
68
                // the keys are different, we will make a new string identifying this entry
69 5
                return join(
70 5
                    ':',
71 5
                    array_map(
72 5
                        function ($key) {
73 5
                            return (string)$key;
74 5
                        },
75 13
                        $keys
76
                    )
77
                );
78
            }
79
        }
80
81
        // all values are the same, use it
82 9
        return $value;
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88 82
    public function current()
89
    {
90 82
        return call_user_func_array($this->valueFunc, array_merge(parent::current(), parent::key()));
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...
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96 82
    public function key()
97
    {
98 82
        return call_user_func_array($this->keyFunc, array_merge(parent::key(), parent::current()));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (current() instead of key()). 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...
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104 82
    public function next()
105
    {
106 82
        parent::next();
107 82
    }
108
}
109