Conditions | 5 |
Paths | 1 |
Total Lines | 39 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Tests | 24 |
CRAP Score | 5 |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
22 | 4 | public function __invoke(): Closure |
|
23 | { |
||
24 | /** @var Closure(iterable<TKey, T>): Generator<int, int<0,1>|float> $pipe */ |
||
25 | 4 | $pipe = (new Pipe())()( |
|
26 | 4 | (new Map())()( |
|
27 | /** |
||
28 | * @param T $_ |
||
29 | * @param iterable<TKey, T> $iterable |
||
30 | */ |
||
31 | 4 | static function (mixed $_, int $key, iterable $iterable): float { |
|
32 | 3 | $collection = Collection::fromIterable($iterable); |
|
33 | |||
34 | 3 | return $collection |
|
|
|||
35 | 3 | ->normalize() |
|
36 | 3 | ->squash() |
|
37 | 3 | ->limit($key + 1) |
|
38 | 3 | ->frequency() |
|
39 | 3 | ->map( |
|
40 | /** |
||
41 | * @param T $_ |
||
42 | */ |
||
43 | 3 | static fn (mixed $_, int $freq): float => $freq / ($key + 1) |
|
44 | 3 | ) |
|
45 | 3 | ->reduce( |
|
46 | 3 | static fn (float $acc, float $p, int $k): float => 0 === $key ? $acc : $acc - $p * log($p, 2) / ((1 === $k) ? 1 : log($k, 2)), |
|
47 | 3 | 0 |
|
48 | 3 | ); |
|
49 | 4 | } |
|
50 | 4 | ), |
|
51 | 4 | (new Map())()( |
|
52 | /** |
||
53 | * @return int<0,1>|float |
||
54 | */ |
||
55 | 4 | static fn (float $value): float|int => (0.0 === $value || 1.0 === $value) ? (int) $value : $value |
|
56 | 4 | ) |
|
57 | 4 | ); |
|
58 | |||
59 | // Point free style |
||
60 | 4 | return $pipe; |
|
61 | } |
||
63 |