RenameExtension::addUniqueIndex()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 20
Ratio 100 %
Metric Value
dl 20
loc 20
rs 9.4285
cc 2
eloc 14
nc 2
nop 5
1
<?php
2
3
namespace RDV\Bundle\MigrationBundle\Migration\Extension;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
7
use Doctrine\DBAL\Schema\Index;
8
use Doctrine\DBAL\Schema\Schema;
9
use Doctrine\DBAL\Schema\Table;
10
use Doctrine\DBAL\Schema\TableDiff;
11
use RDV\Bundle\MigrationBundle\Migration\QueryBag;
12
use RDV\Bundle\MigrationBundle\Migration\Schema\Column;
13
use RDV\Bundle\MigrationBundle\Migration\SqlMigrationQuery;
14
use RDV\Bundle\MigrationBundle\Tools\DbIdentifierNameGenerator;
15
16
class RenameExtension implements DatabasePlatformAwareInterface, NameGeneratorAwareInterface
17
{
18
    /**
19
     * @var AbstractPlatform
20
     */
21
    protected $platform;
22
23
    /**
24
     * @var DbIdentifierNameGenerator
25
     */
26
    protected $nameGenerator;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function setDatabasePlatform(AbstractPlatform $platform)
32
    {
33
        $this->platform = $platform;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function setNameGenerator(DbIdentifierNameGenerator $nameGenerator)
40
    {
41
        $this->nameGenerator = $nameGenerator;
42
    }
43
44
    /**
45
     * Renames a table
46
     *
47
     * @param Schema   $schema
48
     * @param QueryBag $queries
49
     * @param string   $oldTableName
50
     * @param string   $newTableName
51
     */
52
    public function renameTable(Schema $schema, QueryBag $queries, $oldTableName, $newTableName)
53
    {
54
        $table         = $schema->getTable($oldTableName);
55
        $diff          = new TableDiff($table->getName());
56
        $diff->newName = $newTableName;
57
58
        $renameQuery = new SqlMigrationQuery(
59
            $this->platform->getAlterTableSQL($diff)
60
        );
61
62
        $queries->addQuery($renameQuery);
63
    }
64
65
    /**
66
     * Renames a column
67
     *
68
     * @param Schema   $schema
69
     * @param QueryBag $queries
70
     * @param Table    $table
71
     * @param string   $oldColumnName
72
     * @param string   $newColumnName
73
     */
74
    public function renameColumn(Schema $schema, QueryBag $queries, Table $table, $oldColumnName, $newColumnName)
0 ignored issues
show
Unused Code introduced by
The parameter $schema is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        $column = new Column(['column' => $table->getColumn($oldColumnName)]);
77
        $column->changeName($newColumnName);
78
        $diff                 = new TableDiff($table->getName());
79
        $diff->renamedColumns = [$oldColumnName => $column];
0 ignored issues
show
Documentation Bug introduced by
It seems like array($oldColumnName => $column) of type array<string,object<RDV\...gration\Schema\Column>> is incompatible with the declared type array<integer,object<Doc...ne\DBAL\Schema\Column>> of property $renamedColumns.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
81
        $renameQuery = new SqlMigrationQuery(
82
            $this->platform->getAlterTableSQL($diff)
83
        );
84
85
        $queries->addQuery($renameQuery);
86
    }
87
88
    /**
89
     * Create an index without check of table and columns existence.
90
     * This method can be helpful when you need to create an index for renamed table or column
91
     *
92
     * @param Schema      $schema
93
     * @param QueryBag    $queries
94
     * @param string      $tableName
95
     * @param string[]    $columnNames
96
     * @param string|null $indexName
97
     */
98 View Code Duplication
    public function addIndex(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
99
        Schema $schema,
0 ignored issues
show
Unused Code introduced by
The parameter $schema is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
        QueryBag $queries,
101
        $tableName,
102
        array $columnNames,
103
        $indexName = null
104
    ) {
105
        if (!$indexName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $indexName of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
106
            $indexName = $this->nameGenerator->generateIndexName($tableName, $columnNames);
107
        }
108
        $index              = new Index($indexName, $columnNames);
109
        $diff               = new TableDiff($tableName);
110
        $diff->addedIndexes = [$indexName => $index];
0 ignored issues
show
Documentation Bug introduced by
It seems like array($indexName => $index) of type array<string,object<Doctrine\DBAL\Schema\Index>> is incompatible with the declared type array<integer,object<Doctrine\DBAL\Schema\Index>> of property $addedIndexes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
111
112
        $renameQuery = new SqlMigrationQuery(
113
            $this->platform->getAlterTableSQL($diff)
114
        );
115
116
        $queries->addQuery($renameQuery);
117
    }
118
119
    /**
120
     * Create an unique index without check of table and columns existence.
121
     * This method can be helpful when you need to create an index for renamed table or column
122
     *
123
     * @param Schema      $schema
124
     * @param QueryBag    $queries
125
     * @param string      $tableName
126
     * @param string[]    $columnNames
127
     * @param string|null $indexName
128
     */
129 View Code Duplication
    public function addUniqueIndex(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
130
        Schema $schema,
0 ignored issues
show
Unused Code introduced by
The parameter $schema is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
        QueryBag $queries,
132
        $tableName,
133
        array $columnNames,
134
        $indexName = null
135
    ) {
136
        if (!$indexName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $indexName of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
137
            $indexName = $this->nameGenerator->generateIndexName($tableName, $columnNames, true);
138
        }
139
        $index              = new Index($indexName, $columnNames, true);
140
        $diff               = new TableDiff($tableName);
141
        $diff->addedIndexes = [$indexName => $index];
0 ignored issues
show
Documentation Bug introduced by
It seems like array($indexName => $index) of type array<string,object<Doctrine\DBAL\Schema\Index>> is incompatible with the declared type array<integer,object<Doctrine\DBAL\Schema\Index>> of property $addedIndexes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
142
143
        $renameQuery = new SqlMigrationQuery(
144
            $this->platform->getAlterTableSQL($diff)
145
        );
146
147
        $queries->addQuery($renameQuery);
148
    }
149
150
    /**
151
     * Create a foreign key constraint without check of table and columns existence.
152
     * This method can be helpful when you need to create a constraint for renamed table or column
153
     *
154
     * @param Schema      $schema
155
     * @param QueryBag    $queries
156
     * @param string      $tableName
157
     * @param string      $foreignTable
158
     * @param string[]    $localColumnNames
159
     * @param string[]    $foreignColumnNames
160
     * @param array       $options
161
     * @param string|null $constraintName
162
     */
163
    public function addForeignKeyConstraint(
164
        Schema $schema,
0 ignored issues
show
Unused Code introduced by
The parameter $schema is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
165
        QueryBag $queries,
166
        $tableName,
167
        $foreignTable,
168
        array $localColumnNames,
169
        array $foreignColumnNames,
170
        array $options = [],
171
        $constraintName = null
172
    ) {
173
        if (!$constraintName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $constraintName of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
174
            $constraintName = $this->nameGenerator->generateForeignKeyConstraintName(
175
                $tableName,
176
                $localColumnNames
177
            );
178
        }
179
        $constraint = new ForeignKeyConstraint(
180
            $localColumnNames,
181
            $foreignTable,
182
            $foreignColumnNames,
183
            $constraintName,
184
            $options
185
        );
186
        $diff = new TableDiff($tableName);
187
        $diff->addedForeignKeys = [$constraintName => $constraint];
0 ignored issues
show
Documentation Bug introduced by
It seems like array($constraintName => $constraint) of type array<string,object<Doct...\ForeignKeyConstraint>> is incompatible with the declared type array<integer,object<Doc...\ForeignKeyConstraint>> of property $addedForeignKeys.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
188
189
        $renameQuery = new SqlMigrationQuery(
190
            $this->platform->getAlterTableSQL($diff)
191
        );
192
193
        $queries->addQuery($renameQuery);
194
    }
195
}
196