Issues (4)

src/Logger.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\TestDouble;
6
7
use Ray\Aop\MethodInvocation;
8
use ReflectionClass;
9
10
use function assert;
11
use function microtime;
12
13
final class Logger implements LoggerInterface
14
{
15
    /** @var array<string, array<string, array<Log>>> */
16
    private array $logs = [];
17
18
    public function log(MethodInvocation $invocation): mixed
19
    {
20
        $t = microtime(true);
21
        /** @psalm-suppress MixedAssignment */
22
        $result = $invocation->proceed();
23
        $time = microtime(true) - $t;
24
        $parent = (new ReflectionClass($invocation->getThis()))->getParentClass();
25
        assert($parent instanceof ReflectionClass);
26
        $class =  $parent->getName();
27
        $method =  $invocation->getMethod()->getName();
28
        $this->logs[$class][$method][] = new Log(
29
            (array) $invocation->getArguments(),
30
            (array) $invocation->getNamedArguments(),
31
            $result,
32
            $time,
33
        );
34
35
        return $result;
36
    }
37
38
    /**
39
     * @param class-string $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
40
     *
41
     * @return array<Log>
42
     */
43
    public function getLogs(string $class, string $method): array
44
    {
45
        if (! isset($this->logs[$class][$method])) {
46
            return [];
47
        }
48
49
        return $this->logs[$class][$method];
50
    }
51
}
52