Passed
Push — master ( 0a7ead...c8e6c9 )
by Michael
10:47 queued 02:29
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 invalidPlatformSpecified() 0 5 1
A unknownDriver() 0 4 1
A driverRequired() 0 14 2
A notSupported() 0 3 1
A invalidPlatformVersionSpecified() 0 8 1
A invalidPdoInstance() 0 5 1
A wrapException() 0 10 4
A invalidPlatformType() 0 17 2
A driverExceptionDuringQuery() 0 9 2
A driverException() 0 3 1
A typeExists() 0 3 1
A typeNotFound() 0 3 1
A limitOffsetInvalid() 0 3 1
A unknownColumnType() 0 9 1
A invalidWrapperClass() 0 4 1
A invalidTableName() 0 3 1
A invalidDriverClass() 0 4 1
A noColumnsSpecifiedForTable() 0 3 1
B formatParameters() 0 16 5
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
27
class DBALException extends \Exception
28
{
29
    /**
30
     * @param string $method
31
     *
32
     * @return \Doctrine\DBAL\DBALException
33
     */
34 51
    public static function notSupported($method)
35
    {
36 51
        return new self("Operation '$method' is not supported by platform.");
37
    }
38
39
    public static function invalidPlatformSpecified() : self
40
    {
41
        return new self(
42
            "Invalid 'platform' option specified, need to give an instance of ".
43
            "\Doctrine\DBAL\Platforms\AbstractPlatform.");
44
    }
45
46
    /**
47
     * @param mixed $invalidPlatform
48
     */
49 3
    public static function invalidPlatformType($invalidPlatform) : self
50
    {
51 3
        if (\is_object($invalidPlatform)) {
52 2
            return new self(
53 2
                sprintf(
54 2
                    "Option 'platform' must be a subtype of '%s', instance of '%s' given",
55 2
                    AbstractPlatform::class,
56 2
                    \get_class($invalidPlatform)
57
                )
58
            );
59
        }
60
61 1
        return new self(
62 1
            sprintf(
63 1
                "Option 'platform' must be an object and subtype of '%s'. Got '%s'",
64 1
                AbstractPlatform::class,
65 1
                \gettype($invalidPlatform)
66
            )
67
        );
68
    }
69
70
    /**
71
     * Returns a new instance for an invalid specified platform version.
72
     *
73
     * @param string $version        The invalid platform version given.
74
     * @param string $expectedFormat The expected platform version format.
75
     *
76
     * @return DBALException
77
     */
78 10
    public static function invalidPlatformVersionSpecified($version, $expectedFormat)
79
    {
80 10
        return new self(
81 10
            sprintf(
82
                'Invalid platform version "%s" specified. ' .
83 10
                'The platform version has to be specified in the format: "%s".',
84 10
                $version,
85 10
                $expectedFormat
86
            )
87
        );
88
    }
89
90
    /**
91
     * @return \Doctrine\DBAL\DBALException
92
     */
93 1
    public static function invalidPdoInstance()
94
    {
95 1
        return new self(
96
            "The 'pdo' option was used in DriverManager::getConnection() but no ".
97 1
            "instance of PDO was given."
98
        );
99
    }
100
101
    /**
102
     * @param string|null $url The URL that was provided in the connection parameters (if any).
103
     *
104
     * @return \Doctrine\DBAL\DBALException
105
     */
106 5
    public static function driverRequired($url = null)
107
    {
108 5
        if ($url) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $url of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
109 4
            return new self(
110 4
                sprintf(
111
                    "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
112 4
                    "is given to DriverManager::getConnection(). Given URL: %s",
113 4
                    $url
114
                )
115
            );
116
        }
117
118 1
        return new self("The options 'driver' or 'driverClass' are mandatory if no PDO ".
119 1
            "instance is given to DriverManager::getConnection().");
120
    }
121
122
    /**
123
     * @param string $unknownDriverName
124
     * @param array  $knownDrivers
125
     *
126
     * @return \Doctrine\DBAL\DBALException
127
     */
128 1
    public static function unknownDriver($unknownDriverName, array $knownDrivers)
129
    {
130 1
        return new self("The given 'driver' ".$unknownDriverName." is unknown, ".
131 1
            "Doctrine currently supports only the following drivers: ".implode(", ", $knownDrivers));
132
    }
133
134
    /**
135
     * @param \Doctrine\DBAL\Driver     $driver
136
     * @param \Exception $driverEx
137
     * @param string     $sql
138
     * @param array      $params
139
     *
140
     * @return \Doctrine\DBAL\DBALException
141
     */
142 70
    public static function driverExceptionDuringQuery(Driver $driver, \Exception $driverEx, $sql, array $params = [])
