Passed
Pull Request — master (#19)
by
unknown
03:59
created

MapIterator::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Zicht\Itertools\lib;
4
5
use Zicht\Itertools\lib\Traits\AllTrait;
6
use Zicht\Itertools\lib\Traits\AnyTrait;
7
use Zicht\Itertools\lib\Traits\ArrayAccessTrait;
8
use Zicht\Itertools\lib\Traits\ChainTrait;
9
use Zicht\Itertools\lib\Traits\CountableTrait;
10
use Zicht\Itertools\lib\Traits\CycleTrait;
11
use Zicht\Itertools\lib\Traits\DebugInfoTrait;
12
use Zicht\Itertools\lib\Traits\FilterTrait;
13
use Zicht\Itertools\lib\Traits\FirstTrait;
14
use Zicht\Itertools\lib\Traits\GetterTrait;
15
use Zicht\Itertools\lib\Traits\GroupByTrait;
16
use Zicht\Itertools\lib\Traits\ItemsTrait;
17
use Zicht\Itertools\lib\Traits\KeysTrait;
18
use Zicht\Itertools\lib\Traits\LastTrait;
19
use Zicht\Itertools\lib\Traits\MapByTrait;
20
use Zicht\Itertools\lib\Traits\MapTrait;
21
use Zicht\Itertools\lib\Traits\ReduceTrait;
22
use Zicht\Itertools\lib\Traits\ReversedTrait;
23
use Zicht\Itertools\lib\Traits\SliceTrait;
24
use Zicht\Itertools\lib\Traits\SortedTrait;
25
use Zicht\Itertools\lib\Traits\ToArrayTrait;
26
use Zicht\Itertools\lib\Traits\UniqueTrait;
27
use Zicht\Itertools\lib\Traits\ValuesTrait;
28
use Zicht\Itertools\lib\Traits\ZipTrait;
29
30
class MapIterator extends \MultipleIterator implements \Countable, \ArrayAccess
31
{
32
    use ArrayAccessTrait;
33
    use CountableTrait;
34
    use DebugInfoTrait;
35
    use GetterTrait;
36
37
    // Fluent interface traits
38
    use AllTrait;
39
    use AnyTrait;
40
    use ChainTrait;
41
    use CycleTrait;
42
    use FilterTrait;
43
    use FirstTrait;
44
    use GroupByTrait;
45
    use ItemsTrait;
46
    use KeysTrait;
47
    use LastTrait;
48
    use MapByTrait;
49
    use MapTrait;
50
    use ReduceTrait;
51
    use ReversedTrait;
52
    use SliceTrait;
53
    use SortedTrait;
54
    use ToArrayTrait;
55
    use UniqueTrait;
56
    use ValuesTrait;
57
    use ZipTrait;
58
59
    private $valueFunc;
60
    private $keyFunc;
61
62 63
    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...
63
    {
64 63
        parent::__construct(\MultipleIterator::MIT_NEED_ALL| \MultipleIterator::MIT_KEYS_NUMERIC);
65 63
        $args = func_get_args();
66 63
        $argsContainsKeyFunc = $args[1] instanceof \Closure;
67 63
        $this->valueFunc = $args[0];
68
        $this->keyFunc = $argsContainsKeyFunc ? $args[1] : function () { return $this->genericKeysToKey(func_get_args()); };
69 63
        foreach (array_slice($args, $argsContainsKeyFunc ? 2 : 1) as $iterable) {
70 63
            if (!$iterable instanceof \Iterator) {
71 1
                throw new \InvalidArgumentException(sprintf('Not all arguments are iterators'));
72
            }
73 62
            $this->attachIterator($iterable);
74
        }
75 62
    }
76
77 59
    protected function genericKeysToKey($keysAndValues)
78
    {
79 59
        $keys = array_splice($keysAndValues, 0, count($keysAndValues) / 2);
80
81 59
        if (count($keys) == 1) {
82 46
            return $keys[0];
83
        }
84
85 13
        $value = $keys[0];
86 13
        foreach ($keys as $key) {
87 13
            if ($key !== $value) {
88
                // the keys are different, we will make a new string identifying this entry
89
                return join(':', array_map(function ($key) { return (string)$key; }, $keys));
90
            }
91
        }
92
93
        // all values are the same, use it
94 9
        return $value;
95
    }
96
97 59
    public function current()
98
    {
99 59
        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...
100
    }
101
102 59
    public function key()
103
    {
104 59
        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...
105
    }
106
107 59
    public function next()
108
    {
109 59
        parent::next();
110 59
    }
111
}
112