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

AggregatedExceptionTrait::getIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 0
crap 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): 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