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