Passed
Push — master ( 9e9b54...8c04f5 )
by
unknown
38s
created

MapIterator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 22 5
B genericKeysToKey() 0 27 4
A current() 0 4 1
A key() 0 4 1
A next() 0 4 1
1
<?php
2
/**
3
 * @author Boudewijn Schoon <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Itertools\lib;
8
9
use Zicht\Itertools\lib\Interfaces\FiniteIterableInterface;
10
use Zicht\Itertools\lib\Traits\FiniteIterableTrait;
11
12
/**
13
 * Class MapIterator
14
 *
15
 * @package Zicht\Itertools\lib
16
 */
17
class MapIterator extends \MultipleIterator implements FiniteIterableInterface
18
{
19
    use FiniteIterableTrait;
20
21
    /** @var \Closure */
22
    private $valueFunc;
23
24
    /** @var \Closure */
25
    private $keyFunc;
26
27
    /**
28
     * MapIterator constructor.
29
     *
30
     * @param \Closure $valueFunc
31
     */
32 72
    public function __construct(\Closure $valueFunc /* [\Closure $keyFunc], \Iterator $iterable1, [\Iterator $iterable2, [...]] */)
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
    {
34 72
        parent::__construct(\MultipleIterator::MIT_NEED_ALL | \MultipleIterator::MIT_KEYS_NUMERIC);
35 72
        $args = func_get_args();
36 72
        $argsContainsKeyFunc = $args[1] instanceof \Closure;
37 72
        $this->valueFunc = $args[0];
38
39 72
        if ($argsContainsKeyFunc) {
40 1
            $this->keyFunc = $args[1];
41
        } else {
42
            $this->keyFunc = function () {
43 65
                return $this->genericKeysToKey(func_get_args());
44
            };
45
        }
46
47 72
        foreach (array_slice($args, $argsContainsKeyFunc ? 2 : 1) as $iterable) {
48 72
            if (!$iterable instanceof \Iterator) {
49 1
                throw new \InvalidArgumentException(sprintf('Not all arguments are iterators'));
50
            }
51 71
            $this->attachIterator($iterable);
52
        }
53 71
    }
54
55
    /**
56
     * Compute the key for an element
57
     *
58
     * When multiple iterables are given, the key for an element is computed by taking
59
     * the keys of the element of all iterables, when they are the same, this key is used,
60
     * otherwise a compound key is created by concatenating the keys together.
61
     *
62
     * @param array $keysAndValues
63
     * @return mixed
64
     */
65 65
    protected function genericKeysToKey($keysAndValues)
66
    {
67 65
        $keys = array_splice($keysAndValues, 0, count($keysAndValues) / 2);
68
69 65
        if (count($keys) == 1) {
70 52
            return $keys[0];
71
        }
72
73 13
        $value = $keys[0];
74 13
        foreach ($keys as $key) {
75 13
            if ($key !== $value) {
76
                // the keys are different, we will make a new string identifying this entry
77 5
                return join(
78 5
                    ':',
79
                    array_map(
80 13
                        function ($key) {
81 5
                            return (string)$key;
82 13
                        },
83
                        $keys
84
                    )
85
                );
86
            }
87
        }
88
89
        // all values are the same, use it
90 9
        return $value;
91
    }
92
93
    /**
94
     * @{inheritDoc}
95
     */
96 66
    public function current()
97
    {
98 66
        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...
99
    }
100
101
    /**
102
     * @{inheritDoc}
103
     */
104 66
    public function key()
105
    {
106 66
        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...
107
    }
108
109
    /**
110
     * @{inheritDoc}
111
     */
112 66
    public function next()
113
    {
114 66
        parent::next();
115 66
    }
116
}
117