Memoizer::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace ganglio;
4
5
class Memoizer
6
{
7
8
    const NOT_CALLABLE = 1;
9
10
    private $f = null;
11
    private $results = null;
12
13
    public function __construct($f)
14
    {
15
        if (!is_callable($f)) {
16
            throw new \InvalidArgumentException("Argument needs to be callable", self::NOT_CALLABLE);
17
        }
18
19
        $this->f = new \ReflectionFunction($f);
20
    }
21
22
    public function __invoke()
23
    {
24
        $args = func_get_args();
25
        $args_hash = spl_object_hash((object)$args);
26
27
        if (!isset($this->results[$args_hash])) {
28
            $this->results[$args_hash] = $this->f->invokeArgs($args);
29
        }
30
31
        return $this->results[$args_hash];
32
    }
33
}
34