Passed
Pull Request — master (#12)
by Pol
12:26
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
     * @template T
29 4
     * @psalm-pure
30
     */
31
    public static function of(): Closure
32
    {
33
        return
34
            /**
35 4
             * @psalm-param positive-int|null $arity
36
             */
37
            static function (callable $callable, ?int $arity = null, ?int $parameters = null, ?int $requiredParameters = null, ...$arguments): mixed {
38
                if (null === $arity) {
39
                    $reflection = (new ReflectionFunction($callable));
40
                    $parameters ??= $reflection->getNumberOfParameters();
41 4
                    $requiredParameters ??= $reflection->getNumberOfRequiredParameters();
42 4
                    $arity ??= $requiredParameters;
43
                } else {
44 4
                    $parameters = $requiredParameters = $arity;
45 4
                }
46
47
                return self::curryN(
48
                    $callable,
49
                    $arity,
50
                    $parameters,
51
                    $requiredParameters,
52
                    ...$arguments
53
                );
54
            };
55
    }
56
57
    private static function curryN(callable $callable, ?int $arity = null, ?int $parameters = null, ?int $requiredParameters = null, ...$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) ?
0 ignored issues
show
Bug introduced by
It seems like $parameters can also be of type null; however, parameter $parameters of loophp\fpt\Curry::isComplete() 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

60
        return self::isComplete(count($arguments), /** @scrutinizer ignore-type */ $parameters, $requiredParameters) ?
Loading history...
Bug introduced by
It seems like $requiredParameters can also be of type null; however, parameter $requiredParameters of loophp\fpt\Curry::isComplete() 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

60
        return self::isComplete(count($arguments), $parameters, /** @scrutinizer ignore-type */ $requiredParameters) ?
Loading history...
61 4
            ($callable)(...$arguments) :
62
            static fn (...$args): mixed => self::of()($callable, $arity, $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