Passed
Push — master ( 54e8a7...8a4054 )
by Sérgio
03:36
created

memoize.php ➔ memoize()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 1
nop 1
dl 0
loc 14
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Prelude;
6
7
const memoize = __NAMESPACE__.'\memoize';
8
9
use Closure;
10
11
function memoize(callable $callback): Closure
12
{
13
    return function (...$args) use ($callback) {
14 1
        static $cache = [];
15
16 1
        $key = md5(serialize($args));
17
        
18 1
        if (!isset($cache[$key])) {
19 1
            $cache[$key] = $callback(...$args);
20
        }
21
22 1
        return $cache[$key];
23 1
    };
24
}
25