Passed
Push — 1.x ( 9e309f...86c739 )
by Akihito
01:33
created

Spy::proceed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 17
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 22
rs 9.2
1
<?php
2
/**
3
 * This file is part of the Ray.TestDouble package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\TestDouble;
8
9
use Ray\Aop\MethodInvocation;
10
11
final class Spy
12
{
13
    /**
14
     * @var array
15
     */
16
    private $logs = [];
17
18
    public function proceed(MethodInvocation $invocation)
19
    {
20
        $t = microtime(true);
21
        $result = $invocation->proceed();
22
        $time = microtime(true) - $t;
23
        $spyLog = new SpyLog;
24
        list(
25
            $spyLog->class,
26
            $spyLog->method,
27
            $spyLog->arguments,
28
            $spyLog->result,
29
            $spyLog->time
30
        ) = [
31
            (new \ReflectionClass($invocation->getThis()))->getParentClass()->getName(),
32
            $invocation->getMethod()->getName(),
33
            $invocation->getArguments()->getArrayCopy(),
34
            $result,
35
            $time
36
        ];
37
        $this->logs[$spyLog->class][$spyLog->method][] = $spyLog;
38
39
        return $result;
40
    }
41
42
    /**
43
     * @param string $class
44
     * @param string $name
45
     *
46
     * @return SpyLog[]
47
     */
48
    public function getLogs($class, $name)
49
    {
50
        if (! isset($this->logs[$class][$name])) {
51
            return [];
52
        }
53
54
        return $this->logs[$class][$name];
55
    }
56
}
57