AggregatedExceptionTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 81.48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 24
c 1
b 0
f 0
dl 0
loc 49
ccs 22
cts 27
cp 0.8148
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 4 2
A count() 0 3 1
A formatMessage() 0 29 6
1
<?php
2
3
/*
4
 * This file is part of ocubom/twig-svg-extension
5
 *
6
 * © Oscar Cubo Medina <https://ocubom.github.io>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ocubom\Twig\Extension\Svg\Util;
13
14
use function BenTools\IterableFunctions\iterable_to_array;
15
16
trait AggregatedExceptionTrait
17
{
18
    /** @var \Throwable[] */
19
    private array $previous = [];
20
21
    /**
22
     * @param \Throwable|iterable<\Throwable> $previous
23
     */
24 7
    private function formatMessage(string $message = null, $previous = null, bool $previousMessages = false): string
25
    {
26 7
        $this->previous = $previous instanceof \Throwable ? [$previous] : iterable_to_array(
27 7
            $previous ?? /* @scrutinizer ignore-type */ [],
28 7
            false
29 7
        );
30 7
        $count = count($this->previous);
31
32 7
        $message = sprintf(
33 7
            rtrim($message ?? 'Generated %d %s', " \n\r\t\v\x00.:"),
34 7
            $count,
35 7
            1 == $count ? 'exception' : 'exceptions'
36 7
        );
37
38 7
        if ($previousMessages && $count > 0) {
39 1
            $message .= ":\n\n";
40 1
            foreach ($this->previous as $idx => $err) {
41 1
                $message .= sprintf(
42 1
                    "% 3d. [%s] %s.\n",
43 1
                    $idx + 1,
44 1
                    get_class($err),
45 1
                    trim($err->getMessage(), " \n\r\t\v\x00.:")
46 1
                );
47
            }
48
        } else {
49 6
            $message .= '.';
50
        }
51
52 7
        return $message;
53
    }
54
55
    public function getIterator(): \Traversable
56
    {
57
        foreach ($this->previous as $exc) {
58
            yield $exc;
59
        }
60
    }
61
62
    public function count(): int
63
    {
64
        return count($this->previous);
65
    }
66
}
67