Failed Conditions
Pull Request — develop (#3536)
by Jonathan
61:12
created

DBALException::formatParameters()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 1
nop 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL;
6
7
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
8
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
9
use Doctrine\DBAL\Exception\DriverException;
10
use Doctrine\DBAL\Exception\FormatArray;
11
use Exception;
12
use Throwable;
13
use function sprintf;
14
15
class DBALException extends Exception
16
{
17
    /**
18
     * @param string  $sql
19
     * @param mixed[] $params
20
     *
21
     * @return self
22
     */
23
    public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, $sql, array $params = [])
24
    {
25
        $messageFormat = <<<'MESSAGE'
26
An exception occurred while executing "%s"%s:
27
28
%s.
29 3666
MESSAGE;
30
31 3666
        $message = sprintf(
32 3666
            $messageFormat,
33 3255
            $sql,
34
            $params !== [] ? sprintf(' with params %s', (new FormatArray())->__invoke($params)) : '',
35 3666
            $driverEx->getMessage()
36
        );
37 3666
38
        return static::wrapException($driver, $driverEx, $message);
39
    }
40
41
    /**
42
     * @return self
43 3121
     */
44
    public static function driverException(Driver $driver, Throwable $driverEx)
45 3121
    {
46
        return static::wrapException($driver, $driverEx, sprintf('An exception occurred in driver with message: %s', $driverEx->getMessage()));
47
    }
48
49
    /**
50
     * @return self
51 3672
     */
52
    private static function wrapException(Driver $driver, Throwable $driverEx, $msg)
53 3672
    {
54 3179
        if ($driverEx instanceof DriverException) {
55
            return $driverEx;
56 3670
        }
57 3656
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverExceptionInterface) {
58
            return $driver->convertException($msg, $driverEx);
59
        }
60 3243
61
        return new self($msg, 0, $driverEx);
62
    }
63
}
64