Conditions | 12 |
Paths | 289 |
Total Lines | 56 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
57 | public function alterColumn( |
||
58 | AbstractTable $table, |
||
59 | AbstractColumn $initial, |
||
60 | AbstractColumn $column |
||
61 | ) { |
||
62 | if (!$initial instanceof ColumnSchema || !$column instanceof ColumnSchema) { |
||
63 | throw new SchemaException("SQlServer commander can work only with Postgres columns."); |
||
64 | } |
||
65 | |||
66 | if ($column->getName() != $initial->getName()) { |
||
67 | //Renaming is separate operation |
||
68 | $this->run("sp_rename ?, ?, 'COLUMN'", [ |
||
69 | $table->getName() . '.' . $initial->getName(), |
||
70 | $column->getName() |
||
71 | ]); |
||
72 | } |
||
73 | |||
74 | //In SQLServer we have to drop ALL related indexes and foreign keys while |
||
75 | //applying type change... yeah... |
||
76 | |||
77 | $indexesBackup = []; |
||
78 | $foreignBackup = []; |
||
79 | foreach ($table->getIndexes() as $index) { |
||
80 | if (in_array($column->getName(), $index->getColumns())) { |
||
81 | $indexesBackup[] = $index; |
||
82 | $this->dropIndex($table, $index); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | foreach ($table->getForeigns() as $foreign) { |
||
87 | if ($foreign->getColumn() == $column->getName()) { |
||
88 | $foreignBackup[] = $foreign; |
||
89 | $this->dropForeign($table, $foreign); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | //Column will recreate needed constraints |
||
94 | foreach ($column->getConstraints() as $constraint) { |
||
95 | $this->dropConstrain($table, $constraint); |
||
96 | } |
||
97 | |||
98 | foreach ($column->alteringOperations($initial) as $operation) { |
||
99 | $this->run("ALTER TABLE {$table->getName(true)} {$operation}"); |
||
100 | } |
||
101 | |||
102 | //Restoring indexes and foreign keys |
||
103 | foreach ($indexesBackup as $index) { |
||
104 | $this->addIndex($table, $index); |
||
105 | } |
||
106 | |||
107 | foreach ($foreignBackup as $foreign) { |
||
108 | $this->addForeign($table, $foreign); |
||
109 | } |
||
110 | |||
111 | return $this; |
||
112 | } |
||
113 | |||
127 | } |