Passed
Push — type-registry ( c25a28...f90912 )
by Michael
23:20
created

DBALException::invalidDriverClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Doctrine\DBAL;
4
5
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
6
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
7
use Doctrine\DBAL\Exception\DriverException;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\DBAL\Types\Type;
10
use Exception;
11
use Throwable;
12
use function array_map;
13
use function bin2hex;
14
use function get_class;
15
use function gettype;
16
use function implode;
17
use function is_object;
18
use function is_resource;
19
use function is_string;
20
use function json_encode;
21
use function spl_object_id;
22
use function sprintf;
23
use function str_split;
24
25
class DBALException extends Exception
26
{
27
    /**
28
     * @param string $method
29
     *
30
     * @return \Doctrine\DBAL\DBALException
31
     */
32 1072
    public static function notSupported($method)
33
    {
34 1072
        return new self(sprintf("Operation '%s' is not supported by platform.", $method));
35
    }
36
37
    public static function invalidPlatformSpecified() : self
38
    {
39
        return new self(
40
            "Invalid 'platform' option specified, need to give an instance of " . AbstractPlatform::class . '.'
41
        );
42
    }
43
44
    /**
45
     * @param mixed $invalidPlatform
46
     */
47 63
    public static function invalidPlatformType($invalidPlatform) : self
48
    {
49 63
        if (is_object($invalidPlatform)) {
50 42
            return new self(
51 42
                sprintf(
52 42
                    "Option 'platform' must be a subtype of '%s', instance of '%s' given",
53 42
                    AbstractPlatform::class,
54 42
                    get_class($invalidPlatform)
55
                )
56
            );
57
        }
58
59 21
        return new self(
60 21
            sprintf(
61 21
                "Option 'platform' must be an object and subtype of '%s'. Got '%s'",
62 21
                AbstractPlatform::class,
63 21
                gettype($invalidPlatform)
64
            )
65
        );
66
    }
67
68
    /**
69
     * Returns a new instance for an invalid specified platform version.
70
     *
71
     * @param string $version        The invalid platform version given.
72
     * @param string $expectedFormat The expected platform version format.
73
     *
74
     * @return DBALException
75
     */
76 210
    public static function invalidPlatformVersionSpecified($version, $expectedFormat)
77
    {
78 210
        return new self(
79 210
            sprintf(
80
                'Invalid platform version "%s" specified. ' .
81 210
                'The platform version has to be specified in the format: "%s".',
82 210
                $version,
83 210
                $expectedFormat
84
            )
85
        );
86
    }
87
88
    /**
89
     * @return \Doctrine\DBAL\DBALException
90
     */
91 21
    public static function invalidPdoInstance()
92
    {
93 21
        return new self(
94
            "The 'pdo' option was used in DriverManager::getConnection() but no " .
95 21
            'instance of PDO was given.'
96
        );
97
    }
98
99
    /**
100
     * @param string|null $url The URL that was provided in the connection parameters (if any).
101
     *
102
     * @return \Doctrine\DBAL\DBALException
103
     */
104 105
    public static function driverRequired($url = null)
105
    {
106 105
        if ($url) {
107 84
            return new self(
108 84
                sprintf(
109
                    "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
110 84
                    'is given to DriverManager::getConnection(). Given URL: %s',
111 84
                    $url
112
                )
113
            );
114
        }
115
116 21
        return new self("The options 'driver' or 'driverClass' are mandatory if no PDO " .
117 21
            'instance is given to DriverManager::getConnection().');
118
    }
119
120
    /**
121
     * @param string   $unknownDriverName
122
     * @param string[] $knownDrivers
123
     *
124
     * @return \Doctrine\DBAL\DBALException
125
     */
126 21
    public static function unknownDriver($unknownDriverName, array $knownDrivers)
127
    {
128 21
        return new self("The given 'driver' " . $unknownDriverName . ' is unknown, ' .
129 21
            'Doctrine currently supports only the following drivers: ' . implode(', ', $knownDrivers));
130
    }
131
132
    /**
133
     * @param Exception $driverEx
134
     * @param string    $sql
135
     * @param mixed[]   $params
136
     *
137
     * @return \Doctrine\DBAL\DBALException
138
     */
139 2947
    public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, $sql, array $params = [])
140
    {
141 2947
        $msg = "An exception occurred while executing '" . $sql . "'";
142 2947
        if ($params) {
143 235
            $msg .= ' with params ' . self::formatParameters($params);
144
        }
145 2947
        $msg .= ":\n\n" . $driverEx->getMessage();
146
147 2947
        return static::wrapException($driver, $driverEx, $msg);
148
    }
149
150
    /**
151
     * @param Exception $driverEx
152
     *
153
     * @return \Doctrine\DBAL\DBALException
154
     */
