Failed Conditions
Pull Request — develop (#3131)
by Michael
60:38
created

DBALException   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
dl 0
loc 75
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A wrapException() 0 10 4
B formatParameters() 0 16 5
A driverExceptionDuringQuery() 0 9 2
A driverException() 0 3 1
1
<?php
2
3
namespace Doctrine\DBAL;
4
5
use Doctrine\DBAL\Exception;
6
use Doctrine\DBAL\Driver;
7
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use function array_map;
10
use function bin2hex;
11
use function implode;
12
use function is_resource;
13
use function is_string;
14
use function json_encode;
15
use function sprintf;
16
use function str_split;
17
18
class DBALException extends \Exception
19
{
20
21
    /**
22
     * @param \Doctrine\DBAL\Driver $driver
23
     * @param \Exception            $driverEx
24
     * @param string                $sql
25
     * @param array                 $params
26
     *
27
     * @return \Doctrine\DBAL\DBALException
28
     */
29
    public static function driverExceptionDuringQuery(Driver $driver, \Exception $driverEx, $sql, array $params = [])
30
    {
31
        $msg = "An exception occurred while executing '".$sql."'";
32
        if ($params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
33
            $msg .= " with params " . self::formatParameters($params);
34
        }
35
        $msg .= ":\n\n".$driverEx->getMessage();
36
37
        return static::wrapException($driver, $driverEx, $msg);
38
    }
39
40
    /**
41
     * @param \Doctrine\DBAL\Driver $driver
42
     * @param \Exception            $driverEx
43
     *
44
     * @return \Doctrine\DBAL\DBALException
45
     */
46
    public static function driverException(Driver $driver, \Exception $driverEx)
47
    {
48
        return static::wrapException($driver, $driverEx, "An exception occurred in driver: " . $driverEx->getMessage());
49
    }
50
51
    /**
52
     * @param \Doctrine\DBAL\Driver $driver
53
     * @param \Exception            $driverEx
54
     *
55
     * @return \Doctrine\DBAL\DBALException
56
     */
57
    private static function wrapException(Driver $driver, \Exception $driverEx, $msg)
58
    {
59
        if ($driverEx instanceof Exception\DriverException) {
60
            return $driverEx;
61
        }
62
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof Driver\DriverException) {
63
            return $driver->convertException($msg, $driverEx);
64
        }
65
66
        return new self($msg, 0, $driverEx);
67
    }
68
69
    /**
70
     * Returns a human-readable representation of an array of parameters.
71
     * This properly handles binary data by returning a hex representation.
72
     *
73
     * @param array $params
74
     *
75
     * @return string
76
     */
77
    private static function formatParameters(array $params)
78
    {
79
        return '[' . implode(', ', array_map(function ($param) {
80
            if (is_resource($param)) {
81
                return (string) $param;
82
            }
83
            
84
            $json = @json_encode($param);
85
86
            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...
87
                // JSON encoding failed, this is not a UTF-8 string.
88
                return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"';
89
            }
90
91
            return $json;
92
        }, $params)) . ']';
93
    }
94
}
95