AccumulateTrait::accumulate()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Zicht Online <http://zicht.nl>
4
 */
5
6
namespace Zicht\Itertools\lib\Traits;
7
8
use Zicht\Itertools\reductions;
9
use Zicht\Itertools\lib\AccumulateIterator;
10
11
trait AccumulateTrait
12
{
13
    /**
14
     * Returns an iterator that containing accumulated elements
15
     *
16
     * If the optional $closure argument is supplied, it should be a string:
17
     * add, sub, mul, min, or max.  Or it can be a Closure taking two
18
     * arguments that will be used to instead of addition.
19
     *
20
     * > iter\iterable([1,2,3,4,5])->accumulate()
21
     * 1 3 6 10 15
22
     *
23
     * > iter\iterable(['One', 'Two', 'Three'])->accumulate(function ($a, $b) { return $a . $b; })
24
     * 'One' 'OneTwo' 'OneTwoThree'
25
     *
26
     * @param string|\Closure $closure
27
     * @return null|AccumulateIterator
28
     */
29 16
    public function accumulate($closure = 'add')
30
    {
31 16
        if ($this instanceof \Iterator) {
32 15
            return new AccumulateIterator(
33 15
                $this,
34 15
                $closure instanceof \Closure ? $closure : reductions\get_reduction($closure)
0 ignored issues
show
Deprecated Code introduced by
The function Zicht\Itertools\reductions\get_reduction() has been deprecated with message: please use the reduction functions directly, 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...
35
            );
36
        }
37
38 1
        return null;
39
    }
40
}
41