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