Passed
Push — master ( 65be94...111ef5 )
by Alexander
02:18
created

ObjectProxy   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 51
ccs 0
cts 36
cp 0
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A call() 0 14 2
A processResult() 0 7 3
A callInternal() 0 3 1
A getInstance() 0 3 1
A getNewStaticInstance() 0 3 1
1
<?php
2
3
namespace Yiisoft\Proxy;
4
5
abstract class ObjectProxy
6
{
7
    use ProxyTrait;
8
9
    private object $instance;
10
11
    public function __construct(object $instance)
12
    {
13
        $this->instance = $instance;
14
    }
15
16
    protected function call(string $methodName, array $arguments)
17
    {
18
        $this->resetCurrentError();
19
        $result = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
20
        $timeStart = microtime(true);
21
        try {
22
            $result = $this->callInternal($methodName, $arguments);
23
        } catch (\Exception $e) {
24
            $this->repeatError($e);
25
        } finally {
26
            $result = $this->executeMethodProxy($methodName, $arguments, $result, $timeStart);
27
        }
28
29
        return $this->processResult($result);
30
    }
31
32
    abstract protected function executeMethodProxy(string $methodName, array $arguments, $result, float $timeStart);
33
34
    protected function getNewStaticInstance(object $instance): self
35
    {
36
        return new static($instance);
37
    }
38
39
    protected function getInstance(): object
40
    {
41
        return $this->instance;
42
    }
43
44
    private function callInternal(string $methodName, array $arguments)
45
    {
46
        return $this->instance->$methodName(...$arguments);
47
    }
48
49
    private function processResult($result)
50
    {
51
        if (is_object($result) && get_class($result) === get_class($this->instance)) {
52
            $result = $this->getNewStaticInstance($result);
53
        }
54
55
        return $result;
56
    }
57
}
58