Passed
Pull Request — master (#21)
by Alexander
31:07 queued 28:59
created

ObjectProxy::getNewStaticInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Proxy;
6
7
use Exception;
8
9
abstract class ObjectProxy
10
{
11
    use ProxyTrait;
12
13
    private object $instance;
14
15 4
    public function __construct(object $instance)
16
    {
17 4
        $this->instance = $instance;
18
    }
19
20 4
    protected function call(string $methodName, array $arguments)
21
    {
22 4
        $this->resetCurrentError();
23 4
        $result = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
24 4
        $timeStart = microtime(true);
25
        try {
26 4
            $result = $this->callInternal($methodName, $arguments);
27 1
        } catch (Exception $e) {
28 1
            $this->repeatError($e);
29 3
        } finally {
30 4
            $result = $this->executeMethodProxy($methodName, $arguments, $result, $timeStart);
0 ignored issues
show
Bug introduced by
It seems like $timeStart can also be of type string; however, parameter $timeStart of Yiisoft\Proxy\ObjectProxy::executeMethodProxy() 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

30
            $result = $this->executeMethodProxy($methodName, $arguments, $result, /** @scrutinizer ignore-type */ $timeStart);
Loading history...
31
        }
32
33 3
        return $this->processResult($result);
34
    }
35
36
    abstract protected function executeMethodProxy(string $methodName, array $arguments, $result, float $timeStart);
37
38 1
    protected function getNewStaticInstance(object $instance): self
39
    {
40 1
        return new static($instance);
41
    }
42
43
    protected function getInstance(): object
44
    {
45
        return $this->instance;
46
    }
47
48 4
    private function callInternal(string $methodName, array $arguments)
49
    {
50 4
        return $this->instance->$methodName(...$arguments);
51
    }
52
53 3
    private function processResult($result)
54
    {
55 3
        if (is_object($result) && get_class($result) === get_class($this->instance)) {
56 1
            $result = $this->getNewStaticInstance($result);
57
        }
58
59 3
        return $result;
60
    }
61
}
62