Conditions | 30 |
Paths | > 20000 |
Total Lines | 206 |
Code Lines | 121 |
Lines | 0 |
Ratio | 0 % |
Changes | 9 | ||
Bugs | 1 | Features | 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 |
||
322 | function processRequests($db) |
||
323 | { |
||
324 | if (array_key_exists('window', $_REQUEST)) { |
||
325 | // All windows related commands must start with window=. |
||
326 | |||
327 | $windownumber = $_REQUEST['window']; |
||
328 | $windowname = false; |
||
329 | $windowhex = 0; |
||
330 | // TODO: $win_id und $windowname können vermutlich zusammengefasst werden. |
||
331 | $win_id = 0; |
||
332 | |||
333 | if ($windownumber != 'vncwin') { |
||
334 | // This is the normal case. |
||
335 | // Special handling is needed when called with window=vncwin, see below. |
||
336 | $window = $windownumber - 1; |
||
337 | |||
338 | $win_id = $db->getWindowIDBySection($windownumber); |
||
339 | $windowlist = windowList(); |
||
340 | |||
341 | if (count($windowlist) == 0) { |
||
342 | trace("no window found for command"); |
||
343 | } else { |
||
344 | // TODO: improve test whether window exists. |
||
345 | $windowname = $windowlist[$window]; |
||
346 | $windowhex = hexdec($windowname); |
||
347 | } |
||
348 | } |
||
349 | |||
350 | if ($windowname && array_key_exists('key', $_REQUEST)) { |
||
351 | $key = $_REQUEST['key']; |
||
352 | trace("key '$key' in window '$windownumber'"); |
||
353 | wmShow($windowname); |
||
354 | // activateControls($windowhex); |
||
355 | // displayCommand("xdotool windowfocus $windowhex key $key"); |
||
356 | |||
357 | // trying mousemove and click for better vnc control |
||
358 | displayCommand("xdotool mousemove --window $windowhex 100 100 " . |
||
359 | "key $key"); |
||
360 | } |
||
361 | |||
362 | if ($windowname && array_key_exists('keydown', $_REQUEST)) { |
||
363 | // TODO: keydown is currently mapped to key because we had problems |
||
364 | // with sticking keys (no keyup seen). This should be fixed by a |
||
365 | // better event handling. |
||
366 | $key = $_REQUEST['keydown']; |
||
367 | trace("keydown '$key' in window '$windownumber'"); |
||
368 | wmShow($windowname); |
||
369 | // activateControls($windowhex); |
||
370 | // displayCommand("xdotool windowfocus $windowhex key $key"); |
||
371 | |||
372 | // trying mousemove and click for better vnc control |
||
373 | displayCommand("xdotool mousemove --window $windowhex 100 100 " . |
||
374 | "key $key"); |
||
375 | //~ displayCommand("xdotool windowfocus $windowhex keydown $key"); |
||
376 | } |
||
377 | |||
378 | if ($windowname && array_key_exists('keyup', $_REQUEST)) { |
||
379 | // TODO: keyup is currently ignored, see comment above. |
||
380 | $key = $_REQUEST['keyup']; |
||
381 | trace("keyup '$key' in window '$windownumber'"); |
||
382 | // activateControls($windowhex); |
||
383 | //~ wmShow($windowname); |
||
384 | //~ displayCommand("xdotool windowfocus $windowhex keyup $key"); |
||
385 | } |
||
386 | |||
387 | if (array_key_exists('delete', $_REQUEST)) { |
||
388 | |||
389 | $delete = str_replace(" ", "\ ", addslashes($_REQUEST['delete'])); |
||
390 | trace("delete=$delete, close window $windownumber"); |
||
391 | |||
392 | if (file_exists($delete)) { |
||
393 | trace("+++ DELETE FILE FROM WEBINTERFACE +++"); |
||
394 | unlink($delete); |
||
395 | } elseif ($delete == "VNC") { |
||
396 | trace("+++ DELETE VNC Client FROM DAEMON +++"); |
||
397 | // call via daemon: ?window=vncwin&delete=VNC&vncid=123 |
||
398 | trace("vnc delete in control"); |
||
399 | $win_id = $_REQUEST['vncid']; // = hexWindow in database, but not on screen |
||
400 | trace("VNC cia Daemon ... id=$win_id"); |
||
401 | |||
402 | } elseif (strstr($delete, "http")) { |
||
403 | trace("+++ DELETE Browserwindow +++"); |
||
404 | } elseif (preg_match('/(^\w{3,}@\w{1,})/', $delete)) { |
||
405 | trace("+++ DELETE VNC Client FROM WEBINTERFACE +++"); |
||
406 | // call via webinterface |
||
407 | $win_id = $db->querySingle("SELECT win_id FROM window WHERE file='$delete' AND handler='vnc'"); |
||
408 | trace("DELETE VNC Window with ID=$win_id FROM Database :: |
||
409 | SELECT win_id FROM window WHERE file='$delete' AND handler='vnc'"); |
||
410 | } else { |
||
411 | trace("Unhandled delete for '$delete'"); |
||
412 | } |
||
413 | |||
414 | wmClose($win_id); |
||
415 | $db->deleteWindow($win_id); |
||
416 | } |
||
417 | |||
418 | if (array_key_exists('closeOrphans', $_REQUEST)) { |
||
419 | |||
420 | // win_ids in db |
||
421 | $windows_in_db = $db->getWindows(); |
||
422 | $db_ids = array(); |
||
423 | |||
424 | if (count($windows_in_db) > 0) { |
||
425 | foreach ($windows_in_db as $win) { |
||
426 | array_push($db_ids, $win['win_id']); |
||
427 | } |
||
428 | } |
||
429 | |||
430 | // win_ids on screen |
||
431 | $screen_ids = windowListOnScreen(); |
||
432 | |||
433 | // orphaned windows |
||
434 | $orphan_ids = array_diff($screen_ids, $db_ids); |
||
435 | |||
436 | if (count($orphan_ids) > 0) { |
||
437 | // close windows on screen not existing in database |
||
438 | foreach ($orphan_ids as $id) { |
||
439 | wmClose($id); |
||
440 | } |
||
441 | } |
||
442 | |||
443 | } |
||
444 | |||
445 | if (array_key_exists('toggle', $_REQUEST)) { |
||
446 | // Change window state from visible to invisible and vice versa. |
||
447 | $state = $db->getWindowState($win_id); |
||
448 | trace("toggle window $windownumber, id=$win_id, state=$state"); |
||
449 | if ($state == "active") { |
||
450 | wmHide($win_id); |
||
451 | $db->setWindowState($win_id, "inactive"); |
||
452 | } else { |
||
453 | wmShow($win_id); |
||
454 | $db->setWindowState($win_id, "active"); |
||
455 | } |
||
456 | } |
||
457 | } elseif (array_key_exists('layout', $_REQUEST)) { |
||
458 | setLayout($_REQUEST['layout']); |
||
459 | } elseif (array_key_exists('logout', $_REQUEST)) { |
||
460 | doLogout($_REQUEST['logout']); |
||
461 | } elseif (array_key_exists('newVncWindow', $_REQUEST)) { |
||
462 | // TODO: Better write new code for VNC window. |
||
463 | addNewWindow($db, unserialize(urldecode($_REQUEST['newVncWindow']))); |
||
464 | } elseif (array_key_exists('newWindow', $_REQUEST)) { |
||
465 | createNewWindow($db, unserialize(urldecode($_REQUEST['newWindow']))); |
||
466 | } |
||
467 | |||
468 | if (array_key_exists('switchWindows', $_REQUEST)) { |
||
469 | $before = $_REQUEST['before']; |
||
470 | $after = $_REQUEST['after']; |
||
471 | trace("switching $before and $after"); |
||
472 | |||
473 | // exchange section |
||
474 | $win_id1 = $db->getWindowIDBySection($before); |
||
475 | $win_id2 = $db->getWindowIDBySection($after); |
||
476 | |||
477 | $db->updateWindow($win_id1, 'section', $after); |
||
478 | $db->updateWindow($win_id2, 'section', $before); |
||
479 | |||
480 | trace("++updating database $win_id1 section=$after"); |
||
481 | trace("++updating database $win_id2 section=$before"); |
||
482 | |||
483 | // Update display (no layout change). |
||
484 | setLayout(null); |
||
485 | } |
||
486 | |||
487 | if (array_key_exists('openURL', $_REQUEST)) { |
||
488 | $openURL = $_REQUEST['openURL']; |
||
489 | trace("openURL $openURL"); |
||
490 | |||
491 | // If URL leads to pdf file, download it and treat as upload |
||
492 | if (preg_match("/.(pdf|PDF)$/", $openURL)) { |
||
493 | trace("openURL $openURL is a pdf file. Downloading it."); |
||
494 | $date = time(); |
||
495 | $temp_name = basename($openURL); |
||
496 | $temp_dir = "/tmp/palma_$date"; |
||
497 | shell_exec("mkdir $temp_dir && wget $openURL -P $temp_dir/"); |
||
498 | |||
499 | $_FILES['file']['name'] = "$temp_name"; |
||
500 | $_FILES['file']['tmp_name'] = "$temp_dir/$temp_name"; |
||
501 | $_FILES['file']['error'] = "downloaded_from_url"; |
||
502 | |||
503 | trace("Handing over to upload.php"); |
||
504 | include 'upload.php'; |
||
505 | } else { |
||
506 | $dt = new DateTime(); |
||
507 | $date = $dt->format('Y-m-d H:i:s'); |
||
508 | $window = array( |
||
509 | "id" => "", |
||
510 | "win_id" => "", |
||
511 | "section" => "", |
||
512 | "state" => "", |
||
513 | "file" => $openURL, |
||
514 | // "handler" => "iceweasel --new-window", |
||
515 | "handler" => "/usr/bin/midori -e show-navigationbar=false -a", |
||
516 | "userid" => "", |
||
517 | "date" => $date |
||
518 | ); |
||
519 | createNewWindow($db, $window); |
||
520 | } |
||
521 | } |
||
522 | |||
523 | // TODO: chef if query redundant? |
||
524 | if (array_key_exists('closeAll', $_REQUEST)) { |
||
525 | $close = $_REQUEST['closeAll']; |
||
526 | trace("close all windows $close"); |
||
527 | closeAll(); |
||
528 | } |
||
551 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.