LazyParam   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 50
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A lazyParameters() 0 7 1
A __invoke() 0 20 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