Memory::memoizeCallable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
namespace DominionEnterprises\Memoize;
3
4
/**
5
 * An in-memory memoizer that keeps the cached values around for the lifetime of this instance.
6
 */
7
class Memory implements Memoize
8
{
9
    /**
10
     * The saved results
11
     *
12
     * @var array
13
     */
14
    private $_cache = [];
15
16
    /**
17
     * $cacheTime is ignored - this will keep the results around for the lifetime of this instance.
18
     *
19
     * @see Memoize::memoizeCallable
20
     */
21
    public function memoizeCallable($key, $compute, $cacheTime = null)
22
    {
23
        if (array_key_exists($key, $this->_cache)) {
24
            return $this->_cache[$key];
25
        }
26
27
        $result = call_user_func($compute);
28
        $this->_cache[$key] = $result;
29
30
        return $result;
31
    }
32
}
33