Product::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 69
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 20
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 69
ccs 28
cts 28
cp 1
crap 1
rs 9.6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\collection\Operation;
6
7
use Closure;
8
use Generator;
9
use loophp\iterators\IterableIteratorAggregate;
10
use Traversable;
11
12
/**
13
 * @immutable
14
 *
15
 * @template TKey
16
 * @template T
17
 */
18
final class Product extends AbstractOperation
19
{
20
    /**
21
     * @template UKey
22
     * @template U
23
     */
24 4
    public function __invoke(): Closure
25
    {
26 4
        return
27
            /**
28
             * @param iterable<UKey, U> ...$iterables
29
             *
30
             * @return Closure(iterable<TKey, T>): Generator<int, array<array-key, T|U>>
31
             */
32 4
            static function (iterable ...$iterables): Closure {
33
                /** @var Closure(iterable<TKey, T>): Generator<int, list<T|U>> $pipe */
34 4
                $pipe = (new Pipe())()(
35 4
                    (
36
                        /**
37
                         * @param array<int, Traversable<UKey, U>> $iterables
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, Traversable<UKey, U>> at position 4 could not be parsed: Expected '>' at position 4, but found 'Traversable'.
Loading history...
38
                         */
39 4
                        static fn (array $iterables): Closure =>
40
                        /**
41
                         * @param iterable<TKey, T> $iterable
42
                         */
43 4
                        static fn (iterable $iterable): Generator => (
44
                            /**
45
                             * @param Closure(iterable<TKey, T>): (Closure(iterable<UKey, U>): Generator<array<array-key, T|U>>) $f
46
                             */
47 4
                            static fn (Closure $f): Closure => (new FoldLeft())()(
48
                                /**
49
                                 * @param iterable<UKey, U> $a
50
                                 * @param iterable<TKey, T> $x
51
                                 */
52 4
                                static fn (iterable $a, iterable $x): Generator => $f($x)($a)
53 4
                            )
54 4
                        )(
55
                            /**
56
                             * @param (iterable<TKey, T>|iterable<UKey, U>) $xs
0 ignored issues
show
Documentation Bug introduced by
The doc comment (iterable<TKey, T>|iterable<UKey, U>) at position 1 could not be parsed: Expected ')' at position 1, but found 'iterable'.
Loading history...
57
                             */
58 4
                            static fn (iterable $xs): Closure =>
59
                                /**
60
                                 * @param iterable<int, array<array-key, T>> $as
61
                                 */
62 4
                                static fn (iterable $as): Generator => (new FlatMap())()(
63
                                    /**
64
                                     * @param array<int, T> $a
65
                                     */
66 4
                                    static fn (array $a): Generator => (new FlatMap())()(
67
                                        /**
68
                                         * @param T|U $x
69
                                         *
70
                                         * @return Generator<int, array<array-key, T|U>>
71
                                         */
72 4
                                        static fn (mixed $x): Generator => yield [...$a, $x]
73 4
                                    )($xs)
74 4
                                )($as)
75 4
                        )([[]])([$iterable, ...$iterables])
76 4
                    )(
77 4
                        array_map(
78
                            /**
79
                             * @param iterable<UKey, U> $iterable
80
                             *
81
                             * @return IterableIteratorAggregate<UKey, U>
82
                             */
83 4
                            static fn (iterable $iterable): IterableIteratorAggregate => new IterableIteratorAggregate($iterable),
84 4
                            $iterables
85 4
                        )
86 4
                    ),
87 4
                    ((new Unwrap())()),
88 4
                    ((new Normalize())())
89 4
                );
90
91
                // Point free style.
92 4
                return $pipe;
93 4
            };
94
    }
95
}
96