Failed Conditions
Pull Request — master (#3645)
by Matthew
14:32
created

AbstractMySQLDriver   F

Complexity

Total Complexity 73

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Test Coverage

Coverage 94.64%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 73
eloc 105
c 1
b 1
f 0
dl 0
loc 200
ccs 106
cts 112
cp 0.9464
rs 2.56

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createDatabasePlatformForVersion() 0 18 6
A getOracleMysqlVersionNumber() 0 21 5
A getSchemaManager() 0 3 1
A getMariaDbMysqlVersionNumber() 0 14 2
A getDatabasePlatform() 0 3 1
A getDatabase() 0 5 1
D convertException() 0 82 57

How to fix   Complexity   

Complex Class

Complex classes like AbstractMySQLDriver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AbstractMySQLDriver, and based on these observations, apply Extract Interface, too.

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 7955
    public function convertException($message, DriverException $exception)
31
    {
32 7955
        switch ($exception->getErrorCode()) {
33 7955
            case '1213':
34 6355
                return new Exception\DeadlockException($message, $exception);
35 7947
            case '1205':
36 6330
                return new Exception\LockWaitTimeoutException($message, $exception);
37 7939
            case '1050':
38 6530
                return new Exception\TableExistsException($message, $exception);
39
40 7931
            case '1051':
41 7923
            case '1146':
42 6513
                return new Exception\TableNotFoundException($message, $exception);
43
44 7915
            case '1216':
45 7907
            case '1217':
46 7899
            case '1451':
47 7891
            case '1452':
48 7883
            case '1701':
49 7304
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
50
51 7883
            case '1062':
52 7875
            case '1557':
53 7867
            case '1569':
54 7859
            case '1586':
55 6479
                return new Exception\UniqueConstraintViolationException($message, $exception);
56
57 7851
            case '1054':
58 7843
            case '1166':
59 7835
            case '1611':
60 7196
                return new Exception\InvalidFieldNameException($message, $exception);
61
62 7827
            case '1052':
63 7819
            case '1060':
64 7811
            case '1110':
65 7121
                return new Exception\NonUniqueFieldNameException($message, $exception);
66
67 7803
            case '1064':
68 7795
            case '1149':
69 7787
            case '1287':
70 7779
            case '1341':
71 7771
            case '1342':
72 7763
            case '1343':
73 7755
            case '1344':
74 7747
            case '1382':
75 7739
            case '1479':
76 7731
            case '1541':
77 7723
            case '1554':
78 7715
            case '1626':
79 6918
                return new Exception\SyntaxErrorException($message, $exception);
80
81 7707
            case '1044':
82 7674
            case '1045':
83 7641
            case '1046':
84 7608
            case '1049':
85 7575
            case '1095':
86 7542
            case '1142':
87 7509
            case '1143':
88 7476
            case '1227':
89 7443
            case '1370':
90 7410
            case '1429':
91 7410
            case '2002':
92 7377
            case '2005':
93 7094
            case '2054':
94 7635
                return new Exception\ConnectionException($message, $exception);
95 7094
            case '2006':
96
                if ($exception instanceof Driver\Mysqli\MysqliConnectionException || $exception instanceof PDOConnectionException) {
97
                    return new Exception\ConnectionException($message, $exception);
98
                }
99
                break;
100 7094
            case '1048':
101 7061
            case '1121':
102 7028
            case '1138':
103 6995
            case '1171':
104 6962
            case '1252':
105 6929
            case '1263':
106 6896
            case '1364':
107 6863
            case '1566':
108 7086
                return new Exception\NotNullConstraintViolationException($message, $exception);
109
        }
110
111 6305
        return new Exception\DriverException($message, $exception);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     *
117
     * @throws DBALException
118
     */
119 6284
    public function createDatabasePlatformForVersion($version)
120
    {
121 6284
        $mariadb = stripos($version, 'mariadb') !== false;
122 6284
        if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.7', '>=')) {
123 6278
            return new MariaDb1027Platform();
124
        }
125
126 6284
        if (! $mariadb) {
127 6284
            $oracleMysqlVersion = $this->getOracleMysqlVersionNumber($version);
128 6278
            if (version_compare($oracleMysqlVersion, '8', '>=')) {
129 6278
                return new MySQL80Platform();
130
            }
131 6278
            if (version_compare($oracleMysqlVersion, '5.7.9', '>=')) {
132 6278
                return new MySQL57Platform();
133
            }
134
        }
135
136 6278
        return $this->getDatabasePlatform();
137
    }
138
139
    /**
140
     * Get a normalized 'version number' from the server string
141
     * returned by Oracle MySQL servers.
142
     *
143
     * @param string $versionString Version string returned by the driver, i.e. '5.7.10'
144
     *
145
     * @throws DBALException
146
     */
147 6284
    private function getOracleMysqlVersionNumber(string $versionString) : string
148
    {
149 6284
        if (! preg_match(
150 12
            '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/',
151 6284
            $versionString,
152 6284
            $versionParts
153
        )) {
154 6253
            throw DBALException::invalidPlatformVersionSpecified(
155 6
                $versionString,
156 6253
                '<major_version>.<minor_version>.<patch_version>'
157
            );
158
        }
159 6278
        $majorVersion = $versionParts['major'];
160 6278
        $minorVersion = $versionParts['minor'] ?? 0;
161 6278
        $patchVersion = $versionParts['patch'] ?? null;
162
163 6278
        if ($majorVersion === '5' && $minorVersion === '7' && $patchVersion === null) {
164 6278
            $patchVersion = '9';
165
        }
166
167 6278
        return $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
168
    }
169
170
    /**
171
     * Detect MariaDB server version, including hack for some mariadb distributions
172
     * that starts with the prefix '5.5.5-'
173
     *
174
     * @param string $versionString Version string as returned by mariadb server, i.e. '5.5.5-Mariadb-10.0.8-xenial'
175
     *
176
     * @throws DBALException
177
     */
178 6278
    private function getMariaDbMysqlVersionNumber(string $versionString) : string
179
    {
180 6278
        if (! preg_match(
181 6
            '/^(?:5\.5\.5-)?(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/i',
182 6278
            $versionString,
183 6278
            $versionParts
184
        )) {
185
            throw DBALException::invalidPlatformVersionSpecified(
186
                $versionString,
187
                '^(?:5\.5\.5-)?(mariadb-)?<major_version>.<minor_version>.<patch_version>'
188
            );
189
        }
190
191 6278
        return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch'];
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197 7580
    public function getDatabase(Connection $conn)
198
    {
199 7580
        $params = $conn->getParams();
200
201 7580
        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...
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     *
207
     * @return MySqlPlatform
208
     */
209 6284
    public function getDatabasePlatform()
210
    {
211 6284
        return new MySqlPlatform();
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     *
217
     * @return MySqlSchemaManager
218
     */
219 6203
    public function getSchemaManager(Connection $conn)
220
    {
221 6203
        return new MySqlSchemaManager($conn);
222
    }
223
}
224