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