Dispersion::__invoke()   A
last analyzed

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 2
Bugs 1 Features 1
Metric Value
cc 5
eloc 11
c 2
b 1
f 1
nc 1
nop 0
dl 0
loc 30
ccs 15
cts 15
cp 1
crap 5
rs 9.6111
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<numeric-string|int<0,1>, T> $acc
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<numeric-string|int<0,1>, T> at position 2 could not be parsed: Unknown type name 'numeric-string' at position 2 in array<numeric-string|int<0,1>, T>.
Loading history...
29
                 * @param T $value
30
                 *
31
                 * @return list{float|int<0,1>, T}
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];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(0 === $key ...? 0 : 1 / $key, $value) returns the type array<integer,integer|mixed> which is incompatible with the documented return type loophp\collection\Operation\list.
Loading history...
37 4
                }
38 4
            )([0, null]),
39 4
            (new Drop())()(1),
40 4
            (new Map())()(
41
                /**
42
                 * @param array<numeric-string|int<0,1>, T> $value
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<numeric-string|int<0,1>, T> at position 2 could not be parsed: Unknown type name 'numeric-string' at position 2 in array<numeric-string|int<0,1>, T>.
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