Code Duplication    Length = 77-77 lines in 2 locations

typo3/sysext/core/Classes/Database/Schema/ConnectionMigrator.php 2 locations

@@ 389-465 (lines=77) @@
386
     * @throws \Doctrine\DBAL\Schema\SchemaException
387
     * @throws \InvalidArgumentException
388
     */
389
    protected function getNewFieldUpdateSuggestions(SchemaDiff $schemaDiff): array
390
    {
391
        $changedTables = [];
392
393
        foreach ($schemaDiff->changedTables as $index => $changedTable) {
394
            $fromTable = $this->buildQuotedTable($schemaDiff->fromSchema->getTable($changedTable->name));
395
396
            if (count($changedTable->addedColumns) !== 0) {
397
                // Treat each added column with a new diff to get a dedicated suggestions
398
                // just for this single column.
399
                foreach ($changedTable->addedColumns as $addedColumn) {
400
                    $changedTables[$index . ':tbl_' . $addedColumn->getName()] = GeneralUtility::makeInstance(
401
                        TableDiff::class,
402
                        $changedTable->name,
403
                        [$addedColumn],
404
                        [],
405
                        [],
406
                        [],
407
                        [],
408
                        [],
409
                        $fromTable
410
                    );
411
                }
412
            }
413
414
            if (count($changedTable->addedIndexes) !== 0) {
415
                // Treat each added index with a new diff to get a dedicated suggestions
416
                // just for this index.
417
                foreach ($changedTable->addedIndexes as $addedIndex) {
418
                    $changedTables[$index . ':idx_' . $addedIndex->getName()] = GeneralUtility::makeInstance(
419
                        TableDiff::class,
420
                        $changedTable->name,
421
                        [],
422
                        [],
423
                        [],
424
                        [$this->buildQuotedIndex($addedIndex)],
425
                        [],
426
                        [],
427
                        $fromTable
428
                    );
429
                }
430
            }
431
432
            if (count($changedTable->addedForeignKeys) !== 0) {
433
                // Treat each added foreign key with a new diff to get a dedicated suggestions
434
                // just for this foreign key.
435
                foreach ($changedTable->addedForeignKeys as $addedForeignKey) {
436
                    $fkIndex = $index . ':fk_' . $addedForeignKey->getName();
437
                    $changedTables[$fkIndex] = GeneralUtility::makeInstance(
438
                        TableDiff::class,
439
                        $changedTable->name,
440
                        [],
441
                        [],
442
                        [],
443
                        [],
444
                        [],
445
                        [],
446
                        $fromTable
447
                    );
448
                    $changedTables[$fkIndex]->addedForeignKeys = [$this->buildQuotedForeignKey($addedForeignKey)];
449
                }
450
            }
451
        }
452
453
        // Build a new schema diff that only contains added fields
454
        $addFieldSchemaDiff = GeneralUtility::makeInstance(
455
            SchemaDiff::class,
456
            [],
457
            $changedTables,
458
            [],
459
            $schemaDiff->fromSchema
460
        );
461
462
        $statements = $addFieldSchemaDiff->toSql($this->connection->getDatabasePlatform());
463
464
        return ['add' => $this->calculateUpdateSuggestionsHashes($statements)];
465
    }
466
467
    /**
468
     * Extract update suggestions (SQL statements) for changed options
@@ 796-872 (lines=77) @@
793
     * @throws \Doctrine\DBAL\Schema\SchemaException
794
     * @throws \InvalidArgumentException
795
     */
796
    protected function getDropFieldUpdateSuggestions(SchemaDiff $schemaDiff): array
797
    {
798
        $changedTables = [];
799
800
        foreach ($schemaDiff->changedTables as $index => $changedTable) {
801
            $fromTable = $this->buildQuotedTable($schemaDiff->fromSchema->getTable($changedTable->name));
802
803
            if (count($changedTable->removedColumns) !== 0) {
804
                // Treat each changed column with a new diff to get a dedicated suggestions
805
                // just for this single column.
806
                foreach ($changedTable->removedColumns as $removedColumn) {
807
                    $changedTables[$index . ':tbl_' . $removedColumn->getName()] = GeneralUtility::makeInstance(
808
                        TableDiff::class,
809
                        $changedTable->name,
810
                        [],
811
                        [],
812
                        [$this->buildQuotedColumn($removedColumn)],
813
                        [],
814
                        [],
815
                        [],
816
                        $fromTable
817
                    );
818
                }
819
            }
820
821
            if (count($changedTable->removedIndexes) !== 0) {
822
                // Treat each removed index with a new diff to get a dedicated suggestions
823
                // just for this index.
824
                foreach ($changedTable->removedIndexes as $removedIndex) {
825
                    $changedTables[$index . ':idx_' . $removedIndex->getName()] = GeneralUtility::makeInstance(
826
                        TableDiff::class,
827
                        $changedTable->name,
828
                        [],
829
                        [],
830
                        [],
831
                        [],
832
                        [],
833
                        [$this->buildQuotedIndex($removedIndex)],
834
                        $fromTable
835
                    );
836
                }
837
            }
838
839
            if (count($changedTable->removedForeignKeys) !== 0) {
840
                // Treat each removed foreign key with a new diff to get a dedicated suggestions
841
                // just for this foreign key.
842
                foreach ($changedTable->removedForeignKeys as $removedForeignKey) {
843
                    $fkIndex = $index . ':fk_' . $removedForeignKey->getName();
844
                    $changedTables[$fkIndex] = GeneralUtility::makeInstance(
845
                        TableDiff::class,
846
                        $changedTable->name,
847
                        [],
848
                        [],
849
                        [],
850
                        [],
851
                        [],
852
                        [],
853
                        $fromTable
854
                    );
855
                    $changedTables[$fkIndex]->removedForeignKeys = [$this->buildQuotedForeignKey($removedForeignKey)];
856
                }
857
            }
858
        }
859
860
        // Build a new schema diff that only contains removable fields
861
        $removedFieldDiff = GeneralUtility::makeInstance(
862
            SchemaDiff::class,
863
            [],
864
            $changedTables,
865
            [],
866
            $schemaDiff->fromSchema
867
        );
868
869
        $statements = $removedFieldDiff->toSql($this->connection->getDatabasePlatform());
870
871
        return ['drop' => $this->calculateUpdateSuggestionsHashes($statements)];
872
    }
873
874
    /**
875
     * Extract update suggestions (SQL statements) for tables that can