155 70
    public static function driverException(Driver $driver, Throwable $driverEx)
156
    {
157 70
        return static::wrapException($driver, $driverEx, 'An exception occurred in driver: ' . $driverEx->getMessage());
158
    }
159
160
    /**
161
     * @param Exception $driverEx
162
     *
163
     * @return \Doctrine\DBAL\DBALException
164
     */
165 3017
    private static function wrapException(Driver $driver, Throwable $driverEx, $msg)
166
    {
167 3017
        if ($driverEx instanceof DriverException) {
168 21
            return $driverEx;
169
        }
170 2996
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverExceptionInterface) {
171 2776
            return $driver->convertException($msg, $driverEx);
172
        }
173
174 220
        return new self($msg, 0, $driverEx);
175
    }
176
177
    /**
178
     * Returns a human-readable representation of an array of parameters.
179
     * This properly handles binary data by returning a hex representation.
180
     *
181
     * @param mixed[] $params
182
     *
183
     * @return string
184
     */
185 235
    private static function formatParameters(array $params)
186
    {
187
        return '[' . implode(', ', array_map(static function ($param) {
188 235
            if (is_resource($param)) {
189 21
                return (string) $param;
190
            }
191
192 214
            $json = @json_encode($param);
193
194 214
            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...
195
                // JSON encoding failed, this is not a UTF-8 string.
196 21
                return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"';
197
            }
198
199 214
            return $json;
200 235
        }, $params)) . ']';
201
    }
202
203
    /**
204
     * @param string $wrapperClass
205
     *
206
     * @return \Doctrine\DBAL\DBALException
207
     */
208 21
    public static function invalidWrapperClass($wrapperClass)
209
    {
210 21
        return new self("The given 'wrapperClass' " . $wrapperClass . ' has to be a ' .
211 21
            'subtype of \Doctrine\DBAL\Connection.');
212
    }
213
214
    /**
215
     * @param string $driverClass
216
     *
217
     * @return \Doctrine\DBAL\DBALException
218
     */
219 21
    public static function invalidDriverClass($driverClass)
220
    {
221 21
        return new self("The given 'driverClass' " . $driverClass . ' has to implement the ' . Driver::class . ' interface.');
222
    }
223
224
    /**
225
     * @param string $tableName
226
     *
227
     * @return \Doctrine\DBAL\DBALException
228
     */
229 21
    public static function invalidTableName($tableName)
230
    {
231 21
        return new self('Invalid table name specified: ' . $tableName);
232
    }
233
234
    /**
235
     * @param string $tableName
236
     *
237
     * @return \Doctrine\DBAL\DBALException
238
     */
239 378
    public static function noColumnsSpecifiedForTable($tableName)
240
    {
241 378
        return new self('No columns specified for table ' . $tableName);
242
    }
243
244
    /**
245
     * @return \Doctrine\DBAL\DBALException
246
     */
247
    public static function limitOffsetInvalid()
248
    {
249
        return new self('Invalid Offset in Limit Query, it has to be larger than or equal to 0.');
250
    }
251
252
    /**
253
     * @param string $name
254
     *
255
     * @return \Doctrine\DBAL\DBALException
256
     */
257 42
    public static function typeExists($name)
258
    {
259 42
        return new self('Type ' . $name . ' already exists.');
260
    }
261
262
    /**
263
     * @param string $name
264
     *
265
     * @return \Doctrine\DBAL\DBALException
266
     */
267 21
    public static function unknownColumnType($name)
268
    {
269 21
        return new self('Unknown column type "' . $name . '" requested. Any Doctrine type that you use has ' .
270 21
            'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .
271 21
            'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .
272 21
            'introspection then you might have forgotten to register all database types for a Doctrine Type. Use ' .
273 21
            'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .
274 21
            'Type#getMappedDatabaseTypes(). If the type name is empty you might ' .
275 21
            'have a problem with the cache or forgot some mapping information.');
276
    }
277
278
    /**
279
     * @param string $name
280
     *
281
     * @return \Doctrine\DBAL\DBALException
282
     */
283 399
    public static function typeNotFound($name)
284
    {
285 399
        return new self('Type to be overwritten ' . $name . ' does not exist.');
286
    }
287
288 21
    public static function typeNotRegistered(Type $type) : self
289
    {
290 21
        return new self(sprintf('Type of the class %s@%d is not registered.', get_class($type), spl_object_id($type)));
291
    }
292
293 21
    public static function typeAlreadyRegistered(Type $type) : self
294
    {
295 21
        return new self(
296 21
            sprintf('Type of the class %s@%d is already registered.', get_class($type), spl_object_id($type))
297
        );
298
    }
299
}
300