Passed
Pull Request — master (#3025)
by Michael
19:27 queued 26s
created

DBALException   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Test Coverage

Coverage 81.93%

Importance

Changes 0
Metric Value
wmc 29
dl 0
loc 266
ccs 68
cts 83
cp 0.8193
rs 10
c 0
b 0
f 0

19 Methods

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