Memoizer   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 17
ccs 5
cts 5
cp 1
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromClosure() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\memoize;
6
7
use ArrayObject;
8
use Closure;
9
use loophp\memoize\Contract\Memoizer as MemoizerInterface;
10
11
final class Memoizer implements MemoizerInterface
12
{
13 2
    public static function fromClosure(Closure $closure, ?ArrayObject $cache = null): Closure
14
    {
15 2
        $cache ??= new ArrayObject();
16
17
        return
18
            /**
19
             * @psalm-suppress MixedAssignment
20
             *
21
             * @param mixed ...$arguments
22
             *
23
             * @return mixed
24
             */
25 2
            static fn (...$arguments) => $cache[
26 2
                    sha1(serialize($arguments))
27 2
                    ] ??= ($closure)(...$arguments);
28
    }
29
}
30