Total Complexity | 69 |
Total Lines | 189 |
Duplicated Lines | 5.29 % |
Coverage | 97.14% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 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 |
||
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 | * {@inheritdoc} |
||
127 | * |
||
128 | * @throws DBALException |
||
129 | */ |
||
130 | 6 | public function createDatabasePlatformForVersion($version) |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Get a normalized 'version number' from the server string |
||
146 | * returned by Oracle MySQL servers. |
||
147 | * |
||
148 | * @param string $versionString Version string returned by the driver, i.e. '5.7.10' |
||
149 | * @throws DBALException |
||
150 | */ |
||
151 | 6 | private function getOracleMysqlVersionNumber(string $versionString) : string |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * Detect MariaDB server version, including hack for some mariadb distributions |
||
176 | * that starts with the prefix '5.5.5-' |
||
177 | * |
||
178 | * @param string $versionString Version string as returned by mariadb server, i.e. '5.5.5-Mariadb-10.0.8-xenial' |
||
179 | * @throws DBALException |
||
180 | */ |
||
181 | 3 | private function getMariaDbMysqlVersionNumber(string $versionString) : string |
|
182 | { |
||
183 | 3 | if ( ! preg_match( |
|
184 | 3 | '/^(?:5\.5\.5-)?(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/i', |
|
185 | 3 | $versionString, |
|
186 | 3 | $versionParts |
|
187 | )) { |
||
188 | throw DBALException::invalidPlatformVersionSpecified( |
||
189 | $versionString, |
||
190 | '^(?:5\.5\.5-)?(mariadb-)?<major_version>.<minor_version>.<patch_version>' |
||
191 | ); |
||
192 | } |
||
193 | |||
194 | 3 | return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch']; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | 4 | View Code Duplication | public function getDatabase(\Doctrine\DBAL\Connection $conn) |
|
|||
201 | { |
||
202 | 4 | $params = $conn->getParams(); |
|
203 | |||
204 | 4 | if (isset($params['dbname'])) { |
|
205 | 4 | return $params['dbname']; |
|
206 | } |
||
207 | |||
208 | 4 | return $conn->query('SELECT DATABASE()')->fetchColumn(); |
|
209 | } |
||
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | * @return MySqlPlatform |
||
214 | */ |
||
215 | 6 | public function getDatabasePlatform() |
|
216 | { |
||
217 | 6 | return new MySqlPlatform(); |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * {@inheritdoc} |
||
222 | * @return MySqlSchemaManager |
||
223 | */ |
||
224 | 3 | public function getSchemaManager(\Doctrine\DBAL\Connection $conn) |
|
227 | } |
||
228 | } |
||
229 |
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.