Failed Conditions
Pull Request — master (#2825)
by Sébastien
05:15
created

AbstractMySQLDriver   F

Complexity

Total Complexity 69

Size/Duplication

Total Lines 185
Duplicated Lines 5.41 %

Coupling/Cohesion

Components 0
Dependencies 20

Test Coverage

Coverage 97.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 69
c 1
b 0
f 0
lcom 0
cbo 20
dl 10
loc 185
ccs 101
cts 104
cp 0.9712
rs 2.1568

5 Methods

Rating   Name   Duplication   Size   Complexity  
D convertException() 0 78 53
A getDatabase() 10 10 2
A getDatabasePlatform() 0 4 1
A getSchemaManager() 0 4 1
C createDatabasePlatformForVersion() 0 62 12

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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
 * 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\Driver;
21
22
use Doctrine\DBAL\DBALException;
23
use Doctrine\DBAL\Driver;
24
use Doctrine\DBAL\Exception;
25
use Doctrine\DBAL\Platforms\MariaDb102Platform;
26
use Doctrine\DBAL\Platforms\MySQL57Platform;
27
use Doctrine\DBAL\Platforms\MySqlPlatform;
28
use Doctrine\DBAL\Schema\MySqlSchemaManager;
29
use Doctrine\DBAL\VersionAwarePlatformDriver;
30
31
/**
32
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for MySQL based drivers.
33
 *
34
 * @author Steve Müller <[email protected]>
35
 * @link   www.doctrine-project.org
36
 * @since  2.5
37
 */
38
abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
39
{
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
44
     * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
45
     */
46 4
    public function convertException($message, DriverException $exception)
47
    {
48 4
        switch ($exception->getErrorCode()) {
49 4
            case '1213':
50 4
                return new Exception\DeadlockException($message, $exception);
51 4
            case '1205':
52 4
                return new Exception\LockWaitTimeoutException($message, $exception);
53 4
            case '1050':
54 4
                return new Exception\TableExistsException($message, $exception);
55
56 4
            case '1051':
57 4
            case '1146':
58 4
                return new Exception\TableNotFoundException($message, $exception);
59
60 4
            case '1216':
61 4
            case '1217':
62 4
            case '1451':
63 4
            case '1452':
64 4
            case '1701':
65 4
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
66
67 4
            case '1062':
68 4
            case '1557':
69 4
            case '1569':
70 4
            case '1586':
71 4
                return new Exception\UniqueConstraintViolationException($message, $exception);
72
73 4
            case '1054':
74 4
            case '1166':
75 4
            case '1611':
76 4
                return new Exception\InvalidFieldNameException($message, $exception);
77
78 4
            case '1052':
79 4
            case '1060':
80 4
            case '1110':
81 4
                return new Exception\NonUniqueFieldNameException($message, $exception);
82
83 4
            case '1064':
84 4
            case '1149':
85 4
            case '1287':
86 4
            case '1341':
87 4
            case '1342':
88 4
            case '1343':
89 4
            case '1344':
90 4
            case '1382':
91 4
            case '1479':
92 4
            case '1541':
93 4
            case '1554':
94 4
            case '1626':
95 4
                return new Exception\SyntaxErrorException($message, $exception);
96
97 4
            case '1044':
98 4
            case '1045':
99 4
            case '1046':
100 4
            case '1049':
101 4
            case '1095':
102 4
            case '1142':
103 4
            case '1143':
104 4
            case '1227':
105 4
            case '1370':
106 4
            case '1429':
107 4
            case '2002':
108 4
            case '2005':
109 4
                return new Exception\ConnectionException($message, $exception);
110
111 4
            case '1048':
112 4
            case '1121':
113 4
            case '1138':
114 4
            case '1171':
115 4
            case '1252':
116 4
            case '1263':
117 4
            case '1364':
118 4
            case '1566':
119 4
                return new Exception\NotNullConstraintViolationException($message, $exception);
120
        }
121
122 4
        return new Exception\DriverException($message, $exception);
123
    }
124
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 6
    public function createDatabasePlatformForVersion($version)
130
    {
131
        // Detect vendor and version
132 6
        $lowerCaseVersion = strtolower($version);
133
134 6
        if (false !== strpos($lowerCaseVersion, 'mariadb')) {
135
            // MariaDB does not follow one rule for versions.
136
            // - will catch things like '10.2.8-MariaDB-1~lenny-log'
137 3
            preg_match('/(?P<major>\d{1,2})\.(?P<minor>\d{1,2})\.(?P<patch>\d+)/', $lowerCaseVersion, $firstMatch);
138
            // - will catch things like '5.5.5-10.2.8-MariaDB-10.2.8+maria~xenial-log'
139 3
            preg_match('/\-(?P<major>1\d)\.(?P<minor>\d{1,2})\.(?P<patch>\d+)/', $lowerCaseVersion, $secondMatch);
140
141 3
            if (empty($firstMatch) && empty($secondMatch)
142
            ) {
143
                throw DBALException::invalidPlatformVersionSpecified(
144
                    $version,
145
                    'MariaDB-<major_version>.<minor_version>.<patch_version>' .
146
                                 'or <major_version>.<minor_version>.<patch_version>-MariaDB'
147
                );
148
            }
149
150 3
            $versionParts = array_merge($firstMatch, $secondMatch);
151
152 3
            $majorVersion = $versionParts['major'];
153 3
            $minorVersion = $versionParts['minor'];
154 3
            $patchVersion = $versionParts['patch'];
155
156 3
            $version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
157 3
            if (version_compare($version, '10.2.7', '>=')) {
158 3
                return new MariaDb102Platform();
159
            }
160
161
        } else {
162
            // Oracle MySQL
163
164 6
            if (!preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) {
165 3
                throw DBALException::invalidPlatformVersionSpecified(
166 3
                    $version,
167 3
                    '<major_version>.<minor_version>.<patch_version>'
168
                );
169
            }
170 3
            $majorVersion = $versionParts['major'];
171 3
            $minorVersion = isset($versionParts['minor']) ? $versionParts['minor'] : 0;
172 3
            $patchVersion = isset($versionParts['patch']) ? $versionParts['patch'] : null;
173
174 3
            if ('5' === $majorVersion && '7' === $minorVersion && null === $patchVersion) {
175 3
                $patchVersion = '9';
176
            }
177
178 3
            $version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
179
180 3
            if (version_compare($version, '5.7.9', '>=')) {
181 3
                return new MySQL57Platform();
182
            }
183
        }
184
185
186
187
188 3
        return $this->getDatabasePlatform();
189
190
    }
191
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 4 View Code Duplication
    public function getDatabase(\Doctrine\DBAL\Connection $conn)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
    {
198 4
        $params = $conn->getParams();
199
200 4
        if (isset($params['dbname'])) {
201 4
            return $params['dbname'];
202
        }
203
204 4
        return $conn->query('SELECT DATABASE()')->fetchColumn();
0 ignored issues
show
Bug Compatibility introduced by
The expression $conn->query('SELECT DATABASE()')->fetchColumn(); of type string|boolean adds the type boolean to the return on line 204 which is incompatible with the return type declared by the interface Doctrine\DBAL\Driver::getDatabase of type string.
Loading history...
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210 6
    public function getDatabasePlatform()
211
    {
212 6
        return new MySqlPlatform();
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218 3
    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
219
    {
220 3
        return new MySqlSchemaManager($conn);
221
    }
222
}
223