Passed
Push — master ( c81bf2...c3afe7 )
by Aleksei
09:08 queued 01:17
created

TracedContainerException::extendTracedException()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 10
c 1
b 0
f 0
cc 3
nc 2
nop 3
crap 3
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 816
    public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null)
18
    {
19 816
        $this->originalMessage = $message;
20 816
        parent::__construct($message, $code, $previous);
21
    }
22
23
    /**
24
     * @internal
25
     */
26 795
    public static function createWithTrace(
27
        string $message,
28
        array $trace = [],
29
        ?\Throwable $previous = null,
30
    ): static {
31 795
        $class = static::class;
32
        // Merge traces
33 795
        if ($previous instanceof self) {
34 27
            $merge = $previous->containerTrace;
35 27
            if ($trace !== [] && $merge !== []) {
36
                // merge lat element of $traces with first element of $merge
37 15
                \array_push($trace[\count($trace) - 1], ...$merge[0]);
38 15
                unset($merge[0]);
39
            }
40
41 27
            $trace = \array_merge($trace, $merge);
42 27
            $message = "$message\n{$previous->originalMessage}";
43 27
            $class = $previous::class;
44
        }
45
46 795
        $result = $class::createStatic(
47 795
            $message . ($trace === [] ? '' : "\nResolving trace:\n" . Tracer::renderTraceList($trace)),
48 795
            $previous,
49 795
        );
50 795
        $result->originalMessage = $message;
51 795
        $result->containerTrace = $trace;
52 795
        return $result;
53
    }
54
55 784
    protected static function createStatic(string $message, ?\Throwable $previous): static
56
    {
57 784
        return new static($message, previous: $previous);
58
    }
59
}
60