Failed Conditions
Pull Request — develop (#3525)
by Jonathan
09:58
created

DBALException::unknownDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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 Exception;
11
use Throwable;
12
use function array_map;
13
use function bin2hex;
14
use function implode;
15
use function is_resource;
16
use function is_string;
17
use function json_encode;
18
use function preg_replace;
19
use function sprintf;
20
21
class DBALException extends Exception
22
{
23
    /**
24
     * @param string  $sql
25
     * @param mixed[] $params
26
     *
27
     * @return self
28
     */
29 3666
    public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, $sql, array $params = [])
30
    {
31 3666
        $msg = "An exception occurred while executing '" . $sql . "'";
32 3666
        if ($params) {
33 3255
            $msg .= ' with params ' . self::formatParameters($params);
34
        }
35 3666
        $msg .= ":\n\n" . $driverEx->getMessage();
36
37 3666
        return static::wrapException($driver, $driverEx, $msg);
38
    }
39
40
    /**
41
     * @return self
42
     */
43 3121
    public static function driverException(Driver $driver, Throwable $driverEx)
44
    {
45 3121
        return static::wrapException($driver, $driverEx, 'An exception occurred in driver: ' . $driverEx->getMessage());
46
    }
47
48
    /**
49
     * @return self
50
     */
51 3672
    private static function wrapException(Driver $driver, Throwable $driverEx, $msg)
52
    {
53 3672
        if ($driverEx instanceof DriverException) {
54 3179
            return $driverEx;
55
        }
56 3670
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverExceptionInterface) {
57 3656
            return $driver->convertException($msg, $driverEx);
58
        }
59
60 3243
        return new self($msg, 0, $driverEx);
61
    }
62
63
    /**
64
     * Returns a human-readable representation of an array of parameters.
65
     * This properly handles binary data by returning a hex representation.
66
     *
67
     * @param mixed[] $params
68
     *
69
     * @return string
70
     */
71 3255
    private static function formatParameters(array $params)
72
    {
73
        return '[' . implode(', ', array_map(static function ($param) {
74 3255
            if (is_resource($param)) {
75 3204
                return (string) $param;
76
            }
77
78 3253
            $json = @json_encode($param);
79
80 3253
            if (! is_string($json) || $json === 'null' && is_string($param)) {
0 ignored issues
show
introduced by
The condition is_string($json) is always true.
Loading history...
81
                // JSON encoding failed, this is not a UTF-8 string.
82 3229
                return sprintf('"%s"', preg_replace('/.{2}/', '\\x$0', bin2hex($param)));
83
            }
84
85 3253
            return $json;
86 3255
        }, $params)) . ']';
87
    }
88
}
89