Passed
Push — master ( 81b119...2ec323 )
by Pol
02:20
created

Dispersion::__invoke()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 30
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 11
nc 1
nop 0
dl 0
loc 30
ccs 15
cts 15
cp 1
crap 5
rs 9.6111
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\collection\Operation;
6
7
use Closure;
8
use Generator;
9
10
/**
11
 * @immutable
12
 *
13
 * @template TKey
14
 * @template T
15
 */
16
final class Dispersion extends AbstractOperation
17
{
18
    /**
19
     * @return Closure(iterable<TKey, T>): Generator<int, float|int<0,1>>
20
     */
21 4
    public function __invoke(): Closure
22
    {
23
        /** @var Closure(iterable<TKey, T>): Generator<int, float|int<0,1>> $pipe */
24 4
        $pipe = (new Pipe())()(
25 4
            (new Normalize())(),
26 4
            (new ScanLeft())()(
27
                /**
28
                 * @param array<float|int<0,1>, T|null> $acc
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<float|int<0,1>, T|null> at position 4 could not be parsed: Expected '>' at position 4, but found 'int'.
Loading history...
29
                 * @param T $value
30
                 *
31
                 * @return array<float|int<0,1>, T>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<float|int<0,1>, T> at position 4 could not be parsed: Expected '>' at position 4, but found 'int'.
Loading history...
32
                 */
33 4
                static function (array $acc, mixed $value, int $key): array {
34 3
                    [$c, $v] = $acc;
35
36 3
                    return [(0 === $key) ? 0 : (($c * ($key - 1) + (($v === $value) ? 0 : 1)) / $key), $value];
37 4
                }
38 4
            )([0, null]),
39 4
            (new Drop())()(1),
40 4
            (new Map())()(
41
                /**
42
                 * @param array<float|int<0,1>, T> $value
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<float|int<0,1>, T> at position 4 could not be parsed: Expected '>' at position 4, but found 'int'.
Loading history...
43
                 *
44
                 * @return float|int<0,1>
45
                 */
46 4
                static fn (array $value): float|int => (0.0 === $value[0] || 1.0 === $value[0]) ? (int) $value[0] : $value[0]
47 4
            )
48 4
        );
49
50 4
        return $pipe;
51
    }
52
}
53