Memoizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 4
c 4
b 0
f 1
lcom 0
cbo 0
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A __invoke() 0 11 2
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