143
    {
144 70
        $msg = "An exception occurred while executing '".$sql."'";
145 70
        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...
146 10
            $msg .= " with params " . self::formatParameters($params);
147
        }
148 70
        $msg .= ":\n\n".$driverEx->getMessage();
149
150 70
        return static::wrapException($driver, $driverEx, $msg);
151
    }
152
153
    /**
154
     * @param \Doctrine\DBAL\Driver     $driver
155
     * @param \Exception $driverEx
156
     *
157
     * @return \Doctrine\DBAL\DBALException
158
     */
159 1
    public static function driverException(Driver $driver, \Exception $driverEx)
160
    {
161 1
        return static::wrapException($driver, $driverEx, "An exception occurred in driver: " . $driverEx->getMessage());
162
    }
163
164
    /**
165
     * @param \Doctrine\DBAL\Driver     $driver
166
     * @param \Exception $driverEx
167
     *
168
     * @return \Doctrine\DBAL\DBALException
169
     */
170 71
    private static function wrapException(Driver $driver, \Exception $driverEx, $msg)
171
    {
172 71
        if ($driverEx instanceof Exception\DriverException) {
173 1
            return $driverEx;
174
        }
175 70
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof Driver\DriverException) {
176 63
            return $driver->convertException($msg, $driverEx);
177
        }
178
179 7
        return new self($msg, 0, $driverEx);
180
    }
181
182
    /**
183
     * Returns a human-readable representation of an array of parameters.
184
     * This properly handles binary data by returning a hex representation.
185
     *
186
     * @param array $params
187
     *
188
     * @return string
189
     */
190
    private static function formatParameters(array $params)
191
    {
192 10
        return '[' . implode(', ', array_map(function ($param) {
193 10
            if (is_resource($param)) {
194 1
                return (string) $param;
195
            }
196
            
197 9
            $json = @json_encode($param);
198
199 9
            if (! is_string($json) || $json == 'null' && is_string($param)) {
200
                // JSON encoding failed, this is not a UTF-8 string.
201 1
                return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"';
202
            }
203
204 9
            return $json;
205 10
        }, $params)) . ']';
206
    }
207
208
    /**
209
     * @param string $wrapperClass
210
     *
211
     * @return \Doctrine\DBAL\DBALException
212
     */
213 1
    public static function invalidWrapperClass($wrapperClass)
214
    {
215 1
        return new self("The given 'wrapperClass' ".$wrapperClass." has to be a ".
216 1
            "subtype of \Doctrine\DBAL\Connection.");
217
    }
218
219
    /**
220
     * @param string $driverClass
221
     *
222
     * @return \Doctrine\DBAL\DBALException
223
     */
224 1
    public static function invalidDriverClass($driverClass)
225
    {
226 1
        return new self("The given 'driverClass' ".$driverClass." has to implement the ".
227 1
            "\Doctrine\DBAL\Driver interface.");
228
    }
229
230
    /**
231
     * @param string $tableName
232
     *
233
     * @return \Doctrine\DBAL\DBALException
234
     */
235 1
    public static function invalidTableName($tableName)
236
    {
237 1
        return new self("Invalid table name specified: ".$tableName);
238
    }
239
240
    /**
241
     * @param string $tableName
242
     *
243
     * @return \Doctrine\DBAL\DBALException
244
     */
245 17
    public static function noColumnsSpecifiedForTable($tableName)
246
    {
247 17
        return new self("No columns specified for table ".$tableName);
248
    }
249
250
    /**
251
     * @return \Doctrine\DBAL\DBALException
252
     */
253
    public static function limitOffsetInvalid()
254
    {
255
        return new self("Invalid Offset in Limit Query, it has to be larger than or equal to 0.");
256
    }
257
258
    /**
259
     * @param string $name
260
     *
261
     * @return \Doctrine\DBAL\DBALException
262
     */
263
    public static function typeExists($name)
264
    {
265
        return new self('Type '.$name.' already exists.');
266
    }
267
268
    /**
269
     * @param string $name
270
     *
271
     * @return \Doctrine\DBAL\DBALException
272
     */
273
    public static function unknownColumnType($name)
274
    {
275
        return new self('Unknown column type "'.$name.'" requested. Any Doctrine type that you use has ' .
276
            'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .
277
            'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .
278
            'introspection then you might have forgotten to register all database types for a Doctrine Type. Use ' .
279
            'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .
280
            'Type#getMappedDatabaseTypes(). If the type name is empty you might ' .
281
            'have a problem with the cache or forgot some mapping information.'
282
        );
283
    }
284
285
    /**
286
     * @param string $name
287
     *
288
     * @return \Doctrine\DBAL\DBALException
289
     */
290 17
    public static function typeNotFound($name)
291
    {
292 17
        return new self('Type to be overwritten '.$name.' does not exist.');
293
    }
294
}
295