Completed
Pull Request — master (#3650)
by Matthew
14:23
created

AbstractMySQLDriver::convertException()   D

Complexity

Conditions 57
Paths 56

Size

Total Lines 84
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 68
CRAP Score 57.2459

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 84
ccs 68
cts 71
cp 0.9577
rs 4.1666
c 0
b 0
f 0
cc 57
nc 56
nop 2
crap 57.2459

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use Doctrine\DBAL\Connection;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Doctrine\DBAL\Driver\Connection. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Doctrine\DBAL\DBALException;
7
use Doctrine\DBAL\Driver;
8
use Doctrine\DBAL\Exception;
9
use Doctrine\DBAL\Platforms\MariaDb1027Platform;
10
use Doctrine\DBAL\Platforms\MySQL57Platform;
11
use Doctrine\DBAL\Platforms\MySQL80Platform;
12
use Doctrine\DBAL\Platforms\MySqlPlatform;
13
use Doctrine\DBAL\Schema\MySqlSchemaManager;
14
use Doctrine\DBAL\VersionAwarePlatformDriver;
15
use function preg_match;
16
use function stripos;
17
use function version_compare;
18
19
/**
20
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for MySQL based drivers.
21
 */
22
abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
23
{
24
    /**
25
     * {@inheritdoc}
26
     *
27
     * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
28
     * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
29
     */
30 7948
    public function convertException($message, DriverException $exception)
31
    {
32 7948
        switch ($exception->getErrorCode()) {
33 7948
            case '1213':
34 6348
                return new Exception\DeadlockException($message, $exception);
35 7940
            case '1205':
36 6323
                return new Exception\LockWaitTimeoutException($message, $exception);
37 7932
            case '1050':
38 6523
                return new Exception\TableExistsException($message, $exception);
39
40 7924
            case '1051':
41 7916
            case '1146':
42 6506
                return new Exception\TableNotFoundException($message, $exception);
43
44 7908
            case '1216':
45 7900
            case '1217':
46 7892
            case '1451':
47 7884
            case '1452':
48 7876
            case '1701':
49 7297
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
50
51 7876
            case '1062':
52 7868
            case '1557':
53 7860
            case '1569':
54 7852
            case '1586':
55 6472
                return new Exception\UniqueConstraintViolationException($message, $exception);
56
57 7844
            case '1054':
58 7836
            case '1166':
59 7828
            case '1611':
60 7189
                return new Exception\InvalidFieldNameException($message, $exception);
61
62 7820
            case '1052':
63 7812
            case '1060':
64 7804
            case '1110':
65 7114
                return new Exception\NonUniqueFieldNameException($message, $exception);
66
67 7796
            case '1064':
68 7788
            case '1149':
69 7780
            case '1287':
70 7772
            case '1341':
71 7764
            case '1342':
72 7756
            case '1343':
73 7748
            case '1344':
74 7740
            case '1382':
75 7732
            case '1479':
76 7724
            case '1541':
77 7716
            case '1554':
78 7708
            case '1626':
79 6911
                return new Exception\SyntaxErrorException($message, $exception);
80
81 7700
            case '1044':
82 7667
            case '1045':
83 7634
            case '1046':
84 7601
            case '1049':
85 7568
            case '1095':
86 7535
            case '1142':
87 7502
            case '1143':
88 7469
            case '1227':
89 7436
            case '1370':
90 7403
            case '1429':
91 7403
            case '2002':
92 7370
            case '2005':
93 7087
            case '2054':
94 7628
                return new Exception\ConnectionException($message, $exception);
95
96 7087
            case '2006':
97
                if ($exception instanceof Driver\Mysqli\MysqliConnectionException || $exception instanceof PDOConnectionException) {
98
                    return new Exception\ConnectionException($message, $exception);
99
                }
100
                break;
101
102 7087
            case '1048':
103 7054
            case '1121':
104 7021
            case '1138':
105 6988
            case '1171':
106 6955
            case '1252':
107 6922
            case '1263':
108 6889
            case '1364':
109 6856
            case '1566':
110 7079
                return new Exception\NotNullConstraintViolationException($message, $exception);
111
        }
112
113 6298
        return new Exception\DriverException($message, $exception);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     *
119
     * @throws DBALException
120
     */
121 6277
    public function createDatabasePlatformForVersion($version)
122
    {
123 6277
        $mariadb = stripos($version, 'mariadb') !== false;
124 6277
        if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.7', '>=')) {
125 6271
            return new MariaDb1027Platform();
126
        }
127
128 6277
        if (! $mariadb) {
129 6277
            $oracleMysqlVersion = $this->getOracleMysqlVersionNumber($version);
130 6271
            if (version_compare($oracleMysqlVersion, '8', '>=')) {
131 6271
                return new MySQL80Platform();
132
            }
133 6271
            if (version_compare($oracleMysqlVersion, '5.7.9', '>=')) {
134 6271
                return new MySQL57Platform();
135
            }
136
        }
137
138 6271
        return $this->getDatabasePlatform();
139
    }
140
141
    /**
142
     * Get a normalized 'version number' from the server string
143
     * returned by Oracle MySQL servers.
144
     *
145
     * @param string $versionString Version string returned by the driver, i.e. '5.7.10'
146
     *
147
     * @throws DBALException
148
     */
149 6277
    private function getOracleMysqlVersionNumber(string $versionString) : string
150
    {
151 6277
        if (! preg_match(
152 12
            '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/',
153 6277
            $versionString,
154 6277
            $versionParts
155
        )) {
156 6246
            throw DBALException::invalidPlatformVersionSpecified(
157 6
                $versionString,
158 6246
                '<major_version>.<minor_version>.<patch_version>'
159
            );
160
        }
161 6271
        $majorVersion = $versionParts['major'];
162 6271
        $minorVersion = $versionParts['minor'] ?? 0;
163 6271
        $patchVersion = $versionParts['patch'] ?? null;
164
165 6271
        if ($majorVersion === '5' && $minorVersion === '7' && $patchVersion === null) {
166 6271
            $patchVersion = '9';
167
        }
168
169 6271
        return $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
170
    }
171
172
    /**
173
     * Detect MariaDB server version, including hack for some mariadb distributions
174
     * that starts with the prefix '5.5.5-'
175
     *
176
     * @param string $versionString Version string as returned by mariadb server, i.e. '5.5.5-Mariadb-10.0.8-xenial'
177
     *
178
     * @throws DBALException
179
     */
180 6271
    private function getMariaDbMysqlVersionNumber(string $versionString) : string
181
    {
182 6271
        if (! preg_match(
183 6
            '/^(?:5\.5\.5-)?(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/i',
184 6271
            $versionString,
185 6271
            $versionParts
186
        )) {
187
            throw DBALException::invalidPlatformVersionSpecified(
188
                $versionString,
189
                '^(?:5\.5\.5-)?(mariadb-)?<major_version>.<minor_version>.<patch_version>'
190
            );
191
        }
192
193 6271
        return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch'];
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 7573
    public function getDatabase(Connection $conn)
200
    {
201 7573
        $params = $conn->getParams();
202
203 7573
        return $params['dbname'] ?? $conn->query('SELECT DATABASE()')->fetchColumn();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $params['dbname']...BASE()')->fetchColumn() also could return the type false which is incompatible with the return type mandated by Doctrine\DBAL\Driver::getDatabase() of string.
Loading history...
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     *
209
     * @return MySqlPlatform
210
     */
211 6277
    public function getDatabasePlatform()
212
    {
213 6277
        return new MySqlPlatform();
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     *
219
     * @return MySqlSchemaManager
220
     */
221 6196
    public function getSchemaManager(Connection $conn)
222
    {
223 6196
        return new MySqlSchemaManager($conn);
224
    }
225
}
226