Passed
Pull Request — master (#16)
by Pol
13:11
created

ComposeSimple::of()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\fpt;
11
12
use Closure;
13
14
// phpcs:disable Generic.Files.LineLength.TooLong
15
16
/**
17
 * @immutable
18
 */
19
final class ComposeSimple
20
{
21
    /**
22
     * @pure
23
     */
24
    public static function of(): Closure
25
    {
26
        return
27
            static function (callable ...$fs): Closure {
28
                // Identity
29
                $initial = static fn ($v) => $v;
30
31
                foreach ($fs as $f) {
32
                    $initial = ComposeSimple::of1()($initial)($f);
33
                }
34
35
                return $initial;
36
            };
37
    }
38
39
    private static function of1(): Closure
40
    {
41
        return
42
            static fn (callable $f): Closure => static fn (callable $g): Closure => static fn (...$x): mixed => $f($g(...$x));
43
    }
44
}
45