Passed
Pull Request — master (#82)
by Aleksei
09:10
created

CumulativeException   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 19
c 1
b 0
f 0
dl 0
loc 37
ccs 21
cts 21
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessageDetails() 0 16 2
A __construct() 0 6 2
A getExceptions() 0 3 1
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 4
                ++$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