Passed
Push — master ( 90528a...a31674 )
by Radosław
02:00
created

CurriedFn::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Baethon\Phln;
5
6
final class CurriedFn implements FixedArityInterface
7
{
8
    /**
9
     * @type callable
10
     */
11
    private $fn;
12
13
    /**
14
     * @type integer
15
     */
16
    private $arity;
17
18
    /**
19
     * @type array
20
     */
21
    private $args = [];
22
23
    private function __construct(int $arity, callable $fn, array $args = [])
24
    {
25
        $this->arity = $arity;
26
        $this->fn = $fn;
27
        $this->args = $args;
28
    }
29
30
    public static function of(callable $fn): self
31
    {
32
        return ($fn instanceof static)
33
            ? new static($fn->arity, $fn->fn, $fn->args)
34
            : static::ofN(Phln::arity($fn), $fn);
35
    }
36
37
    public static function ofN(int $arity, callable $fn): self
38
    {
39
        return new static($arity, $fn);
40
    }
41
42
    public function __invoke(...$args)
43
    {
44
        $allArgs = array_merge($this->args, $args);
45
46
        return ($this->arity > count($allArgs))
47
            ? new static($this->arity, $this->fn, $allArgs)
48
            : call_user_func_array($this->fn, $allArgs);
49
    }
50
51
    public function getArity(): int
52
    {
53
        return $this->arity - count($this->args);
54
    }
55
}
56