MapTrait::map()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 19
ccs 11
cts 11
cp 1
crap 3
rs 9.6333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Zicht Online <http://zicht.nl>
4
 */
5
6
// phpcs:disable Zicht.Commenting.FunctionComment.ExtraParamComment
7
8
namespace Zicht\Itertools\lib\Traits;
9
10
use Zicht\Itertools\conversions;
11
use Zicht\Itertools\lib\MapIterator;
12
13
trait MapTrait
14
{
15
    /**
16
     * Make an iterator that applies $strategy to every entry in this iterable
17
     *
18
     * If additional iterables are passed, $strategy is called for each entry
19
     * in the $iterable, where the first argument is the value and the
20
     * second argument is the key of the entry
21
     *
22
     * If additional iterables are passed, $strategy is called with the
23
     * values and the keys from the iterables. For example, the first
24
     * call to $strategy will be:
25
     * $strategy($value_iterable1, $value_iterable2, $key_iterable2, $key_iterable2)
26
     *
27
     * With multiple iterables, the iterator stops when the shortest
28
     * iterable is exhausted.
29
     *
30
     * > $minimal = function ($value) { return min(3, $value); };
31
     * > iter\iterable([1, 2, 3, 4])->map($minimal);
32
     * 3 3 3 4
33
     *
34
     * > $average = function ($value1, $value2) { return ($value1 + $value2) / 2; };
35
     * > iter\iterable([1, 2, 3])->map($average, [4, 5, 6]);
36
     * 2.5 3.5 4.5
37
     *
38
     * @param null|string|\Closure $strategy
39
     * @param array|string|\Iterator $iterable
0 ignored issues
show
Bug introduced by
There is no parameter named $iterable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
40
     * @param array|string|\Iterator $iterable2
0 ignored issues
show
Bug introduced by
There is no parameter named $iterable2. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
41
     * @return MapIterator
42
     */
43 86
    public function map($strategy /*, $iterable, $iterable2, ... */)
44
    {
45 86
        if ($this instanceof \Iterator) {
46 85
            if (func_num_args() > 1) {
47 13
                $iterables = array_map(
48 13
                    '\Zicht\Itertools\conversions\mixed_to_iterator',
49 13
                    array_slice(func_get_args(), 1)
50
                );
51
            } else {
52 72
                $iterables = [];
53
            }
54 85
            $reflectorClass = new \ReflectionClass('\Zicht\Itertools\lib\MapIterator');
55 85
            return $reflectorClass->newInstanceArgs(
56 85
                array_merge([conversions\mixed_to_value_getter($strategy), $this], $iterables)
0 ignored issues
show
Deprecated Code introduced by
The function Zicht\Itertools\conversi...mixed_to_value_getter() has been deprecated with message: Use \Zicht\Itertools\util\Conversions::mixedToClosure, will be removed in version 3.0

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
57
            );
58
        }
59
60 1
        return null;
61
    }
62
}
63