Conditions | 39 |
Paths | > 20000 |
Total Lines | 218 |
Code Lines | 97 |
Lines | 26 |
Ratio | 11.93 % |
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 |
||
472 | public function check($callIfSuccess = null, $callIfFail = null) |
||
473 | { |
||
474 | $remember = null; |
||
475 | $validates = null; |
||
476 | $callbackStatus = null; |
||
477 | $values = []; |
||
478 | |||
479 | // Remember output messages in session |
||
480 | $output = $this->session["output"]; |
||
481 | if (isset($_SESSION[$output])) { |
||
482 | $this->output = $_SESSION[$output]; |
||
483 | unset($_SESSION[$output]); |
||
484 | } |
||
485 | |||
486 | // Check if this was a post request |
||
487 | if ($_SERVER["REQUEST_METHOD"] !== "POST") { |
||
488 | // Its not posted, but check if values should be used from session |
||
489 | $failed = $this->session["failed"]; |
||
490 | $remember = $this->session["remember"]; |
||
491 | $save = $this->session["save"]; |
||
492 | |||
493 | if (isset($_SESSION[$failed])) { |
||
494 | |||
495 | // Read form data from session if the previous post failed during validation. |
||
496 | $this->InitElements($_SESSION[$failed]); |
||
497 | unset($_SESSION[$failed]); |
||
498 | |||
499 | } elseif (isset($_SESSION[$remember])) { |
||
500 | |||
501 | // Read form data from session if some form elements should be remembered |
||
502 | foreach ($_SESSION[$remember] as $key => $val) { |
||
503 | $this[$key]['value'] = $val['value']; |
||
504 | } |
||
505 | unset($_SESSION[$remember]); |
||
506 | |||
507 | } elseif (isset($_SESSION[$save])) { |
||
508 | |||
509 | // Read form data from session, |
||
510 | // useful during test where the original form is displayed with its posted values |
||
511 | $this->InitElements($_SESSION[$save]); |
||
512 | unset($_SESSION[$save]); |
||
513 | } |
||
514 | |||
515 | return null; |
||
516 | } |
||
517 | $request = $_POST; |
||
518 | |||
519 | // Check if its a form we are dealing with |
||
520 | if (!isset($request["anax/htmlform-id"])) { |
||
521 | return null; |
||
522 | } |
||
523 | |||
524 | // Check if its this form that was posted |
||
525 | if ($this->form["id"] !== $request["anax/htmlform-id"]) { |
||
526 | return null; |
||
527 | } |
||
528 | |||
529 | // This form was posted, process it |
||
530 | if (isset($_SESSION[$this->session["failed"]])) { |
||
531 | unset($_SESSION[$this->session["failed"]]); |
||
532 | } |
||
533 | |||
534 | $validates = true; |
||
535 | foreach ($this->elements as $element) { |
||
536 | |||
537 | $elementName = $element['name']; |
||
538 | $elementType = $element['type']; |
||
539 | |||
540 | // The form element has a value set |
||
541 | if (isset($request[$elementName])) { |
||
542 | |||
543 | // Multiple choices comes in the form of an array |
||
544 | if (is_array($request[$elementName])) { |
||
545 | $values[$elementName]['values'] = $element['checked'] = $request[$elementName]; |
||
546 | } else { |
||
547 | $values[$elementName]['value'] = $element['value'] = $request[$elementName]; |
||
548 | } |
||
549 | |||
550 | // If the element is a password, do not remember. |
||
551 | if ($elementType === 'password') { |
||
552 | $values[$elementName]['value'] = null; |
||
553 | } |
||
554 | |||
555 | // If the element is a checkbox, set its value of checked. |
||
556 | if ($elementType === 'checkbox') { |
||
557 | $element['checked'] = true; |
||
558 | } |
||
559 | |||
560 | // If the element is a radio, set the value to checked. |
||
561 | if ($elementType === 'radio') { |
||
562 | $element['checked'] = $element['value']; |
||
563 | } |
||
564 | |||
565 | // Do validation of form element |
||
566 | View Code Duplication | if (isset($element['validation'])) { |
|
567 | |||
568 | $element['validation-pass'] = $element->Validate($element['validation'], $this); |
||
569 | |||
570 | if ($element['validation-pass'] === false) { |
||
571 | |||
572 | $values[$elementName] = [ |
||
573 | 'value' => $element['value'], |
||
574 | 'validation-messages' => $element['validation-messages'] |
||
575 | ]; |
||
576 | $validates = false; |
||
577 | |||
578 | } |
||
579 | } |
||
580 | |||
581 | // Hmmm.... Why did I need this remember thing? |
||
582 | if (isset($element['remember']) |
||
583 | && $element['remember'] |
||
584 | ) { |
||
585 | $values[$elementName] = ['value' => $element['value']]; |
||
586 | $remember = true; |
||
587 | } |
||
588 | |||
589 | // Carry out the callback if the form element validates |
||
590 | // Hmmm, what if the element with the callback is not the last element? |
||
591 | if (isset($element['callback']) |
||
592 | && $validates |
||
593 | ) { |
||
594 | |||
595 | if (isset($element['callback-args'])) { |
||
596 | |||
597 | $callbackStatus = call_user_func_array( |
||
598 | $element['callback'], |
||
599 | array_merge([$this]), |
||
600 | $element['callback-args'] |
||
601 | ); |
||
602 | |||
603 | } else { |
||
604 | |||
605 | $callbackStatus = call_user_func($element['callback'], $this); |
||
606 | } |
||
607 | } |
||
608 | |||
609 | } else { |
||
610 | |||
611 | // The form element has no value set |
||
612 | |||
613 | // Set element to null, then we know it was not set. |
||
614 | //$element['value'] = null; |
||
615 | //echo $element['type'] . ':' . $elementName . ':' . $element['value'] . '<br>'; |
||
616 | |||
617 | // If the element is a checkbox, clear its value of checked. |
||
618 | if ($element['type'] === 'checkbox' |
||
619 | || $element['type'] === 'checkbox-multiple' |
||
620 | ) { |
||
621 | |||
622 | $element['checked'] = false; |
||
623 | } |
||
624 | |||
625 | // Do validation even when the form element is not set? |
||
626 | // Duplicate code, revise this section and move outside |
||
627 | // this if-statement? |
||
628 | View Code Duplication | if (isset($element['validation'])) { |
|
629 | |||
630 | $element['validation-pass'] = $element->Validate($element['validation'], $this); |
||
631 | |||
632 | if ($element['validation-pass'] === false) { |
||
633 | |||
634 | $values[$elementName] = [ |
||
635 | 'value' => $element['value'], 'validation-messages' => $element['validation-messages'] |
||
636 | ]; |
||
637 | $validates = false; |
||
638 | } |
||
639 | } |
||
640 | } |
||
641 | } |
||
642 | |||
643 | // Prepare if data should be stored in the session during redirects |
||
644 | // Did form validation or the callback fail? |
||
645 | if ($validates === false |
||
646 | || $callbackStatus === false |
||
647 | ) { |
||
648 | |||
649 | $_SESSION[$this->session["failed"]] = $values; |
||
650 | |||
651 | } elseif ($remember) { |
||
652 | |||
653 | // Hmmm, why do I want to use this |
||
654 | $_SESSION[$this->session["remember"]] = $values; |
||
655 | } |
||
656 | |||
657 | if (isset($this->saveInSession) && $this->saveInSession) { |
||
658 | |||
659 | // Remember all posted values |
||
660 | $_SESSION[$this->session["save"]] = $values; |
||
661 | } |
||
662 | |||
663 | // Lets se what the return value should be |
||
664 | $ret = $validates |
||
665 | ? $callbackStatus |
||
666 | : $validates; |
||
667 | |||
668 | |||
669 | if ($ret === true && isset($callIfSuccess)) { |
||
670 | |||
671 | // Use callback for success, if defined |
||
672 | if (is_callable($callIfSuccess)) { |
||
673 | call_user_func_array($callIfSuccess, [$this]); |
||
674 | } else { |
||
675 | throw new \Exception("Form, success-method is not callable."); |
||
676 | } |
||
677 | |||
678 | } elseif ($ret === false && isset($callIfFail)) { |
||
679 | |||
680 | // Use callback for fail, if defined |
||
681 | if (is_callable($callIfFail)) { |
||
682 | call_user_func_array($callIfFail, [$this]); |
||
683 | } else { |
||
684 | throw new \Exception("Form, success-method is not callable."); |
||
685 | } |
||
686 | } |
||
687 | |||
688 | return $ret; |
||
689 | } |
||
690 | } |
||
691 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.