Passed
Pull Request — master (#12)
by Pol
15:07
created

Curry::isComplete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\fpt;
11
12
use Closure;
13
use Generator;
14
use ReflectionFunction;
15
16
use function count;
17
18
/**
19
 * Stateless "Curry" application.
20
 *
21
 * @psalm-immutable
22
 *
23
 * phpcs:disable Generic.Files.LineLength.TooLong
24
 */
25
final class Curry
26
{
27
    /**
28
     * @psalm-pure
29 4
     */
30
    public static function of(): Closure
31
    {
32
        return
33
            /**
34
             * @param Closure|callable-string $callable
0 ignored issues
show
Documentation Bug introduced by
The doc comment Closure|callable-string at position 2 could not be parsed: Unknown type name 'callable-string' at position 2 in Closure|callable-string.
Loading history...
35 4
             */
36
            static function (callable $callable, ?int $arity = null, int $parameters = 0, int $requiredParameters = 0, mixed ...$arguments): mixed {
37
                $parameters = $requiredParameters = $arity;
38
39
                if (null === $arity) {
40
                    $reflection = (new ReflectionFunction($callable));
41 4
                    $parameters ??= $reflection->getNumberOfParameters();
42 4
                    $requiredParameters ??= $reflection->getNumberOfRequiredParameters();
43
                }
44 4
45 4
                return self::curryN(
46
                    $callable,
47
                    $parameters,
0 ignored issues
show
Bug introduced by
It seems like $parameters can also be of type null; however, parameter $parameters of loophp\fpt\Curry::curryN() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
                    /** @scrutinizer ignore-type */ $parameters,
Loading history...
48
                    $requiredParameters,
0 ignored issues
show
Bug introduced by
It seems like $requiredParameters can also be of type null; however, parameter $requiredParameters of loophp\fpt\Curry::curryN() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
                    /** @scrutinizer ignore-type */ $requiredParameters,
Loading history...
49
                    ...$arguments
50
                );
51
            };
52
    }
53
54
    /**
55
     * @param Closure|callable-string $callable
0 ignored issues
show
Documentation Bug introduced by
The doc comment Closure|callable-string at position 2 could not be parsed: Unknown type name 'callable-string' at position 2 in Closure|callable-string.
Loading history...
56
     */
57
    private static function curryN(callable $callable, int $parameters, int $requiredParameters, mixed ...$arguments): mixed
58
    {
59
        // Normally the curry function takes one parameter at a time (returns a unary function).
60
        return self::isComplete(count($arguments), $parameters, $requiredParameters) ?
61 4
            ($callable)(...$arguments) :
62
            static fn (mixed ...$args): mixed => self::curryN($callable, $parameters, $requiredParameters, ...self::getArguments($arguments, $args));
63 4
    }
64 4
65
    /**
66
     * @psalm-pure
67
     *
68
     * @psalm-param list<mixed> $args
69
     * @psalm-param list<mixed> $argsNext
70
     *
71 4
     * @psalm-return Generator<int, mixed>
72
     */
73
    private static function getArguments(array $args, array $argsNext): Generator
74
    {
75
        return yield from array_merge($args, $argsNext);
76
    }
77
78
    private static function isComplete(int $arguments, int $parameters, int $requiredParameters): bool
79
    {
80
        return $arguments === $parameters || $arguments === $requiredParameters;
81
    }
82
}
83