Total Complexity | 65 |
Total Lines | 386 |
Duplicated Lines | 8.03 % |
Coverage | 0% |
Changes | 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 OracleSchemaManager 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 OracleSchemaManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class OracleSchemaManager extends AbstractSchemaManager |
||
35 | { |
||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | View Code Duplication | public function dropDatabase($database) |
|
|
|||
40 | { |
||
41 | try { |
||
42 | parent::dropDatabase($database); |
||
43 | } catch (DBALException $exception) { |
||
44 | $exception = $exception->getPrevious(); |
||
45 | |||
46 | if (! $exception instanceof DriverException) { |
||
47 | throw $exception; |
||
48 | } |
||
49 | |||
50 | // If we have a error code 1940 (ORA-01940), the drop database operation failed |
||
51 | // because of active connections on the database. |
||
52 | // To force dropping the database, we first have to close all active connections |
||
53 | // on that database and issue the drop database operation again. |
||
54 | if ($exception->getErrorCode() !== 1940) { |
||
55 | throw $exception; |
||
56 | } |
||
57 | |||
58 | $this->killUserSessions($database); |
||
59 | |||
60 | parent::dropDatabase($database); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | protected function _getPortableViewDefinition($view) |
||
68 | { |
||
69 | $view = \array_change_key_case($view, CASE_LOWER); |
||
70 | |||
71 | return new View($this->getQuotedIdentifierName($view['view_name']), $view['text']); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | protected function _getPortableUserDefinition($user) |
||
78 | { |
||
79 | $user = \array_change_key_case($user, CASE_LOWER); |
||
80 | |||
81 | return [ |
||
82 | 'user' => $user['username'], |
||
83 | ]; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | protected function _getPortableTableDefinition($table) |
||
90 | { |
||
91 | $table = \array_change_key_case($table, CASE_LOWER); |
||
92 | |||
93 | return $this->getQuotedIdentifierName($table['table_name']); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | * |
||
99 | * @license New BSD License |
||
100 | * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html |
||
101 | */ |
||
102 | protected function _getPortableTableIndexesList($tableIndexes, $tableName=null) |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | protected function _getPortableTableColumnDefinition($tableColumn) |
||
130 | { |
||
131 | $tableColumn = \array_change_key_case($tableColumn, CASE_LOWER); |
||
132 | |||
133 | $dbType = strtolower($tableColumn['data_type']); |
||
134 | if (strpos($dbType, "timestamp(") === 0) { |
||
135 | if (strpos($dbType, "with time zone")) { |
||
136 | $dbType = "timestamptz"; |
||
137 | } else { |
||
138 | $dbType = "timestamp"; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | $unsigned = $fixed = null; |
||
143 | |||
144 | if ( ! isset($tableColumn['column_name'])) { |
||
145 | $tableColumn['column_name'] = ''; |
||
146 | } |
||
147 | |||
148 | // Default values returned from database sometimes have trailing spaces. |
||
149 | $tableColumn['data_default'] = trim($tableColumn['data_default']); |
||
150 | |||
151 | View Code Duplication | if ($tableColumn['data_default'] === '' || $tableColumn['data_default'] === 'NULL') { |
|
152 | $tableColumn['data_default'] = null; |
||
153 | } |
||
154 | |||
155 | View Code Duplication | if (null !== $tableColumn['data_default']) { |
|
156 | // Default values returned from database are enclosed in single quotes. |
||
157 | $tableColumn['data_default'] = trim($tableColumn['data_default'], "'"); |
||
158 | } |
||
159 | |||
160 | $precision = null; |
||
161 | $scale = null; |
||
162 | |||
163 | $type = $this->_platform->getDoctrineTypeMapping($dbType); |
||
164 | $type = $this->extractDoctrineTypeFromComment($tableColumn['comments'], $type); |
||
165 | $tableColumn['comments'] = $this->removeDoctrineTypeFromComment($tableColumn['comments'], $type); |
||
166 | |||
167 | switch ($dbType) { |
||
168 | case 'number': |
||
169 | if ($tableColumn['data_precision'] == 20 && $tableColumn['data_scale'] == 0) { |
||
170 | $precision = 20; |
||
171 | $scale = 0; |
||
172 | $type = 'bigint'; |
||
173 | } elseif ($tableColumn['data_precision'] == 5 && $tableColumn['data_scale'] == 0) { |
||
174 | $type = 'smallint'; |
||
175 | $precision = 5; |
||
176 | $scale = 0; |
||
177 | } elseif ($tableColumn['data_precision'] == 1 && $tableColumn['data_scale'] == 0) { |
||
178 | $precision = 1; |
||
179 | $scale = 0; |
||
180 | $type = 'boolean'; |
||
181 | } elseif ($tableColumn['data_scale'] > 0) { |
||
182 | $precision = $tableColumn['data_precision']; |
||
183 | $scale = $tableColumn['data_scale']; |
||
184 | $type = 'decimal'; |
||
185 | } |
||
186 | $length = null; |
||
187 | break; |
||
188 | case 'pls_integer': |
||
189 | case 'binary_integer': |
||
190 | $length = null; |
||
191 | break; |
||
192 | case 'varchar': |
||
193 | case 'varchar2': |
||
194 | case 'nvarchar2': |
||
195 | $length = $tableColumn['char_length']; |
||
196 | $fixed = false; |
||
197 | break; |
||
198 | case 'char': |
||
199 | case 'nchar': |
||
200 | $length = $tableColumn['char_length']; |
||
201 | $fixed = true; |
||
202 | break; |
||
203 | case 'date': |
||
204 | case 'timestamp': |
||
205 | $length = null; |
||
206 | break; |
||
207 | case 'float': |
||
208 | case 'binary_float': |
||
209 | case 'binary_double': |
||
210 | $precision = $tableColumn['data_precision']; |
||
211 | $scale = $tableColumn['data_scale']; |
||
212 | $length = null; |
||
213 | break; |
||
214 | case 'clob': |
||
215 | case 'nclob': |
||
216 | $length = null; |
||
217 | break; |
||
218 | case 'blob': |
||
219 | case 'raw': |
||
220 | case 'long raw': |
||
221 | case 'bfile': |
||
222 | $length = null; |
||
223 | break; |
||
224 | case 'rowid': |
||
225 | case 'urowid': |
||
226 | default: |
||
227 | $length = null; |
||
228 | } |
||
229 | |||
230 | $options = [ |
||
231 | 'notnull' => (bool) ($tableColumn['nullable'] === 'N'), |
||
232 | 'fixed' => (bool) $fixed, |
||
233 | 'unsigned' => (bool) $unsigned, |
||
234 | 'default' => $tableColumn['data_default'], |
||
235 | 'length' => $length, |
||
236 | 'precision' => $precision, |
||
237 | 'scale' => $scale, |
||
238 | 'comment' => isset($tableColumn['comments']) && '' !== $tableColumn['comments'] |
||
239 | ? $tableColumn['comments'] |
||
240 | : null, |
||
241 | ]; |
||
242 | |||
243 | return new Column($this->getQuotedIdentifierName($tableColumn['column_name']), Type::getType($type), $options); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | protected function _getPortableTableForeignKeysList($tableForeignKeys) |
||
250 | { |
||
251 | $list = []; |
||
252 | foreach ($tableForeignKeys as $value) { |
||
253 | $value = \array_change_key_case($value, CASE_LOWER); |
||
254 | if (!isset($list[$value['constraint_name']])) { |
||
255 | if ($value['delete_rule'] == "NO ACTION") { |
||
256 | $value['delete_rule'] = null; |
||
257 | } |
||
258 | |||
259 | $list[$value['constraint_name']] = [ |
||
260 | 'name' => $this->getQuotedIdentifierName($value['constraint_name']), |
||
261 | 'local' => [], |
||
262 | 'foreign' => [], |
||
263 | 'foreignTable' => $value['references_table'], |
||
264 | 'onDelete' => $value['delete_rule'], |
||
265 | ]; |
||
266 | } |
||
267 | |||
268 | $localColumn = $this->getQuotedIdentifierName($value['local_column']); |
||
269 | $foreignColumn = $this->getQuotedIdentifierName($value['foreign_column']); |
||
270 | |||
271 | $list[$value['constraint_name']]['local'][$value['position']] = $localColumn; |
||
272 | $list[$value['constraint_name']]['foreign'][$value['position']] = $foreignColumn; |
||
273 | } |
||
274 | |||
275 | $result = []; |
||
276 | foreach ($list as $constraint) { |
||
277 | $result[] = new ForeignKeyConstraint( |
||
278 | array_values($constraint['local']), $this->getQuotedIdentifierName($constraint['foreignTable']), |
||
279 | array_values($constraint['foreign']), $this->getQuotedIdentifierName($constraint['name']), |
||
280 | ['onDelete' => $constraint['onDelete']] |
||
281 | ); |
||
282 | } |
||
283 | |||
284 | return $result; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * {@inheritdoc} |
||
289 | */ |
||
290 | protected function _getPortableSequenceDefinition($sequence) |
||
291 | { |
||
292 | $sequence = \array_change_key_case($sequence, CASE_LOWER); |
||
293 | |||
294 | return new Sequence( |
||
295 | $this->getQuotedIdentifierName($sequence['sequence_name']), |
||
296 | $sequence['increment_by'], |
||
297 | $sequence['min_value'] |
||
298 | ); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | protected function _getPortableFunctionDefinition($function) |
||
305 | { |
||
306 | $function = \array_change_key_case($function, CASE_LOWER); |
||
307 | |||
308 | return $function['name']; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | protected function _getPortableDatabaseDefinition($database) |
||
315 | { |
||
316 | $database = \array_change_key_case($database, CASE_LOWER); |
||
317 | |||
318 | return $database['username']; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * {@inheritdoc} |
||
323 | */ |
||
324 | public function createDatabase($database = null) |
||
325 | { |
||
326 | if (is_null($database)) { |
||
327 | $database = $this->_conn->getDatabase(); |
||
328 | } |
||
329 | |||
330 | $params = $this->_conn->getParams(); |
||
331 | $username = $database; |
||
332 | $password = $params['password']; |
||
333 | |||
334 | $query = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password; |
||
335 | $this->_conn->executeUpdate($query); |
||
336 | |||
337 | $query = 'GRANT DBA TO ' . $username; |
||
338 | $this->_conn->executeUpdate($query); |
||
339 | |||
340 | return true; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * @param string $table |
||
345 | * |
||
346 | * @return boolean |
||
347 | */ |
||
348 | public function dropAutoincrement($table) |
||
349 | { |
||
350 | $sql = $this->_platform->getDropAutoincrementSql($table); |
||
351 | foreach ($sql as $query) { |
||
352 | $this->_conn->executeUpdate($query); |
||
353 | } |
||
354 | |||
355 | return true; |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * {@inheritdoc} |
||
360 | */ |
||
361 | public function dropTable($name) |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Returns the quoted representation of the given identifier name. |
||
370 | * |
||
371 | * Quotes non-uppercase identifiers explicitly to preserve case |
||
372 | * and thus make references to the particular identifier work. |
||
373 | * |
||
374 | * @param string $identifier The identifier to quote. |
||
375 | * |
||
376 | * @return string The quoted identifier. |
||
377 | */ |
||
378 | private function getQuotedIdentifierName($identifier) |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Kills sessions connected with the given user. |
||
389 | * |
||
390 | * This is useful to force DROP USER operations which could fail because of active user sessions. |
||
391 | * |
||
392 | * @param string $user The name of the user to kill sessions for. |
||
393 | * |
||
394 | * @return void |
||
395 | */ |
||
396 | private function killUserSessions($user) |
||
420 | ) |
||
421 | ); |
||
422 | } |
||
425 |
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.