| Total Complexity | 83 |
| Total Lines | 438 |
| Duplicated Lines | 5.71 % |
| Coverage | 8.61% |
| 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 PostgreSqlSchemaManager 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 PostgreSqlSchemaManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class PostgreSqlSchemaManager extends AbstractSchemaManager |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $existingSchemaPaths; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Gets all the existing schema names. |
||
| 42 | * |
||
| 43 | * @return array |
||
| 44 | */ |
||
| 45 | public function getSchemaNames() |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Returns an array of schema search paths. |
||
| 54 | * |
||
| 55 | * This is a PostgreSQL only function. |
||
| 56 | * |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | public function getSchemaSearchPaths() |
||
| 60 | { |
||
| 61 | $params = $this->_conn->getParams(); |
||
| 62 | $schema = explode(",", $this->_conn->fetchColumn('SHOW search_path')); |
||
| 63 | |||
| 64 | if (isset($params['user'])) { |
||
| 65 | $schema = str_replace('"$user"', $params['user'], $schema); |
||
| 66 | } |
||
| 67 | |||
| 68 | return array_map('trim', $schema); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Gets names of all existing schemas in the current users search path. |
||
| 73 | * |
||
| 74 | * This is a PostgreSQL only function. |
||
| 75 | * |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | public function getExistingSchemaSearchPaths() |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Sets or resets the order of the existing schemas in the current search path of the user. |
||
| 89 | * |
||
| 90 | * This is a PostgreSQL only function. |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | public function determineExistingSchemaSearchPaths() |
||
| 101 | }); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | */ |
||
| 107 | public function dropDatabase($database) |
||
| 108 | { |
||
| 109 | try { |
||
| 110 | parent::dropDatabase($database); |
||
| 111 | } catch (DriverException $exception) { |
||
| 112 | // If we have a SQLSTATE 55006, the drop database operation failed |
||
| 113 | // because of active connections on the database. |
||
| 114 | // To force dropping the database, we first have to close all active connections |
||
| 115 | // on that database and issue the drop database operation again. |
||
| 116 | if ($exception->getSQLState() !== '55006') { |
||
| 117 | throw $exception; |
||
| 118 | } |
||
| 119 | |||
| 120 | $this->_execSql( |
||
| 121 | [ |
||
| 122 | $this->_platform->getDisallowDatabaseConnectionsSQL($database), |
||
|
|
|||
| 123 | $this->_platform->getCloseActiveDatabaseConnectionsSQL($database), |
||
| 124 | ] |
||
| 125 | ); |
||
| 126 | |||
| 127 | parent::dropDatabase($database); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | protected function _getPortableTableForeignKeyDefinition($tableForeignKey) |
||
| 135 | { |
||
| 136 | $onUpdate = null; |
||
| 137 | $onDelete = null; |
||
| 138 | |||
| 139 | if (preg_match('(ON UPDATE ([a-zA-Z0-9]+( (NULL|ACTION|DEFAULT))?))', $tableForeignKey['condef'], $match)) { |
||
| 140 | $onUpdate = $match[1]; |
||
| 141 | } |
||
| 142 | if (preg_match('(ON DELETE ([a-zA-Z0-9]+( (NULL|ACTION|DEFAULT))?))', $tableForeignKey['condef'], $match)) { |
||
| 143 | $onDelete = $match[1]; |
||
| 144 | } |
||
| 145 | |||
| 146 | if (preg_match('/FOREIGN KEY \((.+)\) REFERENCES (.+)\((.+)\)/', $tableForeignKey['condef'], $values)) { |
||
| 147 | // PostgreSQL returns identifiers that are keywords with quotes, we need them later, don't get |
||
| 148 | // the idea to trim them here. |
||
| 149 | $localColumns = array_map('trim', explode(",", $values[1])); |
||
| 150 | $foreignColumns = array_map('trim', explode(",", $values[3])); |
||
| 151 | $foreignTable = $values[2]; |
||
| 152 | } |
||
| 153 | |||
| 154 | return new ForeignKeyConstraint( |
||
| 155 | $localColumns, $foreignTable, $foreignColumns, $tableForeignKey['conname'], |
||
| 156 | ['onUpdate' => $onUpdate, 'onDelete' => $onDelete] |
||
| 157 | ); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | protected function _getPortableTriggerDefinition($trigger) |
||
| 164 | { |
||
| 165 | return $trigger['trigger_name']; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | protected function _getPortableViewDefinition($view) |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | */ |
||
| 179 | protected function _getPortableUserDefinition($user) |
||
| 184 | ]; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * {@inheritdoc} |
||
| 189 | */ |
||
| 190 | protected function _getPortableTableDefinition($table) |
||
| 191 | { |
||
| 192 | $schemas = $this->getExistingSchemaSearchPaths(); |
||
| 193 | $firstSchema = array_shift($schemas); |
||
| 194 | |||
| 195 | if ($table['schema_name'] == $firstSchema) { |
||
| 196 | return $table['table_name']; |
||
| 197 | } |
||
| 198 | |||
| 199 | return $table['schema_name'] . "." . $table['table_name']; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | * |
||
| 205 | * @license New BSD License |
||
| 206 | * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html |
||
| 207 | */ |
||
| 208 | protected function _getPortableTableIndexesList($tableIndexes, $tableName=null) |
||
| 209 | { |
||
| 210 | $buffer = []; |
||
| 211 | foreach ($tableIndexes as $row) { |
||
| 212 | $colNumbers = explode(' ', $row['indkey']); |
||
| 213 | $colNumbersSql = 'IN (' . join(' ,', $colNumbers) . ' )'; |
||
| 214 | $columnNameSql = "SELECT attnum, attname FROM pg_attribute |
||
| 215 | WHERE attrelid={$row['indrelid']} AND attnum $colNumbersSql ORDER BY attnum ASC;"; |
||
| 216 | |||
| 217 | $stmt = $this->_conn->executeQuery($columnNameSql); |
||
| 218 | $indexColumns = $stmt->fetchAll(); |
||
| 219 | |||
| 220 | // required for getting the order of the columns right. |
||
| 221 | foreach ($colNumbers as $colNum) { |
||
| 222 | foreach ($indexColumns as $colRow) { |
||
| 223 | if ($colNum == $colRow['attnum']) { |
||
| 224 | $buffer[] = [ |
||
| 225 | 'key_name' => $row['relname'], |
||
| 226 | 'column_name' => trim($colRow['attname']), |
||
| 227 | 'non_unique' => !$row['indisunique'], |
||
| 228 | 'primary' => $row['indisprimary'], |
||
| 229 | 'where' => $row['where'], |
||
| 230 | ]; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | return parent::_getPortableTableIndexesList($buffer, $tableName); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | protected function _getPortableDatabaseDefinition($database) |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritdoc} |
||
| 249 | */ |
||
| 250 | 2 | protected function _getPortableSequencesList($sequences) |
|
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | protected function getPortableNamespaceDefinition(array $namespace) |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritdoc} |
||
| 283 | */ |
||
| 284 | 2 | protected function _getPortableSequenceDefinition($sequence) |
|
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * {@inheritdoc} |
||
| 303 | */ |
||
| 304 | protected function _getPortableTableColumnDefinition($tableColumn) |
||
| 305 | { |
||
| 306 | $tableColumn = array_change_key_case($tableColumn, CASE_LOWER); |
||
| 307 | |||
| 308 | if (strtolower($tableColumn['type']) === 'varchar' || strtolower($tableColumn['type']) === 'bpchar') { |
||
| 309 | // get length from varchar definition |
||
| 310 | $length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $tableColumn['complete_type']); |
||
| 311 | $tableColumn['length'] = $length; |
||
| 312 | } |
||
| 313 | |||
| 314 | $matches = []; |
||
| 315 | |||
| 316 | $autoincrement = false; |
||
| 317 | if (preg_match("/^nextval\('(.*)'(::.*)?\)$/", $tableColumn['default'], $matches)) { |
||
| 318 | $tableColumn['sequence'] = $matches[1]; |
||
| 319 | $tableColumn['default'] = null; |
||
| 320 | $autoincrement = true; |
||
| 321 | } |
||
| 322 | |||
| 323 | if (preg_match("/^['(](.*)[')]::.*$/", $tableColumn['default'], $matches)) { |
||
| 324 | $tableColumn['default'] = $matches[1]; |
||
| 325 | } |
||
| 326 | |||
| 327 | if (stripos($tableColumn['default'], 'NULL') === 0) { |
||
| 328 | $tableColumn['default'] = null; |
||
| 329 | } |
||
| 330 | |||
| 331 | $length = (isset($tableColumn['length'])) ? $tableColumn['length'] : null; |
||
| 332 | if ($length == '-1' && isset($tableColumn['atttypmod'])) { |
||
| 333 | $length = $tableColumn['atttypmod'] - 4; |
||
| 334 | } |
||
| 335 | if ((int) $length <= 0) { |
||
| 336 | $length = null; |
||
| 337 | } |
||
| 338 | $fixed = null; |
||
| 339 | |||
| 340 | if (!isset($tableColumn['name'])) { |
||
| 341 | $tableColumn['name'] = ''; |
||
| 342 | } |
||
| 343 | |||
| 344 | $precision = null; |
||
| 345 | $scale = null; |
||
| 346 | $jsonb = null; |
||
| 347 | |||
| 348 | $dbType = strtolower($tableColumn['type']); |
||
| 349 | if (strlen($tableColumn['domain_type']) && !$this->_platform->hasDoctrineTypeMappingFor($tableColumn['type'])) { |
||
| 350 | $dbType = strtolower($tableColumn['domain_type']); |
||
| 351 | $tableColumn['complete_type'] = $tableColumn['domain_complete_type']; |
||
| 352 | } |
||
| 353 | |||
| 354 | $type = $this->_platform->getDoctrineTypeMapping($dbType); |
||
| 355 | $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type); |
||
| 356 | $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type); |
||
| 357 | |||
| 358 | switch ($dbType) { |
||
| 359 | case 'smallint': |
||
| 360 | View Code Duplication | case 'int2': |
|
| 361 | $tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']); |
||
| 362 | $length = null; |
||
| 363 | break; |
||
| 364 | case 'int': |
||
| 365 | case 'int4': |
||
| 366 | View Code Duplication | case 'integer': |
|
| 367 | $tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']); |
||
| 368 | $length = null; |
||
| 369 | break; |
||
| 370 | case 'bigint': |
||
| 371 | View Code Duplication | case 'int8': |
|
| 372 | $tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']); |
||
| 373 | $length = null; |
||
| 374 | break; |
||
| 375 | case 'bool': |
||
| 376 | case 'boolean': |
||
| 377 | if ($tableColumn['default'] === 'true') { |
||
| 378 | $tableColumn['default'] = true; |
||
| 379 | } |
||
| 380 | |||
| 381 | if ($tableColumn['default'] === 'false') { |
||
| 382 | $tableColumn['default'] = false; |
||
| 383 | } |
||
| 384 | |||
| 385 | $length = null; |
||
| 386 | break; |
||
| 387 | case 'text': |
||
| 388 | $fixed = false; |
||
| 389 | break; |
||
| 390 | case 'varchar': |
||
| 391 | case 'interval': |
||
| 392 | case '_varchar': |
||
| 393 | $fixed = false; |
||
| 394 | break; |
||
| 395 | case 'char': |
||
| 396 | case 'bpchar': |
||
| 397 | $fixed = true; |
||
| 398 | break; |
||
| 399 | case 'float': |
||
| 400 | case 'float4': |
||
| 401 | case 'float8': |
||
| 402 | case 'double': |
||
| 403 | case 'double precision': |
||
| 404 | case 'real': |
||
| 405 | case 'decimal': |
||
| 406 | case 'money': |
||
| 407 | case 'numeric': |
||
| 408 | $tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']); |
||
| 409 | |||
| 410 | if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['complete_type'], $match)) { |
||
| 411 | $precision = $match[1]; |
||
| 412 | $scale = $match[2]; |
||
| 413 | $length = null; |
||
| 414 | } |
||
| 415 | break; |
||
| 416 | case 'year': |
||
| 417 | $length = null; |
||
| 418 | break; |
||
| 419 | |||
| 420 | // PostgreSQL 9.4+ only |
||
| 421 | case 'jsonb': |
||
| 422 | $jsonb = true; |
||
| 423 | break; |
||
| 424 | } |
||
| 425 | |||
| 426 | if ($tableColumn['default'] && preg_match("('([^']+)'::)", $tableColumn['default'], $match)) { |
||
| 427 | $tableColumn['default'] = $match[1]; |
||
| 428 | } |
||
| 429 | |||
| 430 | $options = [ |
||
| 431 | 'length' => $length, |
||
| 432 | 'notnull' => (bool) $tableColumn['isnotnull'], |
||
| 433 | 'default' => $tableColumn['default'], |
||
| 434 | 'precision' => $precision, |
||
| 435 | 'scale' => $scale, |
||
| 436 | 'fixed' => $fixed, |
||
| 437 | 'unsigned' => false, |
||
| 438 | 'autoincrement' => $autoincrement, |
||
| 439 | 'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== '' |
||
| 440 | ? $tableColumn['comment'] |
||
| 441 | : null, |
||
| 442 | ]; |
||
| 443 | |||
| 444 | $column = new Column($tableColumn['field'], Type::getType($type), $options); |
||
| 445 | |||
| 446 | View Code Duplication | if (isset($tableColumn['collation']) && !empty($tableColumn['collation'])) { |
|
| 447 | $column->setPlatformOption('collation', $tableColumn['collation']); |
||
| 448 | } |
||
| 449 | |||
| 450 | if (in_array($column->getType()->getName(), [Type::JSON_ARRAY, Type::JSON], true)) { |
||
| 451 | $column->setPlatformOption('jsonb', $jsonb); |
||
| 452 | } |
||
| 453 | |||
| 454 | return $column; |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * PostgreSQL 9.4 puts parentheses around negative numeric default values that need to be stripped eventually. |
||
| 459 | * |
||
| 460 | * @param mixed $defaultValue |
||
| 461 | * |
||
| 462 | * @return mixed |
||
| 463 | */ |
||
| 464 | private function fixVersion94NegativeNumericDefaultValue($defaultValue) |
||
| 471 | } |
||
| 472 | } |
||
| 473 |