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