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

ObjectProxy::callInternal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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