| Total Complexity | 118 |
| Total Lines | 513 |
| Duplicated Lines | 11.11 % |
| Coverage | 98.21% |
| 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 Comparator 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 Comparator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Comparator |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @param \Doctrine\DBAL\Schema\Schema $fromSchema |
||
| 35 | * @param \Doctrine\DBAL\Schema\Schema $toSchema |
||
| 36 | * |
||
| 37 | * @return \Doctrine\DBAL\Schema\SchemaDiff |
||
| 38 | */ |
||
| 39 | 17 | public static function compareSchemas(Schema $fromSchema, Schema $toSchema) |
|
| 40 | { |
||
| 41 | 17 | $c = new self(); |
|
| 42 | |||
| 43 | 17 | return $c->compare($fromSchema, $toSchema); |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema. |
||
| 48 | * |
||
| 49 | * The returned differences are returned in such a way that they contain the |
||
| 50 | * operations to change the schema stored in $fromSchema to the schema that is |
||
| 51 | * stored in $toSchema. |
||
| 52 | * |
||
| 53 | * @param \Doctrine\DBAL\Schema\Schema $fromSchema |
||
| 54 | * @param \Doctrine\DBAL\Schema\Schema $toSchema |
||
| 55 | * |
||
| 56 | * @return \Doctrine\DBAL\Schema\SchemaDiff |
||
| 57 | */ |
||
| 58 | 27 | public function compare(Schema $fromSchema, Schema $toSchema) |
|
| 59 | { |
||
| 60 | 27 | $diff = new SchemaDiff(); |
|
| 61 | 27 | $diff->fromSchema = $fromSchema; |
|
| 62 | |||
| 63 | 27 | $foreignKeysToTable = []; |
|
| 64 | |||
| 65 | 27 | foreach ($toSchema->getNamespaces() as $namespace) { |
|
| 66 | 2 | if ( ! $fromSchema->hasNamespace($namespace)) { |
|
| 67 | 2 | $diff->newNamespaces[$namespace] = $namespace; |
|
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | 27 | foreach ($fromSchema->getNamespaces() as $namespace) { |
|
| 72 | 2 | if ( ! $toSchema->hasNamespace($namespace)) { |
|
| 73 | 2 | $diff->removedNamespaces[$namespace] = $namespace; |
|
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | 27 | foreach ($toSchema->getTables() as $table) { |
|
| 78 | 21 | $tableName = $table->getShortestName($toSchema->getName()); |
|
| 79 | 21 | if ( ! $fromSchema->hasTable($tableName)) { |
|
| 80 | 5 | $diff->newTables[$tableName] = $toSchema->getTable($tableName); |
|
| 81 | } else { |
||
| 82 | 19 | $tableDifferences = $this->diffTable($fromSchema->getTable($tableName), $toSchema->getTable($tableName)); |
|
| 83 | 19 | if ($tableDifferences !== false) { |
|
| 84 | 21 | $diff->changedTables[$tableName] = $tableDifferences; |
|
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /* Check if there are tables removed */ |
||
| 90 | 27 | foreach ($fromSchema->getTables() as $table) { |
|
| 91 | 20 | $tableName = $table->getShortestName($fromSchema->getName()); |
|
| 92 | |||
| 93 | 20 | $table = $fromSchema->getTable($tableName); |
|
| 94 | 20 | if ( ! $toSchema->hasTable($tableName)) { |
|
| 95 | 5 | $diff->removedTables[$tableName] = $table; |
|
| 96 | } |
||
| 97 | |||
| 98 | // also remember all foreign keys that point to a specific table |
||
| 99 | 20 | foreach ($table->getForeignKeys() as $foreignKey) { |
|
| 100 | 2 | $foreignTable = strtolower($foreignKey->getForeignTableName()); |
|
| 101 | 2 | if (!isset($foreignKeysToTable[$foreignTable])) { |
|
| 102 | 2 | $foreignKeysToTable[$foreignTable] = []; |
|
| 103 | } |
||
| 104 | 20 | $foreignKeysToTable[$foreignTable][] = $foreignKey; |
|
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | 27 | foreach ($diff->removedTables as $tableName => $table) { |
|
| 109 | 5 | if (isset($foreignKeysToTable[$tableName])) { |
|
| 110 | 2 | $diff->orphanedForeignKeys = array_merge($diff->orphanedForeignKeys, $foreignKeysToTable[$tableName]); |
|
| 111 | |||
| 112 | // deleting duplicated foreign keys present on both on the orphanedForeignKey |
||
| 113 | // and the removedForeignKeys from changedTables |
||
| 114 | 2 | foreach ($foreignKeysToTable[$tableName] as $foreignKey) { |
|
| 115 | // strtolower the table name to make if compatible with getShortestName |
||
| 116 | 2 | $localTableName = strtolower($foreignKey->getLocalTableName()); |
|
| 117 | 2 | if (isset($diff->changedTables[$localTableName])) { |
|
| 118 | 2 | foreach ($diff->changedTables[$localTableName]->removedForeignKeys as $key => $removedForeignKey) { |
|
| 119 | // We check if the key is from the removed table if not we skip. |
||
| 120 | 2 | if ($tableName !== strtolower($removedForeignKey->getForeignTableName())) { |
|
| 121 | 1 | continue; |
|
| 122 | } |
||
| 123 | 5 | unset($diff->changedTables[$localTableName]->removedForeignKeys[$key]); |
|
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | 27 | foreach ($toSchema->getSequences() as $sequence) { |
|
| 131 | 4 | $sequenceName = $sequence->getShortestName($toSchema->getName()); |
|
| 132 | 4 | if ( ! $fromSchema->hasSequence($sequenceName)) { |
|
| 133 | 3 | if ( ! $this->isAutoIncrementSequenceInSchema($fromSchema, $sequence)) { |
|
| 134 | 3 | $diff->newSequences[] = $sequence; |
|
| 135 | } |
||
| 136 | } else { |
||
| 137 | 2 | if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) { |
|
| 138 | 4 | $diff->changedSequences[] = $toSchema->getSequence($sequenceName); |
|
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | 27 | foreach ($fromSchema->getSequences() as $sequence) { |
|
| 144 | 4 | if ($this->isAutoIncrementSequenceInSchema($toSchema, $sequence)) { |
|
| 145 | 1 | continue; |
|
| 146 | } |
||
| 147 | |||
| 148 | 3 | $sequenceName = $sequence->getShortestName($fromSchema->getName()); |
|
| 149 | |||
| 150 | 3 | if ( ! $toSchema->hasSequence($sequenceName)) { |
|
| 151 | 3 | $diff->removedSequences[] = $sequence; |
|
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | 27 | return $diff; |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param \Doctrine\DBAL\Schema\Schema $schema |
||
| 160 | * @param \Doctrine\DBAL\Schema\Sequence $sequence |
||
| 161 | * |
||
| 162 | * @return boolean |
||
| 163 | */ |
||
| 164 | 6 | private function isAutoIncrementSequenceInSchema($schema, $sequence) |
|
| 165 | { |
||
| 166 | 6 | foreach ($schema->getTables() as $table) { |
|
| 167 | 2 | if ($sequence->isAutoIncrementsFor($table)) { |
|
| 168 | 2 | return true; |
|
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | 4 | return false; |
|
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param \Doctrine\DBAL\Schema\Sequence $sequence1 |
||
| 177 | * @param \Doctrine\DBAL\Schema\Sequence $sequence2 |
||
| 178 | * |
||
| 179 | * @return boolean |
||
| 180 | */ |
||
| 181 | 3 | public function diffSequence(Sequence $sequence1, Sequence $sequence2) |
|
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Returns the difference between the tables $table1 and $table2. |
||
| 196 | * |
||
| 197 | * If there are no differences this method returns the boolean false. |
||
| 198 | * |
||
| 199 | * @param \Doctrine\DBAL\Schema\Table $table1 |
||
| 200 | * @param \Doctrine\DBAL\Schema\Table $table2 |
||
| 201 | * |
||
| 202 | * @return boolean|\Doctrine\DBAL\Schema\TableDiff |
||
| 203 | */ |
||
| 204 | 123 | public function diffTable(Table $table1, Table $table2) |
|
| 205 | { |
||
| 206 | 123 | $changes = 0; |
|
| 207 | 123 | $tableDifferences = new TableDiff($table1->getName()); |
|
| 208 | 123 | $tableDifferences->fromTable = $table1; |
|
| 209 | |||
| 210 | 123 | $table1Columns = $table1->getColumns(); |
|
| 211 | 123 | $table2Columns = $table2->getColumns(); |
|
| 212 | |||
| 213 | /* See if all the fields in table 1 exist in table 2 */ |
||
| 214 | 123 | foreach ($table2Columns as $columnName => $column) { |
|
| 215 | 118 | if ( !$table1->hasColumn($columnName)) { |
|
| 216 | 29 | $tableDifferences->addedColumns[$columnName] = $column; |
|
| 217 | 118 | $changes++; |
|
| 218 | } |
||
| 219 | } |
||
| 220 | /* See if there are any removed fields in table 2 */ |
||
| 221 | 123 | foreach ($table1Columns as $columnName => $column) { |
|
| 222 | // See if column is removed in table 2. |
||
| 223 | 118 | if ( ! $table2->hasColumn($columnName)) { |
|
| 224 | 28 | $tableDifferences->removedColumns[$columnName] = $column; |
|
| 225 | 28 | $changes++; |
|
| 226 | 28 | continue; |
|
| 227 | } |
||
| 228 | |||
| 229 | // See if column has changed properties in table 2. |
||
| 230 | 98 | $changedProperties = $this->diffColumn($column, $table2->getColumn($columnName)); |
|
| 231 | |||
| 232 | 98 | if ( ! empty($changedProperties)) { |
|
| 233 | 42 | $columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties); |
|
| 234 | 42 | $columnDiff->fromColumn = $column; |
|
| 235 | 42 | $tableDifferences->changedColumns[$column->getName()] = $columnDiff; |
|
| 236 | 98 | $changes++; |
|
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | 123 | $this->detectColumnRenamings($tableDifferences); |
|
| 241 | |||
| 242 | 123 | $table1Indexes = $table1->getIndexes(); |
|
| 243 | 123 | $table2Indexes = $table2->getIndexes(); |
|
| 244 | |||
| 245 | /* See if all the indexes in table 1 exist in table 2 */ |
||
| 246 | 123 | foreach ($table2Indexes as $indexName => $index) { |
|
| 247 | 50 | if (($index->isPrimary() && $table1->hasPrimaryKey()) || $table1->hasIndex($indexName)) { |
|
| 248 | 34 | continue; |
|
| 249 | } |
||
| 250 | |||
| 251 | 16 | $tableDifferences->addedIndexes[$indexName] = $index; |
|
| 252 | 16 | $changes++; |
|
| 253 | } |
||
| 254 | /* See if there are any removed indexes in table 2 */ |
||
| 255 | 123 | foreach ($table1Indexes as $indexName => $index) { |
|
| 256 | // See if index is removed in table 2. |
||
| 257 | 47 | if (($index->isPrimary() && ! $table2->hasPrimaryKey()) || |
|
| 258 | 47 | ! $index->isPrimary() && ! $table2->hasIndex($indexName) |
|
| 259 | ) { |
||
| 260 | 17 | $tableDifferences->removedIndexes[$indexName] = $index; |
|
| 261 | 17 | $changes++; |
|
| 262 | 17 | continue; |
|
| 263 | } |
||
| 264 | |||
| 265 | // See if index has changed in table 2. |
||
| 266 | 34 | $table2Index = $index->isPrimary() ? $table2->getPrimaryKey() : $table2->getIndex($indexName); |
|
| 267 | |||
| 268 | 34 | if ($this->diffIndex($index, $table2Index)) { |
|
|
|
|||
| 269 | 15 | $tableDifferences->changedIndexes[$indexName] = $table2Index; |
|
| 270 | 34 | $changes++; |
|
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | 123 | $this->detectIndexRenamings($tableDifferences); |
|
| 275 | |||
| 276 | 123 | $fromFkeys = $table1->getForeignKeys(); |
|
| 277 | 123 | $toFkeys = $table2->getForeignKeys(); |
|
| 278 | |||
| 279 | 123 | foreach ($fromFkeys as $key1 => $constraint1) { |
|
| 280 | 11 | foreach ($toFkeys as $key2 => $constraint2) { |
|
| 281 | 5 | if ($this->diffForeignKey($constraint1, $constraint2) === false) { |
|
| 282 | 2 | if (strtolower($constraint1->getName()) !== strtolower($constraint2->getName())) { |
|
| 283 | 2 | $tableDifferences->renamedForeignKeys[$constraint1->getName()] = $constraint2; |
|
| 284 | 2 | $changes++; |
|
| 285 | } |
||
| 286 | 2 | unset($fromFkeys[$key1]); |
|
| 287 | 2 | unset($toFkeys[$key2]); |
|
| 288 | } else { |
||
| 289 | 3 | if (strtolower($constraint1->getName()) == strtolower($constraint2->getName())) { |
|
| 290 | 2 | $tableDifferences->changedForeignKeys[] = $constraint2; |
|
| 291 | 2 | $changes++; |
|
| 292 | 2 | unset($fromFkeys[$key1]); |
|
| 293 | 11 | unset($toFkeys[$key2]); |
|
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | 123 | foreach ($fromFkeys as $constraint1) { |
|
| 300 | 7 | $tableDifferences->removedForeignKeys[] = $constraint1; |
|
| 301 | 7 | $changes++; |
|
| 302 | } |
||
| 303 | |||
| 304 | 123 | foreach ($toFkeys as $constraint2) { |
|
| 305 | 2 | $tableDifferences->addedForeignKeys[] = $constraint2; |
|
| 306 | 2 | $changes++; |
|
| 307 | } |
||
| 308 | |||
| 309 | 123 | return $changes ? $tableDifferences : false; |
|
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Try to find columns that only changed their name, rename operations maybe cheaper than add/drop |
||
| 314 | * however ambiguities between different possibilities should not lead to renaming at all. |
||
| 315 | * |
||
| 316 | * @param \Doctrine\DBAL\Schema\TableDiff $tableDifferences |
||
| 317 | * |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | 123 | View Code Duplication | private function detectColumnRenamings(TableDiff $tableDifferences) |
| 321 | { |
||
| 322 | 123 | $renameCandidates = []; |
|
| 323 | 123 | foreach ($tableDifferences->addedColumns as $addedColumnName => $addedColumn) { |
|
| 324 | 29 | foreach ($tableDifferences->removedColumns as $removedColumn) { |
|
| 325 | 22 | if (count($this->diffColumn($addedColumn, $removedColumn)) == 0) { |
|
| 326 | 29 | $renameCandidates[$addedColumn->getName()][] = [$removedColumn, $addedColumn, $addedColumnName]; |
|
| 327 | } |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | 123 | foreach ($renameCandidates as $candidateColumns) { |
|
| 332 | 22 | if (count($candidateColumns) == 1) { |
|
| 333 | 21 | list($removedColumn, $addedColumn) = $candidateColumns[0]; |
|
| 334 | 21 | $removedColumnName = strtolower($removedColumn->getName()); |
|
| 335 | 21 | $addedColumnName = strtolower($addedColumn->getName()); |
|
| 336 | |||
| 337 | 21 | if ( ! isset($tableDifferences->renamedColumns[$removedColumnName])) { |
|
| 338 | 21 | $tableDifferences->renamedColumns[$removedColumnName] = $addedColumn; |
|
| 339 | 21 | unset($tableDifferences->addedColumns[$addedColumnName]); |
|
| 340 | 22 | unset($tableDifferences->removedColumns[$removedColumnName]); |
|
| 341 | } |
||
| 342 | } |
||
| 343 | } |
||
| 344 | 123 | } |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Try to find indexes that only changed their name, rename operations maybe cheaper than add/drop |
||
| 348 | * however ambiguities between different possibilities should not lead to renaming at all. |
||
| 349 | * |
||
| 350 | * @param \Doctrine\DBAL\Schema\TableDiff $tableDifferences |
||
| 351 | * |
||
| 352 | * @return void |
||
| 353 | */ |
||
| 354 | 123 | View Code Duplication | private function detectIndexRenamings(TableDiff $tableDifferences) |
| 382 | } |
||
| 383 | } |
||
| 384 | } |
||
| 385 | 123 | } |
|
| 386 | |||
| 387 | /** |
||
| 388 | * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $key1 |
||
| 389 | * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $key2 |
||
| 390 | * |
||
| 391 | * @return boolean |
||
| 392 | */ |
||
| 393 | 8 | public function diffForeignKey(ForeignKeyConstraint $key1, ForeignKeyConstraint $key2) |
|
| 394 | { |
||
| 395 | 8 | if (array_map('strtolower', $key1->getUnquotedLocalColumns()) != array_map('strtolower', $key2->getUnquotedLocalColumns())) { |
|
| 396 | 1 | return true; |
|
| 397 | } |
||
| 398 | |||
| 399 | 7 | if (array_map('strtolower', $key1->getUnquotedForeignColumns()) != array_map('strtolower', $key2->getUnquotedForeignColumns())) { |
|
| 400 | return true; |
||
| 401 | } |
||
| 402 | |||
| 403 | 7 | if ($key1->getUnqualifiedForeignTableName() !== $key2->getUnqualifiedForeignTableName()) { |
|
| 404 | 1 | return true; |
|
| 405 | } |
||
| 406 | |||
| 407 | 6 | if ($key1->onUpdate() != $key2->onUpdate()) { |
|
| 408 | 1 | return true; |
|
| 409 | } |
||
| 410 | |||
| 411 | 5 | if ($key1->onDelete() != $key2->onDelete()) { |
|
| 412 | return true; |
||
| 413 | } |
||
| 414 | |||
| 415 | 5 | return false; |
|
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Returns the difference between the fields $field1 and $field2. |
||
| 420 | * |
||
| 421 | * If there are differences this method returns $field2, otherwise the |
||
| 422 | * boolean false. |
||
| 423 | * |
||
| 424 | * @param \Doctrine\DBAL\Schema\Column $column1 |
||
| 425 | * @param \Doctrine\DBAL\Schema\Column $column2 |
||
| 426 | * |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | 140 | public function diffColumn(Column $column1, Column $column2) |
|
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * TODO: kill with fire on v3.0 |
||
| 513 | * |
||
| 514 | * @deprecated |
||
| 515 | */ |
||
| 516 | 140 | private function isALegacyJsonComparison(Types\Type $one, Types\Type $other) : bool |
|
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Finds the difference between the indexes $index1 and $index2. |
||
| 528 | * |
||
| 529 | * Compares $index1 with $index2 and returns $index2 if there are any |
||
| 530 | * differences or false in case there are no differences. |
||
| 531 | * |
||
| 532 | * @param \Doctrine\DBAL\Schema\Index $index1 |
||
| 533 | * @param \Doctrine\DBAL\Schema\Index $index2 |
||
| 534 | * |
||
| 535 | * @return boolean |
||
| 536 | */ |
||
| 537 | 41 | public function diffIndex(Index $index1, Index $index2) |
|
| 544 | } |
||
| 545 | } |
||
| 546 |