Completed
Push — master ( b78223...473a6d )
by Paweł
10s
created

CurriedFunction::lift()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Scalp;
6
7
use function Scalp\Reflection\reflectionFunction;
0 ignored issues
show
Bug introduced by
The type Scalp\Reflection\reflectionFunction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
final class CurriedFunction
10
{
11
    private $f;
12
    private $arity;
13
    private $args;
14
15
    public static function lift(callable $f): CurriedFunction
16
    {
17
        return new self($f, reflectionFunction($f)->getNumberOfRequiredParameters(), []);
18
    }
19
20
    public static function liftN(callable $f, int $arity): CurriedFunction
21
    {
22
        $requiredArity = reflectionFunction($f)->getNumberOfRequiredParameters();
23
24
        if ($arity < $requiredArity) {
25
            throw new \ArgumentCountError(sprintf('Declared arity of function is %d, but required number of arguments is %d.', $arity, $requiredArity));
0 ignored issues
show
Bug introduced by
The type ArgumentCountError was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
        }
27
28
        return new self($f, $arity, []);
29
    }
30
31
    public function __invoke(...$args)
32
    {
33
        $callArgs = array_merge($this->args, $args);
34
35
        if (\count($callArgs) >= $this->arity) {
36
            return ($this->f)(...$callArgs);
37
        }
38
39
        return new self($this->f, $this->arity, $callArgs);
40
    }
41
42
    private function __construct(callable $f, int $arity, array $args)
43
    {
44
        $this->f = $f;
45
        $this->arity = $arity;
46
        $this->args = $args;
47
    }
48
}
49