GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

curry()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 1
nop 1
dl 0
loc 34
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Marcosh\PhpValidationDSL;
6
7
use Closure;
8
use ReflectionFunction;
9
10
/**
11
 * implementation heavily inspired by https://github.com/m4rw3r/autoCurry
12
 *
13
 * @param callable $f with signature ($a1, $a2, ...) -> $something
14
 * @return Closure with signature $a1 -> ($a2 -> (... -> $something))
15
 *
16
 * @psalm-return Closure(): callable
17
 */
18
function curry(callable $f): Closure
19
{
20
    $innerCurry =
21
        /**
22
         * @return Closure
23
         *
24
         * @psalm-return Closure(): callable
25
         */
26
        static function (
27
            callable $f,
28
            ?int $numberOfParameters = null,
29
            array $parameters = []
30
        ) use (&$innerCurry) : Closure {
31
            if (null === $numberOfParameters) {
32
                // retrieve number of parameters from reflection
33
                $fClosure = Closure::fromCallable($f);
34
                $fRef = new ReflectionFunction($fClosure);
35
                $numberOfParameters = $fRef->getNumberOfParameters();
36
            }
37
38
            /** @psalm-suppress MissingClosureReturnType */
39
            return static function () use ($f, $numberOfParameters, $parameters, $innerCurry) {
40
                /** @var array<int, mixed> $newParameters */
41
                $newParameters = array_merge($parameters, func_get_args());
42
43
                if (count($newParameters) >= $numberOfParameters) {
44
                    return call_user_func_array($f, $newParameters);
45
                }
46
47
                return $innerCurry($f, $numberOfParameters, $newParameters);
48
            };
49
        };
50
51
    return $innerCurry($f);
52
}
53
54
/**
55
 * @param callable $f with signature $a1 -> ($a2 -> (... -> $something))
56
 * @return Closure with signature ($a1, $a2, ...) -> $something
57
 *
58
 * @psalm-return Closure(... array<int, mixed>): mixed
59
 */
60
function uncurry(callable $f): Closure
61
{
62
    /**
63
     * @psalm-suppress MissingClosureParamType
64
     * @psalm-suppress MissingClosureReturnType
65
     */
66
    return static function (...$params) use ($f) {
67
        if ([] === $params) {
68
            return $f();
69
        }
70
71
        $firstParam = $params[0];
72
73
        $firstApplication = $f($firstParam);
74
75
        if (! is_callable($firstApplication)) {
76
            return $firstApplication;
77
        }
78
79
        return uncurry($firstApplication)(...array_slice($params, 1));
80
    };
81
}
82