Conditions | 13 |
Paths | 126 |
Total Lines | 122 |
Code Lines | 68 |
Lines | 0 |
Ratio | 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 |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | protected function synchroniseSchema() |
||
84 | { |
||
85 | if (!$this->requiresRebuild()) { |
||
86 | //Probably some index changed or table renamed |
||
87 | return parent::synchroniseSchema(); |
||
88 | } |
||
89 | |||
90 | $this->logger()->debug("Rebuilding table {table} to apply required modifications.", [ |
||
91 | 'table' => $this->getName(true) |
||
92 | ]); |
||
93 | |||
94 | //Temporary table is required to copy data over |
||
95 | $temporary = $this->createTemporary(); |
||
96 | |||
97 | //Moving data over |
||
98 | $this->copyData($temporary, $this->columnsMapping(true)); |
||
99 | |||
100 | //Dropping current table |
||
101 | $this->commander->dropTable($this->initial->getName()); |
||
102 | |||
103 | //Renaming temporary table (should automatically handle table renaming) |
||
104 | $this->commander->renameTable($temporary->getName(), $this->getName()); |
||
105 | |||
106 | //We can create needed indexes now |
||
107 | foreach ($this->getIndexes() as $index) { |
||
108 | $this->commander->addIndex($this, $index); |
||
109 | } |
||
110 | |||
111 | return $this; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | protected function columnSchema($name, $schema = null) |
||
118 | { |
||
119 | return new ColumnSchema($this, $name, $schema); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | protected function indexSchema($name, $schema = null) |
||
126 | { |
||
127 | return new IndexSchema($this, $name, $schema); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | protected function referenceSchema($name, $schema = null) |
||
134 | { |
||
135 | return new ReferenceSchema($this, $name, $schema); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Rebuild is required when columns or foreign keys are altered. |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | private function requiresRebuild() |
||
144 | { |
||
145 | $difference = [ |
||
146 | count($this->comparator->addedColumns()), |
||
147 | count($this->comparator->droppedColumns()), |
||
148 | count($this->comparator->alteredColumns()), |
||
149 | count($this->comparator->addedForeigns()), |
||
150 | count($this->comparator->droppedForeigns()), |
||
151 | count($this->comparator->alteredForeigns()) |
||
152 | ]; |
||
153 | |||
154 | return array_sum($difference) != 0; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Temporary table. |
||
159 | * |
||
160 | * @return TableSchema |
||
161 | */ |
||
162 | private function createTemporary() |
||
163 | { |
||
164 | //Temporary table is required to copy data over |
||
165 | $temporary = clone $this; |
||
166 | $temporary->setName('spiral_temp_' . $this->getName() . '_' . uniqid()); |
||
167 | |||
168 | //We don't need any index in temporary table |
||
169 | foreach ($temporary->getIndexes() as $index) { |
||
170 | $temporary->forgetIndex($index); |
||
171 | } |
||
172 | |||
173 | $this->commander->createTable($temporary); |
||
174 | |||
175 | return $temporary; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Copy table data to another location. |
||
180 | * |
||
181 | * @see http://stackoverflow.com/questions/4007014/alter-column-in-sqlite |
||
182 | * @param AbstractTable $temporary |
||
183 | * @param array $mapping Association between old and new columns (quoted). |
||
184 | */ |
||
185 | private function copyData(AbstractTable $temporary, array $mapping) |
||
186 | { |
||
187 | $this->logger()->debug( |
||
188 | "Copying table data from {source} to {table} using mapping ({columns}) => ({target}).", |
||
189 | [ |
||
190 | 'source' => $this->driver->identifier($this->initial->getName()), |
||
191 | 'table' => $temporary->getName(true), |
||
192 | 'columns' => join(', ', $mapping), |
||
193 | 'target' => join(', ', array_keys($mapping)) |
||
194 | ] |
||
195 | ); |
||
196 | |||
197 | $query = \Spiral\interpolate( |
||
198 | "INSERT INTO {table} ({target}) SELECT {columns} FROM {source}", |
||
199 | [ |
||
200 | 'source' => $this->driver->identifier($this->initial->getName()), |
||
231 | } |
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.