Test Failed
Push — master ( 51fa41...9871c3 )
by
unknown
03:53
created

reductions.php ➔ max()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Boudewijn Schoon <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Itertools\reductions;
8
9
use Zicht\Itertools\lib\ChainIterator;
10
11
/**
12
 * Returns a closure that adds two numbers together
13
 *
14
 * @return \Closure
15
 */
16
function add()
17
{
18
    return function ($a, $b) {
19 23
        return $a + $b;
20 30
    };
21
}
22
23
/**
24
 * Returns a closure that subtracts one number from another
25
 *
26
 * @return \Closure
27
 */
28
function sub()
29
{
30
    return function ($a, $b) {
31 19
        return $a - $b;
32 20
    };
33
}
34
35
/**
36
 * Returns a closure that multiplies two numbers
37
 *
38
 * @return \Closure
39
 */
40
function mul()
41
{
42
    return function ($a, $b) {
43 18
        return $a * $b;
44 18
    };
45
}
46
47
/**
48
 * Returns a closure that returns the smallest of two numbers
49
 *
50
 * @return \Closure
51
 */
52
function min()
53
{
54
    return function ($a, $b) {
55 18
        return $a < $b ? $a : $b;
56 18
    };
57
}
58
59
/**
60
 * Returns a closure that returns the largest of two numbers
61
 *
62
 * @return \Closure
63
 */
64
function max()
65
{
66
    return function ($a, $b) {
67 18
        return $a < $b ? $b : $a;
68 18
    };
69
}
70
71
/**
72
 * Returns a closure that concatenates two strings using $glue
73
 *
74
 * @param string $glue
75
 * @return \Closure
76
 */
77
function join($glue = '')
78
{
79 22 View Code Duplication
    if (!is_string($glue)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80 5
        throw new \InvalidArgumentException(sprintf('Argument $GLUE must be a string to join, not %s', is_object($glue) ? get_class($glue) : gettype($glue)));
81
    }
82
    return function ($a, $b) use ($glue) {
83 17 View Code Duplication
        if (!is_string($a)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84 6
            throw new \InvalidArgumentException(sprintf('Argument $A must be a string to join, not %s', is_object($a) ? get_class($a) : gettype($a)));
85
        }
86 11 View Code Duplication
        if (!is_string($b)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87 6
            throw new \InvalidArgumentException(sprintf('Argument $B must be a string to join, not %s', is_object($b) ? get_class($b) : gettype($b)));
88
        }
89 5
        return \join($glue, [$a, $b]);
90 17
    };
91
}
92
93
/**
94
 * Returns a closure that chains lists together
95
 *
96
 * > $lists = [[1, 2, 3], [4, 5, 6]]
97
 * > iterable($lists)->reduce(reductions\chain(), new ChainIterator())
98
 * results in a ChainIterator: 1, 2, 3, 4, 5, 6
99
 *
100
 * @return \Closure
101
 */
102
function chain()
103
{
104
    return function ($chainIterator, $b) {
105 4
        if (!($chainIterator instanceof ChainIterator)) {
106 1
            throw new \InvalidArgumentException('Argument $A must be a ChainIterator.  Did your call "reduce" with "new ChainIterator()" as the initial parameter?');
107
        }
108
109 3
        $chainIterator->extend($b);
110 3
        return $chainIterator;
111 5
    };
112
}
113
114
/**
115
 * Returns a reduction closure
116
 *
117
 * @param string $name
118
 * @return \Closure
119
 * @throws \InvalidArgumentException
120
 *
121
 * @deprecated please use the reduction functions directly, will be removed in version 3.0
122
 */
123
function get_reduction($name)
124
{
125
    if (is_string($name)) {
126
        switch ($name) {
127
            case 'add':
128
                return add();
129
            case 'sub':
130
                return sub();
131
            case 'mul':
132
                return mul();
133
            case 'min':
134
                return min();
135
            case 'max':
136
                return max();
137
            case 'join':
138
                return call_user_func_array('\Zicht\Itertools\reductions\join', array_slice(func_get_args(), 1));
139
            case 'chain':
140
                return chain();
141
        }
142
    }
143
144
    throw new \InvalidArgumentException(sprintf('$NAME "%s" is not a valid reduction.', $name));
145
}
146
147
/**
148
 * @deprecated please use the reduction functions directly, will be removed in version 3.0
149
 */
150
function getReduction($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
151
{
152
    return call_user_func_array('\Zicht\Itertools\reductions\get_reduction', func_get_args());
153
}
154