Passed
Push — master ( cf37f2...bd53e8 )
by Alexander
02:23
created

ProxyLogTrait::processLogData()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
nc 8
nop 3
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
crap 4.0466
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Proxy;
6
7
use Yiisoft\Yii\Debug\Event\ProxyMethodCallEvent;
8
9
use function get_class;
10
11
trait ProxyLogTrait
12
{
13
    private ContainerProxyConfig $config;
14
15 6
    protected function logProxy(
16
        string $service,
17
        object $instance,
18
        string $method,
19
        array $arguments,
20
        $result,
21
        float $timeStart
22
    ): void {
23 6
        $error = $this->getCurrentError();
0 ignored issues
show
Bug introduced by
It seems like getCurrentError() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        /** @scrutinizer ignore-call */ 
24
        $error = $this->getCurrentError();
Loading history...
24 6
        $this->processLogData($arguments, $result, $error);
25
26 6
        if ($this->config->getCollector() !== null) {
27 6
            $this->logToCollector($service, $instance, $method, $arguments, $result, $error, $timeStart);
28
        }
29
30 6
        if ($this->config->getDispatcher() !== null) {
31 6
            $this->logToEvent($service, $instance, $method, $arguments, $result, $error, $timeStart);
32
        }
33 6
    }
34
35 6
    private function processLogData(array &$arguments, &$result, ?object &$error): void
36
    {
37 6
        if (!($this->config->getLogLevel() & ContainerInterfaceProxy::LOG_ARGUMENTS)) {
38
            $arguments = null;
39
        }
40
41 6
        if (!($this->config->getLogLevel() & ContainerInterfaceProxy::LOG_RESULT)) {
42 6
            $result = null;
43
        }
44
45 6
        if (!($this->config->getLogLevel() & ContainerInterfaceProxy::LOG_ERROR)) {
46 6
            $error = null;
47
        }
48 6
    }
49
50 6
    private function logToCollector(
51
        string $service,
52
        object $instance,
53
        string $method,
54
        ?array $arguments,
55
        $result,
56
        ?object $error,
57
        float $timeStart
58
    ): void {
59 6
        $this->config->getCollector()->collect(
60 6
            $service,
61 6
            get_class($instance),
62
            $method,
63
            $arguments,
64
            $result,
65 6
            $this->getCurrentResultStatus(),
0 ignored issues
show
Bug introduced by
It seems like getCurrentResultStatus() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            $this->/** @scrutinizer ignore-call */ 
66
                   getCurrentResultStatus(),
Loading history...
66
            $error,
67
            $timeStart,
68 6
            microtime(true),
0 ignored issues
show
Bug introduced by
It seems like microtime(true) can also be of type string; however, parameter $timeEnd of Yiisoft\Yii\Debug\Collec...torInterface::collect() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            /** @scrutinizer ignore-type */ microtime(true),
Loading history...
69
        );
70 6
    }
71
72 6
    private function logToEvent(
73
        string $service,
74
        object $instance,
75
        string $method,
76
        ?array $arguments,
77
        $result,
78
        ?object $error,
79
        float $timeStart
80
    ): void {
81 6
        $this->config->getDispatcher()->dispatch(
82 6
            new ProxyMethodCallEvent(
83 6
                $service,
84 6
                get_class($instance),
85
                $method,
86
                $arguments,
87
                $result,
88 6
                $this->getCurrentResultStatus(),
89
                $error,
90
                $timeStart,
91 6
                microtime(true),
0 ignored issues
show
Bug introduced by
It seems like microtime(true) can also be of type string; however, parameter $timeEnd of Yiisoft\Yii\Debug\Event\...allEvent::__construct() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
                /** @scrutinizer ignore-type */ microtime(true),
Loading history...
92
            )
93
        );
94 6
    }
95
}
96