Failed Conditions
Pull Request — master (#3354)
by Michael
12:53
created

DBALException   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Test Coverage

Coverage 94.25%

Importance

Changes 0
Metric Value
wmc 31
eloc 70
dl 0
loc 272
ccs 82
cts 87
cp 0.9425
rs 9.92
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A typeAlreadyRegistered() 0 4 1
A typeExists() 0 3 1
A invalidPlatformSpecified() 0 4 1
A unknownDriver() 0 4 1
A typeNotFound() 0 3 1
A limitOffsetInvalid() 0 3 1
A unknownColumnType() 0 9 1
A driverRequired() 0 14 2
A notSupported() 0 3 1
A invalidPlatformVersionSpecified() 0 8 1
A invalidWrapperClass() 0 4 1
A invalidTableName() 0 3 1
A typeNotRegistered() 0 3 1
A invalidDriverClass() 0 3 1
A invalidPdoInstance() 0 5 1
A wrapException() 0 10 4
A invalidPlatformType() 0 17 2
A driverExceptionDuringQuery() 0 9 2
A formatParameters() 0 16 5
A noColumnsSpecifiedForTable() 0 3 1
A driverException() 0 3 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_hash;
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 1225
    public static function notSupported($method)
33
    {
34 1225
        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 72
    public static function invalidPlatformType($invalidPlatform) : self
48
    {
49 72
        if (is_object($invalidPlatform)) {
50 48
            return new self(
51 48
                sprintf(
52 48
                    "Option 'platform' must be a subtype of '%s', instance of '%s' given",
53 48
                    AbstractPlatform::class,
54 48
                    get_class($invalidPlatform)
55
                )
56
            );
57
        }
58
59 24
        return new self(
60 24
            sprintf(
61 24
                "Option 'platform' must be an object and subtype of '%s'. Got '%s'",
62 24
                AbstractPlatform::class,
63 24
                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 240
    public static function invalidPlatformVersionSpecified($version, $expectedFormat)
77
    {
78 240
        return new self(
79 240
            sprintf(
80
                'Invalid platform version "%s" specified. ' .
81 240
                'The platform version has to be specified in the format: "%s".',
82 240
                $version,
83 240
                $expectedFormat
84
            )
85
        );
86
    }
87
88
    /**
89
     * @return \Doctrine\DBAL\DBALException
90
     */
91 24
    public static function invalidPdoInstance()
92
    {
93 24
        return new self(
94
            "The 'pdo' option was used in DriverManager::getConnection() but no " .
95 24
            '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 120
    public static function driverRequired($url = null)
105
    {
106 120
        if ($url) {
107 96
            return new self(
108 96
                sprintf(
109
                    "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
110 96
                    'is given to DriverManager::getConnection(). Given URL: %s',
111 96
                    $url
112
                )
113
            );
114
        }
115
116 24
        return new self("The options 'driver' or 'driverClass' are mandatory if no PDO " .
117 24
            '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 24
    public static function unknownDriver($unknownDriverName, array $knownDrivers)
127
    {
128 24
        return new self("The given 'driver' " . $unknownDriverName . ' is unknown, ' .
129 24
            '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 3312
    public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, $sql, array $params = [])
140
    {
141 3312
        $msg = "An exception occurred while executing '" . $sql . "'";
142 3312
        if ($params) {
143 260
            $msg .= ' with params ' . self::formatParameters($params);
144
        }
145 3312
        $msg .= ":\n\n" . $driverEx->getMessage();
146
147 3312
        return static::wrapException($driver, $driverEx, $msg);
148
    }
149
150
    /**
151
     * @param Exception $driverEx
152
     *
153
     * @return \Doctrine\DBAL\DBALException
154
     */
155 73
    public static function driverException(Driver $driver, Throwable $driverEx)
156
    {
157 73
        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 3385
    private static function wrapException(Driver $driver, Throwable $driverEx, $msg)
166
    {
167 3385
        if ($driverEx instanceof DriverException) {
168 24
            return $driverEx;
169
        }
170 3361
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverExceptionInterface) {
171 2933
            return $driver->convertException($msg, $driverEx);
172
        }
173
174 428
        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 260
    private static function formatParameters(array $params)
186
    {
187
        return '[' . implode(', ', array_map(static function ($param) {
188 260
            if (is_resource($param)) {
189 24
                return (string) $param;
190
            }
191
192 236
            $json = @json_encode($param);
193
194 236
            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 24
                return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"';
197
            }
198
199 236
            return $json;
200 260
        }, $params)) . ']';
201
    }
202
203
    /**
204
     * @param string $wrapperClass
205
     *
206
     * @return \Doctrine\DBAL\DBALException
207
     */
208 24
    public static function invalidWrapperClass($wrapperClass)
209
    {
210 24
        return new self("The given 'wrapperClass' " . $wrapperClass . ' has to be a ' .
211 24
            'subtype of \Doctrine\DBAL\Connection.');
212
    }
213
214
    /**
215
     * @param string $driverClass
216
     *
217
     * @return \Doctrine\DBAL\DBALException
218
     */
219 24
    public static function invalidDriverClass($driverClass)
220
    {
221 24
        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 24
    public static function invalidTableName($tableName)
230
    {
231 24
        return new self('Invalid table name specified: ' . $tableName);
232
    }
233
234
    /**
235
     * @param string $tableName
236
     *
237
     * @return \Doctrine\DBAL\DBALException
238
     */
239 432
    public static function noColumnsSpecifiedForTable($tableName)
240
    {
241 432
        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 48
    public static function typeExists($name)
258
    {
259 48
        return new self('Type ' . $name . ' already exists.');
260
    }
261
262
    /**
263
     * @param string $name
264
     *
265
     * @return \Doctrine\DBAL\DBALException
266
     */
267 24
    public static function unknownColumnType($name)
268
    {
269 24
        return new self('Unknown column type "' . $name . '" requested. Any Doctrine type that you use has ' .
270 24
            'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .
271 24
            'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .
272 24
            'introspection then you might have forgotten to register all database types for a Doctrine Type. Use ' .
273 24
            'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .
274 24
            'Type#getMappedDatabaseTypes(). If the type name is empty you might ' .
275 24
            '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 456
    public static function typeNotFound($name)
284
    {
285 456
        return new self('Type to be overwritten ' . $name . ' does not exist.');
286
    }
287
288 24
    public static function typeNotRegistered(Type $type) : self
289
    {
290 24
        return new self(sprintf('Type of the class %s@%s is not registered.', get_class($type), spl_object_hash($type)));
291
    }
292
293 24
    public static function typeAlreadyRegistered(Type $type) : self
294
    {
295 24
        return new self(
296 24
            sprintf('Type of the class %s@%s is already registered.', get_class($type), spl_object_hash($type))
297
        );
298
    }
299
}
300