Passed
Pull Request — master (#71)
by Aleksei
06:25
created

CumulativeException::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Exception;
6
7
use RuntimeException;
8
use Throwable;
9
10
class CumulativeException extends RuntimeException
11
{
12
    /** @var Throwable[] */
13
    private array $exceptions;
14
15 5
    public function __construct(Throwable ...$exceptions)
16
    {
17 5
        $this->exceptions = $exceptions;
18 5
        $count = count($exceptions);
19 5
        $message = $count === 1 ? 'One exception was thrown.' : $count . ' exceptions were thrown.';
20 5
        parent::__construct($message . $this->getMessageDetails());
21 5
    }
22
23
    /**
24
     * @return Throwable[]
25
     */
26 1
    public function getExceptions(): array
27
    {
28 1
        return $this->exceptions;
29
    }
30
31 5
    private function getMessageDetails(): string
32
    {
33 5
        $result = '';
34 5
        $num = 0;
35 5
        foreach ($this->exceptions as $exception) {
36 4
            $result .= sprintf(
37 4
                "\n\n%d) %s:%s\n[%s] #%d: %s",
38
                ++$num,
39 4
                $exception->getFile(),
40 4
                $exception->getLine(),
41 4
                get_class($exception),
42 4
                $exception->getCode(),
43 4
                $exception->getMessage()
44
            );
45
        }
46 5
        return $result;
47
    }
48
}
49