Conditions | 73 |
Paths | 69 |
Total Lines | 305 |
Code Lines | 163 |
Lines | 154 |
Ratio | 50.49 % |
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 |
||
414 | function filter_fields_set ($filter_id, $template_id) |
||
415 | { |
||
416 | debug_write_log(DEBUG_TRACE, '[filter_fields_set]'); |
||
417 | debug_write_log(DEBUG_DUMP, '[filter_fields_set] $filter_id = ' . $filter_id); |
||
418 | debug_write_log(DEBUG_DUMP, '[filter_fields_set] $template_id = ' . $template_id); |
||
419 | |||
420 | dal_query('filters/ffdelall.sql', $filter_id, $_SESSION[VAR_USERID]); |
||
421 | $rs = dal_query('states/list.sql', $template_id, 'state_type, state_name'); |
||
422 | |||
423 | while (($row = $rs->fetch())) |
||
424 | { |
||
425 | $rsf = dal_query('filters/flist.sql', |
||
426 | $row['state_id'], |
||
427 | $_SESSION[VAR_USERID], |
||
428 | FIELD_ALLOW_TO_READ); |
||
429 | |||
430 | while (($row = $rsf->fetch())) |
||
431 | { |
||
432 | $name = 'field' . $row['field_id']; |
||
433 | |||
434 | if (isset($_REQUEST[$name])) |
||
435 | { |
||
436 | debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Found filter for field #{$row['field_id']}."); |
||
437 | |||
438 | switch ($row['field_type']) |
||
439 | { |
||
440 | case FIELD_TYPE_NUMBER: |
||
441 | |||
442 | debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is number."); |
||
443 | |||
444 | $min_value = ustrcut(try_request('min_' . $name), ustrlen(MAX_FIELD_INTEGER) + 1); |
||
445 | $max_value = ustrcut(try_request('max_' . $name), ustrlen(MAX_FIELD_INTEGER) + 1); |
||
446 | |||
447 | if (ustrlen($min_value) == 0) |
||
448 | { |
||
449 | $min_value = NULL; |
||
450 | } |
||
451 | |||
452 | if (ustrlen($max_value) == 0) |
||
453 | { |
||
454 | $max_value = NULL; |
||
455 | } |
||
456 | |||
457 | if (!is_null($min_value) && !is_intvalue($min_value) || |
||
458 | !is_null($max_value) && !is_intvalue($max_value)) |
||
459 | { |
||
460 | debug_write_log(DEBUG_NOTICE, '[filter_fields_set] At least one of range values is invalid.'); |
||
461 | } |
||
462 | else |
||
463 | { |
||
464 | if (!is_null($min_value) && !is_null($max_value) && ($min_value > $max_value)) |
||
465 | { |
||
466 | swap($min_value, $max_value); |
||
467 | } |
||
468 | |||
469 | dal_query('filters/ffcreate.sql', |
||
470 | $filter_id, |
||
471 | $row['field_id'], |
||
472 | is_null($min_value) ? NULL : $min_value, |
||
473 | is_null($max_value) ? NULL : $max_value); |
||
474 | } |
||
475 | |||
476 | break; |
||
477 | |||
478 | case FIELD_TYPE_FLOAT: |
||
479 | |||
480 | debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is float."); |
||
481 | |||
482 | $min_value = ustrcut(try_request('min_' . $name), ustrlen(MIN_FIELD_FLOAT)); |
||
483 | $max_value = ustrcut(try_request('max_' . $name), ustrlen(MAX_FIELD_FLOAT)); |
||
484 | |||
485 | if (ustrlen($min_value) == 0) |
||
486 | { |
||
487 | $min_value = NULL; |
||
488 | } |
||
489 | |||
490 | if (ustrlen($max_value) == 0) |
||
491 | { |
||
492 | $max_value = NULL; |
||
493 | } |
||
494 | |||
495 | if (!is_null($min_value) && !is_floatvalue($min_value) || |
||
496 | !is_null($max_value) && !is_floatvalue($max_value)) |
||
497 | { |
||
498 | debug_write_log(DEBUG_NOTICE, '[filter_fields_set] At least one of range values is invalid.'); |
||
499 | } |
||
500 | else |
||
501 | { |
||
502 | if (!is_null($min_value) && !is_null($max_value) && (bccomp($min_value, $max_value) > 0)) |
||
503 | { |
||
504 | swap($min_value, $max_value); |
||
505 | } |
||
506 | |||
507 | dal_query('filters/ffcreate.sql', |
||
508 | $filter_id, |
||
509 | $row['field_id'], |
||
510 | is_null($min_value) ? NULL : value_find_float($min_value), |
||
511 | is_null($max_value) ? NULL : value_find_float($max_value)); |
||
512 | } |
||
513 | |||
514 | break; |
||
515 | |||
516 | case FIELD_TYPE_STRING: |
||
517 | case FIELD_TYPE_MULTILINED: |
||
518 | |||
519 | debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is string/multilined."); |
||
520 | |||
521 | $value = ustrcut(try_request('edit_' . $name), MAX_FIELD_STRING); |
||
522 | |||
523 | if (ustrlen($value) == 0) |
||
524 | { |
||
525 | $value = NULL; |
||
526 | } |
||
527 | |||
528 | $param = value_find_string($value); |
||
529 | |||
530 | dal_query('filters/ffcreate.sql', |
||
531 | $filter_id, |
||
532 | $row['field_id'], |
||
533 | is_null($param) ? NULL : $param, |
||
534 | NULL); |
||
535 | |||
536 | break; |
||
537 | |||
538 | case FIELD_TYPE_CHECKBOX: |
||
539 | |||
540 | debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is checkbox."); |
||
541 | |||
542 | $checked = try_request('check_' . $name, 0); |
||
543 | |||
544 | dal_query('filters/ffcreate.sql', |
||
545 | $filter_id, |
||
546 | $row['field_id'], |
||
547 | bool2sql($checked), |
||
548 | NULL); |
||
549 | |||
550 | break; |
||
551 | |||
552 | View Code Duplication | case FIELD_TYPE_LIST: |
|
553 | |||
554 | debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is list."); |
||
555 | |||
556 | $value = ustrcut(try_request('list_' . $name), ustrlen(MAXINT)); |
||
557 | |||
558 | if (ustrlen($value) == 0) |
||
559 | { |
||
560 | $value = NULL; |
||
561 | } |
||
562 | |||
563 | if (!is_null($value) && !is_intvalue($value)) |
||
564 | { |
||
565 | debug_write_log(DEBUG_NOTICE, '[filter_fields_set] Invalid integer value.'); |
||
566 | } |
||
567 | else |
||
568 | { |
||
569 | if (!is_null($value) && ($value < 1 || $value > MAXINT)) |
||
570 | { |
||
571 | debug_write_log(DEBUG_NOTICE, '[filter_fields_set] Invalid integer range.'); |
||
572 | } |
||
573 | else |
||
574 | { |
||
575 | dal_query('filters/ffcreate.sql', |
||
576 | $filter_id, |
||
577 | $row['field_id'], |
||
578 | is_null($value) ? NULL : $value, |
||
579 | NULL); |
||
580 | } |
||
581 | } |
||
582 | |||
583 | break; |
||
584 | |||
585 | View Code Duplication | case FIELD_TYPE_RECORD: |
|
586 | |||
587 | debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is record."); |
||
588 | |||
589 | $value = ustrcut(try_request('edit_' . $name), ustrlen(MAXINT)); |
||
590 | |||
591 | if (ustrlen($value) == 0) |
||
592 | { |
||
593 | $value = NULL; |
||
594 | } |
||
595 | |||
596 | if (!is_null($value) && !is_intvalue($value)) |
||
597 | { |
||
598 | debug_write_log(DEBUG_NOTICE, '[filter_fields_set] Invalid record ID.'); |
||
599 | } |
||
600 | else |
||
601 | { |
||
602 | if (!is_null($value) && ($value < 1 || $value > MAXINT)) |
||
603 | { |
||
604 | debug_write_log(DEBUG_NOTICE, '[filter_fields_set] Invalid integer range.'); |
||
605 | } |
||
606 | else |
||
607 | { |
||
608 | dal_query('filters/ffcreate.sql', |
||
609 | $filter_id, |
||
610 | $row['field_id'], |
||
611 | is_null($value) ? NULL : $value, |
||
612 | NULL); |
||
613 | } |
||
614 | } |
||
615 | |||
616 | break; |
||
617 | |||
618 | View Code Duplication | case FIELD_TYPE_DATE: |
|
619 | |||
620 | debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is date."); |
||
621 | |||
622 | $min_value = ustrcut(try_request('min_' . $name), ustrlen(get_date(SAMPLE_DATE))); |
||
623 | $max_value = ustrcut(try_request('max_' . $name), ustrlen(get_date(SAMPLE_DATE))); |
||
624 | |||
625 | if (ustrlen($min_value) == 0) |
||
626 | { |
||
627 | $min_date = NULL; |
||
628 | } |
||
629 | else |
||
630 | { |
||
631 | $min_date = ustr2date($min_value); |
||
632 | } |
||
633 | |||
634 | if (ustrlen($max_value) == 0) |
||
635 | { |
||
636 | $max_date = NULL; |
||
637 | } |
||
638 | else |
||
639 | { |
||
640 | $max_date = ustr2date($max_value); |
||
641 | } |
||
642 | |||
643 | if (!is_null($min_date) && $min_date == -1 || |
||
644 | !is_null($max_date) && $max_date == -1) |
||
645 | { |
||
646 | debug_write_log(DEBUG_NOTICE, '[filter_fields_set] At least one of range values is invalid.'); |
||
647 | } |
||
648 | else |
||
649 | { |
||
650 | if (!is_null($min_date) && !is_null($max_date) && ($min_date > $max_date)) |
||
651 | { |
||
652 | swap($min_date, $max_date); |
||
653 | } |
||
654 | |||
655 | dal_query('filters/ffcreate.sql', |
||
656 | $filter_id, |
||
657 | $row['field_id'], |
||
658 | is_null($min_date) ? NULL : $min_date, |
||
659 | is_null($max_date) ? NULL : $max_date); |
||
660 | } |
||
661 | |||
662 | break; |
||
663 | |||
664 | View Code Duplication | case FIELD_TYPE_DURATION: |
|
665 | |||
666 | debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is duration."); |
||
667 | |||
668 | $min_value = ustrcut(try_request('min_' . $name), ustrlen(time2ustr(MAX_FIELD_DURATION))); |
||
669 | $max_value = ustrcut(try_request('max_' . $name), ustrlen(time2ustr(MAX_FIELD_DURATION))); |
||
670 | |||
671 | if (ustrlen($min_value) == 0) |
||
672 | { |
||
673 | $min_time = NULL; |
||
674 | } |
||
675 | else |
||
676 | { |
||
677 | $min_time = ustr2time($min_value); |
||
678 | } |
||
679 | |||
680 | if (ustrlen($max_value) == 0) |
||
681 | { |
||
682 | $max_time = NULL; |
||
683 | } |
||
684 | else |
||
685 | { |
||
686 | $max_time = ustr2time($max_value); |
||
687 | } |
||
688 | |||
689 | if (!is_null($min_time) && $min_time == -1 || |
||
690 | !is_null($max_time) && $max_time == -1) |
||
691 | { |
||
692 | debug_write_log(DEBUG_NOTICE, '[filter_fields_set] At least one of range values is invalid.'); |
||
693 | } |
||
694 | else |
||
695 | { |
||
696 | if (!is_null($min_time) && !is_null($max_time) && ($min_time > $max_time)) |
||
697 | { |
||
698 | swap($min_time, $max_time); |
||
699 | } |
||
700 | |||
701 | dal_query('filters/ffcreate.sql', |
||
702 | $filter_id, |
||
703 | $row['field_id'], |
||
704 | is_null($min_time) ? NULL : $min_time, |
||
705 | is_null($max_time) ? NULL : $max_time); |
||
706 | } |
||
707 | |||
708 | break; |
||
709 | |||
710 | default: |
||
711 | debug_write_log(DEBUG_WARNING, '[filter_fields_set] Unknown field type = ' . $row['field_type']); |
||
712 | } |
||
713 | } |
||
714 | } |
||
715 | } |
||
716 | |||
717 | return NO_ERROR; |
||
718 | } |
||
719 | |||
721 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.