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