LoaderException::__construct()   B
last analyzed

Complexity

Conditions 10
Paths 36

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 10.3248

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 45
ccs 23
cts 27
cp 0.8519
rs 7.6666
c 1
b 0
f 0
cc 10
nc 36
nop 5
crap 10.3248

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Exception;
13
14
use Ocubom\Twig\Extension\Svg\Util\AggregatedExceptionTrait;
15
16
use function BenTools\IterableFunctions\iterable_to_array;
17
use function Ocubom\Twig\Extension\is_string;
18
19
class LoaderException extends RuntimeException
20
{
21
    use AggregatedExceptionTrait;
22
23
    /**
24
     * @param \Throwable|iterable<\Throwable> $previous
25
     */
26 6
    public function __construct(string $ident, \ReflectionClass $loader = null, string $source = null, int $code = 0, $previous = null)
27
    {
28 6
        if (is_iterable($previous)) {
29 4
            $previous = iterable_to_array(/* @scrutinizer ignore-type */ $previous, false);
30
31 4
            if (1 === count($previous)) {
32 4
                $previous = $previous[0];
33
            }
34
        }
35
36 6
        if ($previous instanceof \Throwable) {
37 4
            $lines = explode("\n", $previous->getMessage());
38 4
            $message = sprintf(
39 4
                '%s loading SVG for "%s"',
40 4
                rtrim($lines[0], " \n\r\t\v\x00.:"),
41 4
                $ident
42 4
            );
43
44 4
            if (is_string($source)) {
45
                $message .= sprintf(' on "%s"', $source);
46
            }
47
48 4
            if ($loader instanceof \ReflectionClass) {
49 4
                $message .= sprintf(' by "%s"', $loader->getShortName());
50
            }
51
52 4
            if (count($lines) > 1) {
53
                $message .= ':';
54
                $message .= implode("\n", array_slice($lines, 1));
55
            } else {
56 4
                $message .= '.';
57
            }
58
        } else {
59 6
            $message = $loader instanceof \ReflectionClass
60 6
                ? sprintf('"%s" cannot load SVG for "%s"', $loader->getShortName(), $ident)
61
                : sprintf('Unable to load SVG for "%s"', $ident);
62
63 6
            if (is_string($source)) {
64 4
                $message .= sprintf(' on "%s"', $source);
65
            }
66
67 6
            $message = $this->formatMessage($message, $previous);
68
        }
69
70 6
        parent::__construct($message, $code, $previous instanceof \Throwable ? $previous : null);
71
    }
72
}
73