Conditions | 23 |
Paths | 304 |
Total Lines | 175 |
Code Lines | 121 |
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:
1 | <?php |
||
223 | public function doAlter($msg = '') |
||
224 | { |
||
225 | $data = $this->misc->getDatabaseAccessor(); |
||
226 | |||
227 | if (!isset($_REQUEST['stage'])) { |
||
228 | $_REQUEST['stage'] = 1; |
||
229 | } |
||
230 | |||
231 | $this->prtrace('$_REQUEST', $_REQUEST, 'msg', $msg); |
||
232 | |||
233 | switch ($_REQUEST['stage']) { |
||
234 | case 1: |
||
235 | $this->printTrail('column'); |
||
236 | $this->printTitle($this->lang['stralter'], 'pg.column.alter'); |
||
237 | $this->printMsg($msg); |
||
238 | |||
239 | echo '<script src="' . \SUBFOLDER . '/assets/js/tables.js" type="text/javascript"></script>'; |
||
240 | echo '<form action="' . \SUBFOLDER . "/src/views/colproperties\" method=\"post\">\n"; |
||
241 | |||
242 | // Output table header |
||
243 | echo "<table>\n"; |
||
244 | echo "<tr><th class=\"data required\">{$this->lang['strname']}</th>\n"; |
||
245 | if ($data->hasAlterColumnType()) { |
||
246 | echo "<th class=\"data required\" colspan=\"2\">{$this->lang['strtype']}</th>\n"; |
||
247 | echo "<th class=\"data\">{$this->lang['strlength']}</th>\n"; |
||
248 | } else { |
||
249 | echo "<th class=\"data required\">{$this->lang['strtype']}</th>\n"; |
||
250 | } |
||
251 | echo "<th class=\"data\">{$this->lang['strnotnull']}</th>\n<th class=\"data\">{$this->lang['strdefault']}</th>\n<th class=\"data\">{$this->lang['strcomment']}</th></tr>\n"; |
||
252 | |||
253 | $column = $data->getTableAttributes($_REQUEST['table'], $_REQUEST['column']); |
||
254 | $column->fields['attnotnull'] = $data->phpBool($column->fields['attnotnull']); |
||
255 | |||
256 | // Upon first drawing the screen, load the existing column information |
||
257 | // from the database. |
||
258 | if (!isset($_REQUEST['default'])) { |
||
259 | $_REQUEST['field'] = $column->fields['attname']; |
||
260 | $_REQUEST['type'] = $column->fields['base_type']; |
||
261 | // Check to see if its' an array type... |
||
262 | // XXX: HACKY |
||
263 | if ('[]' == substr($column->fields['base_type'], strlen($column->fields['base_type']) - 2)) { |
||
264 | $_REQUEST['type'] = substr($column->fields['base_type'], 0, strlen($column->fields['base_type']) - 2); |
||
265 | $_REQUEST['array'] = '[]'; |
||
266 | } else { |
||
267 | $_REQUEST['type'] = $column->fields['base_type']; |
||
268 | $_REQUEST['array'] = ''; |
||
269 | } |
||
270 | // To figure out the length, look in the brackets :( |
||
271 | // XXX: HACKY |
||
272 | if ($column->fields['type'] != $column->fields['base_type'] && preg_match('/\\(([0-9, ]*)\\)/', $column->fields['type'], $bits)) { |
||
273 | $_REQUEST['length'] = $bits[1]; |
||
274 | } else { |
||
275 | $_REQUEST['length'] = ''; |
||
276 | } |
||
277 | |||
278 | $_REQUEST['default'] = $_REQUEST['olddefault'] = $column->fields['adsrc']; |
||
279 | if ($column->fields['attnotnull']) { |
||
280 | $_REQUEST['notnull'] = 'YES'; |
||
281 | } |
||
282 | |||
283 | $_REQUEST['comment'] = $column->fields['comment']; |
||
284 | } |
||
285 | |||
286 | // Column name |
||
287 | echo "<tr><td><input name=\"field\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
288 | htmlspecialchars($_REQUEST['field']), "\" /></td>\n"; |
||
289 | |||
290 | // Column type |
||
291 | $escaped_predef_types = []; // the JS escaped array elements |
||
292 | if ($data->hasAlterColumnType()) { |
||
293 | // Fetch all available types |
||
294 | $types = $data->getTypes(true, false, true); |
||
295 | $types_for_js = []; |
||
296 | |||
297 | echo "<td><select name=\"type\" id=\"type\" class=\"select2\" onchange=\"checkLengths(document.getElementById('type').value,'');\">" . "\n"; |
||
298 | while (!$types->EOF) { |
||
299 | $typname = $types->fields['typname']; |
||
300 | $types_for_js[] = $typname; |
||
301 | echo "\t<option value=\"", htmlspecialchars($typname), '"', ($typname == $_REQUEST['type']) ? ' selected="selected"' : '', '>', |
||
302 | $this->misc->printVal($typname), "</option>\n"; |
||
303 | $types->moveNext(); |
||
304 | } |
||
305 | echo "</select>\n"; |
||
306 | echo "</td>\n"; |
||
307 | |||
308 | // Output array type selector |
||
309 | echo "<td><select name=\"array\">\n"; |
||
310 | echo "\t<option value=\"\"", ('' == $_REQUEST['array']) ? ' selected="selected"' : '', "></option>\n"; |
||
311 | echo "\t<option value=\"[]\"", ('[]' == $_REQUEST['array']) ? ' selected="selected"' : '', ">[ ]</option>\n"; |
||
312 | echo "</select></td>\n"; |
||
313 | $predefined_size_types = array_intersect($data->predefined_size_types, $types_for_js); |
||
314 | foreach ($predefined_size_types as $value) { |
||
315 | $escaped_predef_types[] = "'{$value}'"; |
||
316 | } |
||
317 | |||
318 | echo '<td><input name="length" id="lengths" size="8" value="', |
||
319 | htmlspecialchars($_REQUEST['length']), "\" /></td>\n"; |
||
320 | } else { |
||
321 | // Otherwise draw the read-only type name |
||
322 | echo '<td>', $this->misc->printVal($data->formatType($column->fields['type'], $column->fields['atttypmod'])), "</td>\n"; |
||
323 | } |
||
324 | |||
325 | echo '<td><input type="checkbox" name="notnull"', (isset($_REQUEST['notnull'])) ? ' checked="checked"' : '', " /></td>\n"; |
||
326 | echo '<td><input name="default" size="20" value="', |
||
327 | htmlspecialchars($_REQUEST['default']), "\" /></td>\n"; |
||
328 | echo '<td><input name="comment" size="40" value="', |
||
329 | htmlspecialchars($_REQUEST['comment']), "\" /></td></tr>\n"; |
||
330 | echo "</table>\n"; |
||
331 | echo "<p><input type=\"hidden\" name=\"action\" value=\"properties\" />\n"; |
||
332 | echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n"; |
||
333 | echo $this->misc->form; |
||
334 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
335 | echo '<input type="hidden" name="column" value="', htmlspecialchars($_REQUEST['column']), "\" />\n"; |
||
336 | echo '<input type="hidden" name="olddefault" value="', htmlspecialchars($_REQUEST['olddefault']), "\" />\n"; |
||
337 | if ($column->fields['attnotnull']) { |
||
338 | echo "<input type=\"hidden\" name=\"oldnotnull\" value=\"on\" />\n"; |
||
339 | } |
||
340 | |||
341 | echo '<input type="hidden" name="oldtype" value="', htmlspecialchars($data->formatType($column->fields['type'], $column->fields['atttypmod'])), "\" />\n"; |
||
342 | // Add hidden variables to suppress error notices if we don't support altering column type |
||
343 | if (!$data->hasAlterColumnType()) { |
||
344 | echo '<input type="hidden" name="type" value="', htmlspecialchars($_REQUEST['type']), "\" />\n"; |
||
345 | echo '<input type="hidden" name="length" value="', htmlspecialchars($_REQUEST['length']), "\" />\n"; |
||
346 | echo '<input type="hidden" name="array" value="', htmlspecialchars($_REQUEST['array']), "\" />\n"; |
||
347 | } |
||
348 | echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />\n"; |
||
349 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
350 | echo "</form>\n"; |
||
351 | echo '<script type="text/javascript">predefined_lengths = new Array(' . implode(',', $escaped_predef_types) . ");checkLengths(document.getElementById('type').value,'');</script>\n"; |
||
352 | |||
353 | break; |
||
354 | case 2: |
||
355 | // Check inputs |
||
356 | if ('' == trim($_REQUEST['field'])) { |
||
357 | $_REQUEST['stage'] = 1; |
||
358 | $this->doAlter($this->lang['strcolneedsname']); |
||
359 | |||
360 | return; |
||
361 | } |
||
362 | if (!isset($_REQUEST['length'])) { |
||
363 | $_REQUEST['length'] = ''; |
||
364 | } |
||
365 | |||
366 | list($status, $sql) = $data->alterColumn( |
||
367 | $_REQUEST['table'], |
||
368 | $_REQUEST['column'], |
||
369 | $_REQUEST['field'], |
||
370 | isset($_REQUEST['notnull']), |
||
371 | isset($_REQUEST['oldnotnull']), |
||
372 | $_REQUEST['default'], |
||
373 | $_REQUEST['olddefault'], |
||
374 | $_REQUEST['type'], |
||
375 | $_REQUEST['length'], |
||
376 | $_REQUEST['array'], |
||
377 | $_REQUEST['oldtype'], |
||
378 | $_REQUEST['comment'] |
||
379 | ); |
||
380 | |||
381 | $this->prtrace('status', $status, 'sql', $sql); |
||
382 | if (0 == $status) { |
||
383 | if ($_REQUEST['column'] != $_REQUEST['field']) { |
||
384 | $_REQUEST['column'] = $_REQUEST['field']; |
||
385 | $this->misc->setReloadBrowser(true); |
||
386 | } |
||
387 | $this->doDefault($sql . "<br/>{$this->lang['strcolumnaltered']}"); |
||
388 | } else { |
||
389 | $_REQUEST['stage'] = 1; |
||
390 | $this->doAlter($sql . "<br/>{$this->lang['strcolumnalteredbad']}"); |
||
391 | |||
392 | return; |
||
393 | } |
||
394 | |||
395 | break; |
||
396 | default: |
||
397 | echo "<p>{$this->lang['strinvalidparam']}</p>\n"; |
||
398 | } |
||
401 |