Passed
Push — master ( 9d4bf9...615338 )
by Radosław
02:10
created

CurriedFn::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Baethon\Phln;
6
7
final class CurriedFn implements FixedArityInterface
8
{
9
    /**
10
     * @var callable
11
     */
12
    private $fn;
13
14
    private int $arity;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
15
16
    /**
17
     * @var array<int, mixed> $args
18
     */
19
    private array $args = [];
20
21
    /**
22
     * @param int $arity
23
     * @param callable $fn,
24
     * @param mixed[] $args
25
     */
26
    private function __construct(int $arity, callable $fn, array $args = [])
27
    {
28
        $this->arity = $arity;
29
        $this->fn = $fn;
30
        $this->args = $args;
31
    }
32
33
    public static function of(callable $fn): self
34
    {
35
        return ($fn instanceof static)
36
            ? new static($fn->arity, $fn->fn, $fn->args)
37
            : static::ofN(Phln::arity($fn), $fn);
38
    }
39
40
    public static function ofN(int $arity, callable $fn): self
41
    {
42
        return new static($arity, $fn);
43
    }
44
45
    /**
46
     * @param array<int, mixed> ...$args
47
     * @return CurriedFn|mixed
48
     */
49
    public function __invoke(...$args)
50
    {
51
        $allArgs = array_merge($this->args, $args);
52
53
        return ($this->arity > count($allArgs))
54
            ? new static($this->arity, $this->fn, $allArgs)
55
            : call_user_func_array($this->fn, $allArgs);
56
    }
57
58
    public function getArity(): int
59
    {
60
        return $this->arity - count($this->args);
61
    }
62
}
63