Passed
Push — main ( d61a4b...a3ea10 )
by Oscar
12:17
created

AggregatedExceptionTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 24
dl 0
loc 48
ccs 20
cts 26
cp 0.7692
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 4 2
A count() 0 3 1
A formatMessage() 0 28 5
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): string
25
    {
26 7
        $this->previous = $previous instanceof \Throwable
27
            ? [$previous]
28 7
            : iterable_to_array($previous ?? /* @scrutinizer ignore-type */ [], false);
29 7
        $count = count($this->previous);
30
31 7
        $message = sprintf(
32 7
            rtrim($message ?? 'Generated %d %s', " \n\r\t\v\x00.:"),
33 7
            $count,
34 7
            1 == $count ? 'exception' : 'exceptions'
35 7
        );
36
37 7
        if ($count > 0) {
38 2
            $message .= ":\n\n";
39 2
            foreach ($this->previous as $idx => $err) {
40 2
                $message .= sprintf(
41 2
                    "% 3d. [%s] %s.\n",
42 2
                    $idx + 1,
43 2
                    get_class($err),
44 2
                    trim($err->getMessage(), " \n\r\t\v\x00.:")
45 2
                );
46
            }
47
        } else {
48 6
            $message .= '.';
49
        }
50
51 7
        return $message;
52
    }
53
54
    public function getIterator(): \Traversable
55
    {
56
        foreach ($this->previous as $exc) {
57
            yield $exc;
58
        }
59
    }
60
61
    public function count(): int
62
    {
63
        return count($this->previous);
64
    }
65
}
66