Passed
Push — master ( abdf29...71665b )
by
unknown
03:29 queued 43s
created

Reductions::sub()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 1
nop 0
dl 12
loc 12
rs 9.5555
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 5
1
<?php
2
/**
3
 * @copyright Zicht Online <http://zicht.nl>
4
 */
5
6
namespace Zicht\Itertools\util;
7
8
use Zicht\Itertools;
9
10
class Reductions
11
{
12
    /**
13
     * Returns a closure that adds two numbers together
14
     *
15
     * @return \Closure
16
     */
17 30 View Code Duplication
    public static function add()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
18
    {
19
        return function ($a, $b) {
20 23
            if (!is_numeric($a)) {
21 5
                throw new \InvalidArgumentException(sprintf('Argument $A must be numeric to perform addition, not %s', is_object($a) ? get_class($a) : gettype($a)));
22
            }
23 18
            if (!is_numeric($b)) {
24 5
                throw new \InvalidArgumentException(sprintf('Argument $B must be numeric to perform addition, not %s', is_object($b) ? get_class($b) : gettype($b)));
25
            }
26 13
            return $a + $b;
27 30
        };
28
    }
29
30
    /**
31
     * Returns a closure that subtracts one number from another
32
     *
33
     * @return \Closure
34
     */
35 20 View Code Duplication
    public static function sub()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
36
    {
37
        return function ($a, $b) {
38 19
            if (!is_numeric($a)) {
39 5
                throw new \InvalidArgumentException(sprintf('Argument $A must be numeric to perform subtraction, not %s', is_object($a) ? get_class($a) : gettype($a)));
40
            }
41 14
            if (!is_numeric($b)) {
42 5
                throw new \InvalidArgumentException(sprintf('Argument $B must be numeric to perform subtraction, not %s', is_object($b) ? get_class($b) : gettype($b)));
43
            }
44 9
            return $a - $b;
45 20
        };
46
    }
47
48
    /**
49
     * Returns a closure that multiplies two numbers
50
     *
51
     * @return \Closure
52
     */
53 18 View Code Duplication
    public static function mul()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
54
    {
55
        return function ($a, $b) {
56 18
            if (!is_numeric($a)) {
57 5
                throw new \InvalidArgumentException(sprintf('Argument $A must be numeric to perform multiplication, not %s', is_object($a) ? get_class($a) : gettype($a)));
58
            }
59 13
            if (!is_numeric($b)) {
60 5
                throw new \InvalidArgumentException(sprintf('Argument $B must be numeric to perform multiplication, not %s', is_object($b) ? get_class($b) : gettype($b)));
61
            }
62 8
            return $a * $b;
63 18
        };
64
    }
65
66
    /**
67
     * Returns a closure that returns the smallest of two numbers
68
     *
69
     * @return \Closure
70
     */
71 19 View Code Duplication
    public static function min()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
72
    {
73
        return function ($a, $b) {
74 19
            if (!(is_numeric($a) || $a instanceof \DateTime)) {
75 5
                throw new \InvalidArgumentException(sprintf('Argument $A must be numeric to determine minimum, not %s', is_object($a) ? get_class($a) : gettype($a)));
76
            }
77 14
            if (!(is_numeric($b) || $b instanceof \DateTime)) {
78 5
                throw new \InvalidArgumentException(sprintf('Argument $B must be numeric to determine minimum, not %s', is_object($b) ? get_class($b) : gettype($b)));
79
            }
80 9
            return $a < $b ? $a : $b;
81 19
        };
82
    }
83
84
    /**
85
     * Returns a closure that returns the largest of two numbers
86
     *
87
     * @return \Closure
88
     */
89 19 View Code Duplication
    public static function max()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
90
    {
91
        return function ($a, $b) {
92 19
            if (!(is_numeric($a) || $a instanceof \DateTime)) {
93 5
                throw new \InvalidArgumentException(sprintf('Argument $A must be numeric to determine maximum, not %s', is_object($a) ? get_class($a) : gettype($a)));
94
            }
95 14
            if (!(is_numeric($b) || $b instanceof \DateTime)) {
96 5
                throw new \InvalidArgumentException(sprintf('Argument $B must be numeric to determine maximum, not %s', is_object($b) ? get_class($b) : gettype($b)));
97
            }
98 9
            return $a < $b ? $b : $a;
99 19
        };
100
    }
101
102
    /**
103
     * Returns a closure that concatenates two strings using $glue
104
     *
105
     * @param string $glue
106
     * @return \Closure
107
     */
108 22
    public static function join($glue = '')
109
    {
110 22
        if (!is_string($glue)) {
111 5
            throw new \InvalidArgumentException(sprintf('Argument $GLUE must be a string to join, not %s', is_object($glue) ? get_class($glue) : gettype($glue)));
112
        }
113 17
        return function ($a, $b) use ($glue) {
114 17
            if (!is_string($a)) {
115 6
                throw new \InvalidArgumentException(sprintf('Argument $A must be a string to join, not %s', is_object($a) ? get_class($a) : gettype($a)));
116
            }
117 11
            if (!is_string($b)) {
118 6
                throw new \InvalidArgumentException(sprintf('Argument $B must be a string to join, not %s', is_object($b) ? get_class($b) : gettype($b)));
119
            }
120 5
            return \join($glue, [$a, $b]);
121 17
        };
122
    }
123
124
    /**
125
     * @deprecated please use the reduction functions directly, will be removed in version 3.0
126
     * @param string $name
127
     * @param null $default
128
     * @return \Closure|null
129
     */
130
    public static function getReduction($name, $default = null)
131
    {
132
        switch ($name) {
133
            case 'add':
134
                return self::add();
135
            case 'sub':
136
                return self::sub();
137
            case 'mul':
138
                return self::mul();
139
            case 'min':
140
                return self::min();
141
            case 'max':
142
                return self::max();
143
            case 'join':
144
                return self::join();
145
            default:
146
                return $default;
147
        }
148
    }
149
}
150