Passed
Push — exceptions ( 722472 )
by Michael
15:25
created

DBALException::unknownColumnType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 2
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 mixed $invalidPlatform
23
     */
24 48
    public static function invalidPlatformType($invalidPlatform) : self
25
    {
26 48
        if (\is_object($invalidPlatform)) {
27 32
            return new self(
28 32
                sprintf(
29 32
                    "Option 'platform' must be a subtype of '%s', instance of '%s' given",
30 32
                    AbstractPlatform::class,
31 32
                    \get_class($invalidPlatform)
32
                )
33
            );
34
        }
35
36 16
        return new self(
37 16
            sprintf(
38 16
                "Option 'platform' must be an object and subtype of '%s'. Got '%s'",
39 16
                AbstractPlatform::class,
40 16
                \gettype($invalidPlatform)
41
            )
42
        );
43
    }
44
45
    /**
46
     * @param string|null $url The URL that was provided in the connection parameters (if any).
47
     *
48
     * @return \Doctrine\DBAL\DBALException
49
     */
50 80
    public static function driverRequired($url = null)
51
    {
52 80
        if ($url) {
53 64
            return new self(
54 64
                sprintf(
55
                    "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
56 64
                    "is given to DriverManager::getConnection(). Given URL: %s",
57 64
                    $url
58
                )
59
            );
60
        }
61
62 16
        return new self("The options 'driver' or 'driverClass' are mandatory if no PDO ".
63 16
            "instance is given to DriverManager::getConnection().");
64
    }
65
66
    /**
67
     * @param string $unknownDriverName
68
     * @param array  $knownDrivers
69
     *
70
     * @return \Doctrine\DBAL\DBALException
71
     */
72 16
    public static function unknownDriver($unknownDriverName, array $knownDrivers)
73
    {
74 16
        return new self("The given 'driver' ".$unknownDriverName." is unknown, ".
75 16
            "Doctrine currently supports only the following drivers: ".implode(", ", $knownDrivers));
76
    }
77
78
    /**
79
     * @param \Doctrine\DBAL\Driver $driver
80
     * @param \Exception            $driverEx
81
     * @param string                $sql
82
     * @param array                 $params
83
     *
84
     * @return \Doctrine\DBAL\DBALException
85
     */
86 2145
    public static function driverExceptionDuringQuery(Driver $driver, \Exception $driverEx, $sql, array $params = [])
87
    {
88 2145
        $msg = "An exception occurred while executing '".$sql."'";
89 2145
        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...
90 175
            $msg .= " with params " . self::formatParameters($params);
91
        }
92 2145
        $msg .= ":\n\n".$driverEx->getMessage();
93
94 2145
        return static::wrapException($driver, $driverEx, $msg);
95
    }
96
97
    /**
98
     * @param \Doctrine\DBAL\Driver $driver
99
     * @param \Exception            $driverEx
100
     *
101
     * @return \Doctrine\DBAL\DBALException
102
     */
103 48
    public static function driverException(Driver $driver, \Exception $driverEx)
104
    {
105 48
        return static::wrapException($driver, $driverEx, "An exception occurred in driver: " . $driverEx->getMessage());
106
    }
107
108
    /**
109
     * @param \Doctrine\DBAL\Driver $driver
110
     * @param \Exception            $driverEx
111
     *
112
     * @return \Doctrine\DBAL\DBALException
113
     */
114 2193
    private static function wrapException(Driver $driver, \Exception $driverEx, $msg)
115
    {
116 2193
        if ($driverEx instanceof Exception\DriverException) {
117 16
            return $driverEx;
118
        }
119 2177
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof Driver\DriverException) {
120 1887
            return $driver->convertException($msg, $driverEx);
121
        }
122
123 290
        return new self($msg, 0, $driverEx);
124
    }
125
126
    /**
127
     * Returns a human-readable representation of an array of parameters.
128
     * This properly handles binary data by returning a hex representation.
129
     *
130
     * @param array $params
131
     *
132
     * @return string
133
     */
134 175
    private static function formatParameters(array $params)
135
    {
136
        return '[' . implode(', ', array_map(function ($param) {
137 175
            if (is_resource($param)) {
138 16
                return (string) $param;
139
            }
140
            
141 159
            $json = @json_encode($param);
142
143 159
            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...
144
                // JSON encoding failed, this is not a UTF-8 string.
145 16
                return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"';
146
            }
147
148 159
            return $json;
149 175
        }, $params)) . ']';
150
    }
151
}
152