| Conditions | 148 |
| Paths | > 20000 |
| Total Lines | 398 |
| Lines | 21 |
| Ratio | 5.28 % |
| 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 |
||
| 545 | function parse($string) { |
||
| 546 | // Temporarily set locale to en_US in order to handle floats properly |
||
| 547 | $old = @setlocale(LC_ALL, 0); |
||
| 548 | @setlocale(LC_ALL, 'C'); |
||
| 549 | |||
| 550 | // PHP bug? Settings need to be refreshed in PHP4 |
||
| 551 | $this->print = new csstidy_print($this); |
||
| 552 | //$this->optimise = new csstidy_optimise($this); |
||
| 553 | |||
| 554 | $all_properties = & $GLOBALS['csstidy']['all_properties']; |
||
| 555 | $at_rules = & $GLOBALS['csstidy']['at_rules']; |
||
| 556 | $quoted_string_properties = & $GLOBALS['csstidy']['quoted_string_properties']; |
||
| 557 | |||
| 558 | $this->css = array(); |
||
| 559 | $this->print->input_css = $string; |
||
| 560 | $string = str_replace("\r\n", "\n", $string) . ' '; |
||
| 561 | $cur_comment = ''; |
||
| 562 | |||
| 563 | for ($i = 0, $size = strlen($string); $i < $size; $i++) { |
||
| 564 | if ($string[$i] === "\n" || $string[$i] === "\r") { |
||
| 565 | ++$this->line; |
||
| 566 | } |
||
| 567 | |||
| 568 | switch ($this->status) { |
||
| 569 | /* Case in at-block */ |
||
| 570 | case 'at': |
||
| 571 | if (csstidy::is_token($string, $i)) { |
||
| 572 | if ($string[$i] === '/' && @$string[$i + 1] === '*') { |
||
| 573 | $this->status = 'ic'; |
||
| 574 | ++$i; |
||
| 575 | $this->from[] = 'at'; |
||
| 576 | } elseif ($string[$i] === '{') { |
||
| 577 | $this->status = 'is'; |
||
| 578 | $this->at = $this->css_new_media_section($this->at); |
||
| 579 | $this->_add_token(AT_START, $this->at); |
||
| 580 | } elseif ($string[$i] === ',') { |
||
| 581 | $this->at = trim($this->at) . ','; |
||
| 582 | } elseif ($string[$i] === '\\') { |
||
| 583 | $this->at .= $this->_unicode($string, $i); |
||
| 584 | } |
||
| 585 | // fix for complicated media, i.e @media screen and (-webkit-min-device-pixel-ratio:1.5) |
||
| 586 | // '/' is included for ratios in Opera: (-o-min-device-pixel-ratio: 3/2) |
||
| 587 | View Code Duplication | elseif (in_array($string[$i], array('(', ')', ':', '.', '/'))) { |
|
| 588 | $this->at .= $string[$i]; |
||
| 589 | } |
||
| 590 | } else { |
||
| 591 | $lastpos = strlen($this->at) - 1; |
||
| 592 | if (!( (ctype_space($this->at[$lastpos]) || csstidy::is_token($this->at, $lastpos) && $this->at[$lastpos] === ',') && ctype_space($string[$i]))) { |
||
| 593 | $this->at .= $string[$i]; |
||
| 594 | } |
||
| 595 | } |
||
| 596 | break; |
||
| 597 | |||
| 598 | /* Case in-selector */ |
||
| 599 | case 'is': |
||
| 600 | if (csstidy::is_token($string, $i)) { |
||
| 601 | if ($string[$i] === '/' && @$string[$i + 1] === '*' && trim($this->selector) == '') { |
||
| 602 | $this->status = 'ic'; |
||
| 603 | ++$i; |
||
| 604 | $this->from[] = 'is'; |
||
| 605 | } elseif ($string[$i] === '@' && trim($this->selector) == '') { |
||
| 606 | // Check for at-rule |
||
| 607 | $this->invalid_at = true; |
||
| 608 | foreach ($at_rules as $name => $type) { |
||
| 609 | if (!strcasecmp(substr($string, $i + 1, strlen($name)), $name)) { |
||
| 610 | ($type === 'at') ? $this->at = '@' . $name : $this->selector = '@' . $name; |
||
| 611 | $this->status = $type; |
||
| 612 | $i += strlen($name); |
||
| 613 | $this->invalid_at = false; |
||
| 614 | } |
||
| 615 | } |
||
| 616 | |||
| 617 | if ($this->invalid_at) { |
||
| 618 | $this->selector = '@'; |
||
| 619 | $invalid_at_name = ''; |
||
| 620 | for ($j = $i + 1; $j < $size; ++$j) { |
||
| 621 | if (!ctype_alpha($string[$j])) { |
||
| 622 | break; |
||
| 623 | } |
||
| 624 | $invalid_at_name .= $string[$j]; |
||
| 625 | } |
||
| 626 | $this->log('Invalid @-rule: ' . $invalid_at_name . ' (removed)', 'Warning'); |
||
| 627 | } |
||
| 628 | } elseif (($string[$i] === '"' || $string[$i] === "'")) { |
||
| 629 | $this->cur_string[] = $string[$i]; |
||
| 630 | $this->status = 'instr'; |
||
| 631 | $this->str_char[] = $string[$i]; |
||
| 632 | $this->from[] = 'is'; |
||
| 633 | /* fixing CSS3 attribute selectors, i.e. a[href$=".mp3" */ |
||
| 634 | $this->quoted_string[] = ($string[$i - 1] == '=' ); |
||
| 635 | } elseif ($this->invalid_at && $string[$i] === ';') { |
||
| 636 | $this->invalid_at = false; |
||
| 637 | $this->status = 'is'; |
||
| 638 | } elseif ($string[$i] === '{') { |
||
| 639 | $this->status = 'ip'; |
||
| 640 | if($this->at == '') { |
||
| 641 | $this->at = $this->css_new_media_section(DEFAULT_AT); |
||
| 642 | } |
||
| 643 | $this->selector = $this->css_new_selector($this->at,$this->selector); |
||
| 644 | $this->_add_token(SEL_START, $this->selector); |
||
| 645 | $this->added = false; |
||
| 646 | } elseif ($string[$i] === '}') { |
||
| 647 | $this->_add_token(AT_END, $this->at); |
||
| 648 | $this->at = ''; |
||
| 649 | $this->selector = ''; |
||
| 650 | $this->sel_separate = array(); |
||
| 651 | } elseif ($string[$i] === ',') { |
||
| 652 | $this->selector = trim($this->selector) . ','; |
||
| 653 | $this->sel_separate[] = strlen($this->selector); |
||
| 654 | } elseif ($string[$i] === '\\') { |
||
| 655 | $this->selector .= $this->_unicode($string, $i); |
||
| 656 | View Code Duplication | } elseif ($string[$i] === '*' && @in_array($string[$i + 1], array('.', '#', '[', ':'))) { |
|
| 657 | // remove unnecessary universal selector, FS#147 |
||
| 658 | } else { |
||
| 659 | $this->selector .= $string[$i]; |
||
| 660 | } |
||
| 661 | } else { |
||
| 662 | $lastpos = strlen($this->selector) - 1; |
||
| 663 | if ($lastpos == -1 || !( (ctype_space($this->selector[$lastpos]) || csstidy::is_token($this->selector, $lastpos) && $this->selector[$lastpos] === ',') && ctype_space($string[$i]))) { |
||
| 664 | $this->selector .= $string[$i]; |
||
| 665 | } |
||
| 666 | else if (ctype_space($string[$i]) && $this->get_cfg('preserve_css') && !$this->get_cfg('merge_selectors')) { |
||
| 667 | $this->selector .= $string[$i]; |
||
| 668 | } |
||
| 669 | } |
||
| 670 | break; |
||
| 671 | |||
| 672 | /* Case in-property */ |
||
| 673 | case 'ip': |
||
| 674 | if (csstidy::is_token($string, $i)) { |
||
| 675 | if (($string[$i] === ':' || $string[$i] === '=') && $this->property != '') { |
||
| 676 | $this->status = 'iv'; |
||
| 677 | if (!$this->get_cfg('discard_invalid_properties') || csstidy::property_is_valid($this->property)) { |
||
| 678 | $this->property = $this->css_new_property($this->at,$this->selector,$this->property); |
||
| 679 | $this->_add_token(PROPERTY, $this->property); |
||
| 680 | } |
||
| 681 | } elseif ($string[$i] === '/' && @$string[$i + 1] === '*' && $this->property == '') { |
||
| 682 | $this->status = 'ic'; |
||
| 683 | ++$i; |
||
| 684 | $this->from[] = 'ip'; |
||
| 685 | View Code Duplication | } elseif ($string[$i] === '}') { |
|
| 686 | $this->explode_selectors(); |
||
| 687 | $this->status = 'is'; |
||
| 688 | $this->invalid_at = false; |
||
| 689 | $this->_add_token(SEL_END, $this->selector); |
||
| 690 | $this->selector = ''; |
||
| 691 | $this->property = ''; |
||
| 692 | } elseif ($string[$i] === ';') { |
||
| 693 | $this->property = ''; |
||
| 694 | } elseif ($string[$i] === '\\') { |
||
| 695 | $this->property .= $this->_unicode($string, $i); |
||
| 696 | } |
||
| 697 | // else this is dumb IE a hack, keep it |
||
| 698 | elseif ($this->property=='' AND !ctype_space($string[$i])) { |
||
| 699 | $this->property .= $string[$i]; |
||
| 700 | } |
||
| 701 | } |
||
| 702 | elseif (!ctype_space($string[$i])) { |
||
| 703 | $this->property .= $string[$i]; |
||
| 704 | } |
||
| 705 | break; |
||
| 706 | |||
| 707 | /* Case in-value */ |
||
| 708 | case 'iv': |
||
| 709 | $pn = (($string[$i] === "\n" || $string[$i] === "\r") && $this->property_is_next($string, $i + 1) || $i == strlen($string) - 1); |
||
| 710 | if ((csstidy::is_token($string, $i) || $pn) && (!($string[$i] == ',' && !ctype_space($string[$i+1])))) { |
||
| 711 | if ($string[$i] === '/' && @$string[$i + 1] === '*') { |
||
| 712 | $this->status = 'ic'; |
||
| 713 | ++$i; |
||
| 714 | $this->from[] = 'iv'; |
||
| 715 | } elseif (($string[$i] === '"' || $string[$i] === "'" || $string[$i] === '(')) { |
||
| 716 | $this->cur_string[] = $string[$i]; |
||
| 717 | $this->str_char[] = ($string[$i] === '(') ? ')' : $string[$i]; |
||
| 718 | $this->status = 'instr'; |
||
| 719 | $this->from[] = 'iv'; |
||
| 720 | $this->quoted_string[] = in_array(strtolower($this->property), $quoted_string_properties); |
||
| 721 | } elseif ($string[$i] === ',') { |
||
| 722 | $this->sub_value = trim($this->sub_value) . ','; |
||
| 723 | } elseif ($string[$i] === '\\') { |
||
| 724 | $this->sub_value .= $this->_unicode($string, $i); |
||
| 725 | } elseif ($string[$i] === ';' || $pn) { |
||
| 726 | if ($this->selector[0] === '@' && isset($at_rules[substr($this->selector, 1)]) && $at_rules[substr($this->selector, 1)] === 'iv') { |
||
| 727 | $this->status = 'is'; |
||
| 728 | |||
| 729 | switch ($this->selector) { |
||
| 730 | case '@charset': |
||
| 731 | /* Add quotes to charset */ |
||
| 732 | $this->sub_value_arr[] = '"' . trim($this->sub_value) . '"'; |
||
| 733 | $this->charset = $this->sub_value_arr[0]; |
||
| 734 | break; |
||
| 735 | case '@namespace': |
||
| 736 | /* Add quotes to namespace */ |
||
| 737 | $this->sub_value_arr[] = '"' . trim($this->sub_value) . '"'; |
||
| 738 | $this->namespace = implode(' ', $this->sub_value_arr); |
||
| 739 | break; |
||
| 740 | case '@import': |
||
| 741 | $this->sub_value = trim($this->sub_value); |
||
| 742 | |||
| 743 | if (empty($this->sub_value_arr)) { |
||
| 744 | // Quote URLs in imports only if they're not already inside url() and not already quoted. |
||
| 745 | if (substr($this->sub_value, 0, 4) != 'url(') { |
||
| 746 | if (!($this->sub_value[0] == substr($this->sub_value, -1) && in_array($this->sub_value[0], array("'", '"')))) { |
||
| 747 | $this->sub_value = '"' . $this->sub_value . '"'; |
||
| 748 | } |
||
| 749 | } |
||
| 750 | } |
||
| 751 | |||
| 752 | $this->sub_value_arr[] = $this->sub_value; |
||
| 753 | $this->import[] = implode(' ', $this->sub_value_arr); |
||
| 754 | break; |
||
| 755 | } |
||
| 756 | |||
| 757 | $this->sub_value_arr = array(); |
||
| 758 | $this->sub_value = ''; |
||
| 759 | $this->selector = ''; |
||
| 760 | $this->sel_separate = array(); |
||
| 761 | } else { |
||
| 762 | $this->status = 'ip'; |
||
| 763 | } |
||
| 764 | } elseif ($string[$i] !== '}') { |
||
| 765 | $this->sub_value .= $string[$i]; |
||
| 766 | } |
||
| 767 | if (($string[$i] === '}' || $string[$i] === ';' || $pn) && !empty($this->selector)) { |
||
| 768 | if ($this->at == '') { |
||
| 769 | $this->at = $this->css_new_media_section(DEFAULT_AT); |
||
| 770 | } |
||
| 771 | |||
| 772 | // case settings |
||
| 773 | if ($this->get_cfg('lowercase_s')) { |
||
| 774 | $this->selector = strtolower($this->selector); |
||
| 775 | } |
||
| 776 | $this->property = strtolower($this->property); |
||
| 777 | |||
| 778 | $this->optimise->subvalue(); |
||
| 779 | if ($this->sub_value != '') { |
||
| 780 | if (substr($this->sub_value, 0, 6) == 'format') { |
||
| 781 | $format_strings = csstidy::parse_string_list(substr($this->sub_value, 7, -1)); |
||
| 782 | if (!$format_strings) { |
||
| 783 | $this->sub_value = ""; |
||
| 784 | } |
||
| 785 | else { |
||
| 786 | $this->sub_value = "format("; |
||
| 787 | |||
| 788 | foreach ($format_strings as $format_string) { |
||
| 789 | $this->sub_value .= '"' . str_replace('"', '\\"', $format_string) . '",'; |
||
| 790 | } |
||
| 791 | |||
| 792 | $this->sub_value = substr($this->sub_value, 0, -1) . ")"; |
||
| 793 | } |
||
| 794 | } |
||
| 795 | if ($this->sub_value != '') { |
||
| 796 | $this->sub_value_arr[] = $this->sub_value; |
||
| 797 | } |
||
| 798 | $this->sub_value = ''; |
||
| 799 | } |
||
| 800 | |||
| 801 | $this->value = array_shift($this->sub_value_arr); |
||
| 802 | while(count($this->sub_value_arr)){ |
||
| 803 | //$this->value .= (substr($this->value,-1,1)==','?'':' ').array_shift($this->sub_value_arr); |
||
| 804 | $this->value .= ' '.array_shift($this->sub_value_arr); |
||
| 805 | } |
||
| 806 | |||
| 807 | $this->optimise->value(); |
||
| 808 | |||
| 809 | $valid = csstidy::property_is_valid($this->property); |
||
| 810 | if ((!$this->invalid_at || $this->get_cfg('preserve_css')) && (!$this->get_cfg('discard_invalid_properties') || $valid)) { |
||
| 811 | $this->css_add_property($this->at, $this->selector, $this->property, $this->value); |
||
| 812 | $this->_add_token(VALUE, $this->value); |
||
| 813 | $this->optimise->shorthands(); |
||
| 814 | } |
||
| 815 | if (!$valid) { |
||
| 816 | if ($this->get_cfg('discard_invalid_properties')) { |
||
| 817 | $this->log('Removed invalid property: ' . $this->property, 'Warning'); |
||
| 818 | } else { |
||
| 819 | $this->log('Invalid property in ' . strtoupper($this->get_cfg('css_level')) . ': ' . $this->property, 'Warning'); |
||
| 820 | } |
||
| 821 | } |
||
| 822 | |||
| 823 | $this->property = ''; |
||
| 824 | $this->sub_value_arr = array(); |
||
| 825 | $this->value = ''; |
||
| 826 | } |
||
| 827 | View Code Duplication | if ($string[$i] === '}') { |
|
| 828 | $this->explode_selectors(); |
||
| 829 | $this->_add_token(SEL_END, $this->selector); |
||
| 830 | $this->status = 'is'; |
||
| 831 | $this->invalid_at = false; |
||
| 832 | $this->selector = ''; |
||
| 833 | } |
||
| 834 | } elseif (!$pn) { |
||
| 835 | $this->sub_value .= $string[$i]; |
||
| 836 | |||
| 837 | if (ctype_space($string[$i]) || $string[$i] == ',') { |
||
| 838 | $this->optimise->subvalue(); |
||
| 839 | if ($this->sub_value != '') { |
||
| 840 | $this->sub_value_arr[] = $this->sub_value; |
||
| 841 | $this->sub_value = ''; |
||
| 842 | } |
||
| 843 | } |
||
| 844 | } |
||
| 845 | break; |
||
| 846 | |||
| 847 | /* Case in string */ |
||
| 848 | case 'instr': |
||
| 849 | $_str_char = $this->str_char[count($this->str_char)-1]; |
||
| 850 | $_cur_string = $this->cur_string[count($this->cur_string)-1]; |
||
| 851 | $temp_add = $string[$i]; |
||
| 852 | |||
| 853 | // Add another string to the stack. Strings can't be nested inside of quotes, only parentheses, but |
||
| 854 | // parentheticals can be nested more than once. |
||
| 855 | if ($_str_char === ")" && ($string[$i] === "(" || $string[$i] === '"' || $string[$i] === '\'') && !csstidy::escaped($string, $i)) { |
||
| 856 | $this->cur_string[] = $string[$i]; |
||
| 857 | $this->str_char[] = $string[$i] == "(" ? ")" : $string[$i]; |
||
| 858 | $this->from[] = 'instr'; |
||
| 859 | $this->quoted_string[] = !($string[$i] === "("); |
||
| 860 | continue 2; |
||
| 861 | } |
||
| 862 | |||
| 863 | if ($_str_char !== ")" && ($string[$i] === "\n" || $string[$i] === "\r") && !($string[$i - 1] === '\\' && !csstidy::escaped($string, $i - 1))) { |
||
| 864 | $temp_add = "\\A"; |
||
| 865 | $this->log('Fixed incorrect newline in string', 'Warning'); |
||
| 866 | } |
||
| 867 | |||
| 868 | $_cur_string .= $temp_add; |
||
| 869 | |||
| 870 | if ($string[$i] === $_str_char && !csstidy::escaped($string, $i)) { |
||
| 871 | $_quoted_string = array_pop($this->quoted_string); |
||
| 872 | |||
| 873 | $this->status = array_pop($this->from); |
||
| 874 | |||
| 875 | if (!preg_match('|[' . implode('', $GLOBALS['csstidy']['whitespace']) . ']|uis', $_cur_string) && $this->property !== 'content') { |
||
| 876 | if (!$_quoted_string) { |
||
| 877 | if ($_str_char !== ')') { |
||
| 878 | // Convert properties like |
||
| 879 | // font-family: 'Arial'; |
||
| 880 | // to |
||
| 881 | // font-family: Arial; |
||
| 882 | // or |
||
| 883 | // url("abc") |
||
| 884 | // to |
||
| 885 | // url(abc) |
||
| 886 | $_cur_string = substr($_cur_string, 1, -1); |
||
| 887 | } |
||
| 888 | } else { |
||
| 889 | $_quoted_string = false; |
||
| 890 | } |
||
| 891 | } |
||
| 892 | |||
| 893 | array_pop($this->cur_string); |
||
| 894 | array_pop($this->str_char); |
||
| 895 | |||
| 896 | if ($_str_char === ")") { |
||
| 897 | $_cur_string = "(" . trim(substr($_cur_string, 1, -1)) . ")"; |
||
| 898 | } |
||
| 899 | |||
| 900 | if ($this->status === 'iv') { |
||
| 901 | if (!$_quoted_string){ |
||
| 902 | if (strpos($_cur_string,',')!==false) |
||
| 903 | // we can on only remove space next to ',' |
||
| 904 | $_cur_string = implode(',',array_map('trim',explode(',',$_cur_string))); |
||
| 905 | // and multiple spaces (too expensive) |
||
| 906 | if (strpos($_cur_string,' ')!==false) |
||
| 907 | $_cur_string = preg_replace(",\s+,"," ",$_cur_string); |
||
| 908 | } |
||
| 909 | $this->sub_value .= $_cur_string; |
||
| 910 | } elseif ($this->status === 'is') { |
||
| 911 | $this->selector .= $_cur_string; |
||
| 912 | } elseif ($this->status === 'instr') { |
||
| 913 | $this->cur_string[count($this->cur_string)-1] .= $_cur_string; |
||
| 914 | } |
||
| 915 | } |
||
| 916 | else { |
||
| 917 | $this->cur_string[count($this->cur_string)-1] = $_cur_string; |
||
| 918 | } |
||
| 919 | break; |
||
| 920 | |||
| 921 | /* Case in-comment */ |
||
| 922 | case 'ic': |
||
| 923 | if ($string[$i] === '*' && $string[$i + 1] === '/') { |
||
| 924 | $this->status = array_pop($this->from); |
||
| 925 | $i++; |
||
| 926 | $this->_add_token(COMMENT, $cur_comment); |
||
| 927 | $cur_comment = ''; |
||
| 928 | } else { |
||
| 929 | $cur_comment .= $string[$i]; |
||
| 930 | } |
||
| 931 | break; |
||
| 932 | } |
||
| 933 | } |
||
| 934 | |||
| 935 | $this->optimise->postparse(); |
||
| 936 | |||
| 937 | $this->print->_reset(); |
||
| 938 | |||
| 939 | @setlocale(LC_ALL, $old); // Set locale back to original setting |
||
| 940 | |||
| 941 | return!(empty($this->css) && empty($this->import) && empty($this->charset) && empty($this->tokens) && empty($this->namespace)); |
||
| 942 | } |
||
| 943 | |||
| 1249 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: