Failed Conditions
Push — master ( 6fdfca...ce7560 )
by Marco
06:09 queued 01:06
created

DBALException::invalidPlatformType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 1
crap 2
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 49
    public static function notSupported($method)
35
    {
36 49
        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 string|null 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 69
    public static function driverExceptionDuringQuery(Driver $driver, \Exception $driverEx, $sql, array $params = [])
143
    {
144 69
        $msg = "An exception occurred while executing '".$sql."'";
145 69
        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 9
            $msg .= " with params " . self::formatParameters($params);
147
        }
148 69
        $msg .= ":\n\n".$driverEx->getMessage();
149
150 69
        return static::wrapException($driver, $driverEx, $msg);
0 ignored issues
show
Bug introduced by
Since wrapException() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of wrapException() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
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());
0 ignored issues
show
Bug introduced by
Since wrapException() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of wrapException() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
162
    }
163
164
    /**
165
     * @param \Doctrine\DBAL\Driver     $driver
166
     * @param \Exception $driverEx
167
     *
168
     * @return \Doctrine\DBAL\DBALException
169
     */
170 70
    private static function wrapException(Driver $driver, \Exception $driverEx, $msg)
171
    {
172 70
        if ($driverEx instanceof Exception\DriverException) {
173 1
            return $driverEx;
174
        }
175 69
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof Driver\DriverException) {
176 63
            return $driver->convertException($msg, $driverEx);
177
        }
178
179 6
        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 9
        return '[' . implode(', ', array_map(function ($param) {
193 9
            $json = @json_encode($param);
194
195 9
            if (! is_string($json) || $json == 'null' && is_string($param)) {
196
                // JSON encoding failed, this is not a UTF-8 string.
197 1
                return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"';
198
            }
199
200 9
            return $json;
201 9
        }, $params)) . ']';
202
    }
203
204
    /**
205
     * @param string $wrapperClass
206
     *
207
     * @return \Doctrine\DBAL\DBALException
208
     */
209 1
    public static function invalidWrapperClass($wrapperClass)
210
    {
211 1
        return new self("The given 'wrapperClass' ".$wrapperClass." has to be a ".
212 1
            "subtype of \Doctrine\DBAL\Connection.");
213
    }
214
215
    /**
216
     * @param string $driverClass
217
     *
218
     * @return \Doctrine\DBAL\DBALException
219
     */
220 1
    public static function invalidDriverClass($driverClass)
221
    {
222 1
        return new self("The given 'driverClass' ".$driverClass." has to implement the ".
223 1
            "\Doctrine\DBAL\Driver interface.");
224
    }
225
226
    /**
227
     * @param string $tableName
228
     *
229
     * @return \Doctrine\DBAL\DBALException
230
     */
231 1
    public static function invalidTableName($tableName)
232
    {
233 1
        return new self("Invalid table name specified: ".$tableName);
234
    }
235
236
    /**
237
     * @param string $tableName
238
     *
239
     * @return \Doctrine\DBAL\DBALException
240
     */
241 16
    public static function noColumnsSpecifiedForTable($tableName)
242
    {
243 16
        return new self("No columns specified for table ".$tableName);
244
    }
245
246
    /**
247
     * @return \Doctrine\DBAL\DBALException
248
     */
249
    public static function limitOffsetInvalid()
250
    {
251
        return new self("Invalid Offset in Limit Query, it has to be larger than or equal to 0.");
252
    }
253
254
    /**
255
     * @param string $name
256
     *
257
     * @return \Doctrine\DBAL\DBALException
258
     */
259
    public static function typeExists($name)
260
    {
261
        return new self('Type '.$name.' already exists.');
262
    }
263
264
    /**
265
     * @param string $name
266
     *
267
     * @return \Doctrine\DBAL\DBALException
268
     */
269
    public static function unknownColumnType($name)
270
    {
271
        return new self('Unknown column type "'.$name.'" requested. Any Doctrine type that you use has ' .
272
            'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .
273
            'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .
274
            'introspection then you might have forgotten to register all database types for a Doctrine Type. Use ' .
275
            'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .
276
            'Type#getMappedDatabaseTypes(). If the type name is empty you might ' .
277
            'have a problem with the cache or forgot some mapping information.'
278
        );
279
    }
280
281
    /**
282
     * @param string $name
283
     *
284
     * @return \Doctrine\DBAL\DBALException
285
     */
286 16
    public static function typeNotFound($name)
287
    {
288 16
        return new self('Type to be overwritten '.$name.' does not exist.');
289
    }
290
}
291