SortByOperation::apply()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
rs 9.9666
c 0
b 0
f 0
cc 3
nc 1
nop 1
crap 3.0067
1
<?php
2
/**
3
 * File was created 12.06.2015 12:38
4
 *
5
 * @author Karsten J. Gerber <[email protected]>
6
 */
7
8
namespace PeekAndPoke\Component\Psi\Operation\FullSet;
9
10
use PeekAndPoke\Component\Psi\FullSetOperation;
11
use PeekAndPoke\Component\Psi\Operation\AbstractUnaryFunctionOperation;
12
13
/**
14
 * SortByOperation
15
 *
16
 * @author Karsten J. Gerber <[email protected]>
17
 */
18
class SortByOperation extends AbstractUnaryFunctionOperation implements FullSetOperation
19
{
20
    /**
21
     * @param \Iterator $set
22
     *
23
     * @return \Iterator
24
     */
25 2
    public function apply(\Iterator $set)
26
    {
27 2
        $func = $this->function;
28 2
        $data = iterator_to_array($set);
29
30 2
        usort($data, function ($i1, $i2) use ($func) {
31
32 2
            $val1 = $func($i1);
33 2
            $val2 = $func($i2);
34
35 2
            if ($val1 === $val2) {
36
                return 0;
37
            }
38
39 2
            return $val1 > $val2 ? 1 : -1;
40 2
        });
41
42 2
        return new \ArrayIterator($data);
43
    }
44
}
45