Conditions | 18 |
Paths | 2162 |
Total Lines | 146 |
Code Lines | 79 |
Lines | 0 |
Ratio | 0 % |
Changes | 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
153 | public function alterColumn( |
||
154 | $table, |
||
155 | $column, |
||
156 | $name, |
||
157 | $notnull, |
||
158 | $oldnotnull, |
||
159 | $default, |
||
160 | $olddefault, |
||
161 | $type, |
||
162 | $length, |
||
163 | $array, |
||
164 | $oldtype, |
||
165 | $comment |
||
166 | ) { |
||
167 | // Begin transaction |
||
168 | $status = $this->beginTransaction(); |
||
169 | $sql = ''; |
||
170 | $sqlrename = ''; |
||
171 | |||
172 | if (0 !== $status) { |
||
173 | $this->rollbackTransaction(); |
||
174 | |||
175 | return [-6, $sql]; |
||
176 | } |
||
177 | |||
178 | // Rename the column, if it has been changed |
||
179 | if ($column !== $name) { |
||
180 | [$status, $sqlrename] = $this->renameColumn($table, $column, $name); |
||
181 | |||
182 | if (0 !== $status) { |
||
183 | $this->rollbackTransaction(); |
||
184 | |||
185 | return [-4, $sql]; |
||
186 | } |
||
187 | } |
||
188 | |||
189 | $f_schema = $this->_schema; |
||
190 | $this->fieldClean($f_schema); |
||
191 | $this->fieldClean($name); |
||
192 | $this->fieldClean($table); |
||
193 | $this->fieldClean($column); |
||
194 | |||
195 | $toAlter = []; |
||
196 | // Create the command for changing nullability |
||
197 | if ($notnull !== $oldnotnull) { |
||
198 | $toAlter[] = \sprintf( |
||
199 | 'ALTER COLUMN "%s" ', |
||
200 | $name |
||
201 | ) . ($notnull ? 'SET' : 'DROP') . ' NOT NULL'; |
||
202 | } |
||
203 | |||
204 | // Add default, if it has changed |
||
205 | if ($default !== $olddefault) { |
||
206 | if ('' === $default) { |
||
207 | $toAlter[] = \sprintf( |
||
208 | 'ALTER COLUMN "%s" DROP DEFAULT', |
||
209 | $name |
||
210 | ); |
||
211 | } else { |
||
212 | $toAlter[] = \sprintf( |
||
213 | 'ALTER COLUMN "%s" SET DEFAULT %s', |
||
214 | $name, |
||
215 | $default |
||
216 | ); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | // Add type, if it has changed |
||
221 | if ('' === $length) { |
||
|
|||
222 | $ftype = $type; |
||
223 | } else { |
||
224 | switch ($type) { |
||
225 | // Have to account for weird placing of length for with/without |
||
226 | // time zone types |
||
227 | case 'timestamp with time zone': |
||
228 | case 'timestamp without time zone': |
||
229 | $qual = \mb_substr($type, 9); |
||
230 | $ftype = \sprintf( |
||
231 | 'timestamp(%s)%s', |
||
232 | $length, |
||
233 | $qual |
||
234 | ); |
||
235 | |||
236 | break; |
||
237 | case 'time with time zone': |
||
238 | case 'time without time zone': |
||
239 | $qual = \mb_substr($type, 4); |
||
240 | $ftype = \sprintf( |
||
241 | 'time(%s)%s', |
||
242 | $length, |
||
243 | $qual |
||
244 | ); |
||
245 | |||
246 | break; |
||
247 | |||
248 | default: |
||
249 | $ftype = \sprintf( |
||
250 | '%s(%s)', |
||
251 | $type, |
||
252 | $length |
||
253 | ); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | // Add array qualifier, if requested |
||
258 | if ($array) { |
||
259 | $ftype .= '[]'; |
||
260 | } |
||
261 | |||
262 | if ($ftype !== $oldtype) { |
||
263 | $toAlter[] = \sprintf( |
||
264 | 'ALTER COLUMN "%s" TYPE %s', |
||
265 | $name, |
||
266 | $ftype |
||
267 | ); |
||
268 | } |
||
269 | |||
270 | // Attempt to process the batch alteration, if anything has been changed |
||
271 | if (!empty($toAlter)) { |
||
272 | // Initialise an empty SQL string |
||
273 | $sql = \sprintf( |
||
274 | 'ALTER TABLE "%s"."%s" ', |
||
275 | $f_schema, |
||
276 | $table |
||
277 | ) |
||
278 | . \implode(',', $toAlter); |
||
279 | |||
280 | $status = $this->execute($sql); |
||
281 | |||
282 | if (0 !== $status) { |
||
283 | $this->rollbackTransaction(); |
||
284 | |||
285 | return [-1, $sql]; |
||
286 | } |
||
287 | } |
||
288 | |||
289 | // Update the comment on the column |
||
290 | $status = $this->setComment('COLUMN', $name, $table, $comment); |
||
291 | |||
292 | if (0 !== $status) { |
||
293 | $this->rollbackTransaction(); |
||
294 | |||
295 | return [-5, $sql]; |
||
296 | } |
||
297 | |||
298 | return [$this->endTransaction(), $sqlrename . '<br>' . $sql]; |
||
299 | } |
||
473 |