LazyParam::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Marcosh\Effector;
6
7
final class LazyParam
8
{
9
    /**
10
     * @var callable
11
     */
12
    private $function;
13
14
    /**
15
     * @var callable[]
16
     */
17
    private $parameters;
18
19
    private function __construct(
20
        callable $function,
21
        callable ... $parameters
22
    )
23
    {
24
        $this->function = $function;
25
        $this->parameters = $parameters;
26
    }
27
28
    public static function lazyParameters(
29
        callable $function,
30
        callable ... $parameters
31
    ): self
32
    {
33
        return new self($function, ... $parameters);
34
    }
35
36
    public function __invoke(... $parameterInput)
37
    {
38
        if ([] === $this->parameters) {
39
            return ($this->function)(... $parameterInput);
40
        }
41
42
        $nextParameter = $this->parameters[0];
43
44
        $otherParameters = array_slice($this->parameters, 1);
45
46
        return self::lazyParameters(
47
            function (... $parameters) use ($nextParameter, $parameterInput) {
48
                return ($this->function)(
49
                    $nextParameter(... $parameterInput),
50
                    ... $parameters
51
                );
52
            },
53
            ... $otherParameters
54
        );
55
    }
56
}
57