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

DBALException   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Test Coverage

Coverage 91.46%

Importance

Changes 0
Metric Value
wmc 29
eloc 67
dl 0
loc 261
rs 10
c 0
b 0
f 0
ccs 75
cts 82
cp 0.9146

19 Methods

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