Passed
Push — master ( 41e710...91ffae )
by Aleksei
06:19 queued 21s
created

TracedContainerException   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 17
dl 0
loc 36
ccs 18
cts 18
cp 1
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createWithTrace() 0 12 2
A extendTracedException() 0 11 3
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Exception\Container;
6
7
use Spiral\Core\Internal\Tracer;
8
9
/**
10
 * Exception that contains trace of resolving.
11
 */
12
class TracedContainerException extends ContainerException
13
{
14
    protected array $containerTrace = [];
15
    protected string $originalMessage = '';
16
17 791
    public function __construct(string $message = "", int $code = 0, ?\Throwable $previous = null)
18
    {
19 791
        $this->originalMessage = $message;
20 791
        parent::__construct($message, $code, $previous);
21
    }
22
23 791
    public static function createWithTrace(
24
        string $message,
25
        array $traces,
26
        ?\Throwable $previous = null,
27
    ): static {
28 791
        $result = new static(
29 791
            $message . ($traces === [] ? '' : "\nResolving trace:\n" . Tracer::renderTraceList($traces)),
30 791
            previous: $previous,
31 791
        );
32 791
        $result->originalMessage = $message;
33 791
        $result->containerTrace = $traces;
34 791
        return $result;
35
    }
36
37 15
    public static function extendTracedException(string $message, array $traces, self $exception): static
38
    {
39 15
        $merge = $exception->containerTrace;
40 15
        if ($traces !== [] && $merge !== []) {
41
            // merge lat element of $traces with first element of $merge
42 15
            \array_push($traces[\count($traces) - 1], ...$merge[0]);
43 15
            unset($merge[0]);
44
        }
45
46 15
        $traces = \array_merge($traces, $merge);
47 15
        return static::createWithTrace("$message\n{$exception->originalMessage}", $traces, previous: $exception);
48
    }
49
}
50