ProxyTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 1 Features 0
Metric Value
eloc 7
c 7
b 1
f 0
dl 0
loc 47
ccs 9
cts 9
cp 1
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A repeatError() 0 4 1
A getCurrentError() 0 3 1
A hasCurrentError() 0 3 1
A resetCurrentError() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Proxy;
6
7
use Throwable;
8
9
/**
10
 * @internal
11
 *
12
 * This trait allows to handle errors during proxy method calls. Handling can be added in
13
 * {@see ObjectProxy::afterCall()} event.
14
 */
15
trait ProxyTrait
16
{
17
    /**
18
     * @var Throwable|null A throwable object extracted from exception thrown during the last proxy method call. It's
19
     * `null` when no exception was thrown. Automatically reset during the new call.
20
     */
21
    private ?Throwable $currentError = null;
22
23
    /**
24
     * Gets current error.
25
     *
26
     * @return Throwable|null {@see $currentError}.
27
     */
28 2
    public function getCurrentError(): ?Throwable
29
    {
30 2
        return $this->currentError;
31
    }
32
33
    /**
34
     * Whether a proxy has current error.
35
     *
36
     * @return bool `true` if it has current error and `false` otherwise.
37
     */
38 2
    public function hasCurrentError(): bool
39
    {
40 2
        return $this->currentError !== null;
41
    }
42
43
    /**
44
     * Throws current error again.
45
     *
46
     * @param Throwable $error A throwable object.
47
     *
48
     * @throws Throwable An exact error previously stored in {@see $currentError}.
49
     */
50 3
    protected function repeatError(Throwable $error): void
51
    {
52 3
        $this->currentError = $error;
53 3
        throw $error;
54
    }
55
56
    /**
57
     * Resets current error.
58
     */
59 9
    protected function resetCurrentError(): void
60
    {
61 9
        $this->currentError = null;
62
    }
63
}
64