Complex classes like Selecta often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Selecta, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Selecta |
||
6 | { |
||
7 | public static $single_tags = array('img', 'br', 'hr', 'input'); |
||
8 | public static $pseudo_classes = array('disabled', 'checked'); |
||
9 | public static $meta_map = array('.'=>'class', '#'=>'id'); |
||
10 | |||
11 | public static function build($selector, $contents='', $open_tags=true, $close_tags=true) |
||
12 | { |
||
13 | $parts = explode(' ', $selector); |
||
14 | if (count($parts)) { |
||
15 | $parts = array_reverse($parts); |
||
16 | foreach($parts as $part) { |
||
17 | $contents = self::tagify($part, $contents, $open_tags, $close_tags); |
||
18 | } |
||
19 | } |
||
20 | return $contents; |
||
21 | } |
||
22 | |||
23 | public static function wrap($selector, $contents='') |
||
24 | { |
||
25 | return self::build($selector, $contents, true, true); |
||
26 | } |
||
27 | |||
28 | public static function open($selector) |
||
29 | { |
||
30 | return self::build($selector, '', true, false); |
||
31 | } |
||
32 | |||
33 | public static function close($selector) |
||
34 | { |
||
35 | return self::build($selector, '', false, true); |
||
36 | } |
||
37 | |||
38 | private static function tagify($selector='', $contents='', $open_tags=true, $close_tags=true) |
||
39 | { |
||
40 | $attrs = array(); |
||
41 | |||
42 | // replace dots within attribute selectors |
||
43 | $selector = self::sanitise_attribute_metas($selector); |
||
44 | |||
45 | $metas = '\.\#\[\:'; |
||
46 | $pattern = '/(['.$metas.'])([^'.$metas.']*)?/'; |
||
47 | preg_match_all($pattern, $selector, $matches, PREG_SET_ORDER); |
||
48 | |||
49 | if (count($matches)) { |
||
50 | foreach($matches as $match) { |
||
51 | $attrs = self::build_attributes($match[1], $match[2], $attrs); |
||
52 | } |
||
53 | |||
54 | // reduce selector to just tag name |
||
55 | $parts = preg_split('/['.$metas.']/', $selector); |
||
56 | $selector = $parts[0]; |
||
57 | } |
||
58 | |||
59 | return self::build_tag($selector, $attrs, $contents, $open_tags, $close_tags); |
||
60 | } |
||
61 | |||
62 | private static function build_attributes($meta_char, $value, $attrs) |
||
63 | { |
||
64 | $key = false; |
||
65 | if (isset(self::$meta_map[$meta_char])) { |
||
66 | $key = self::$meta_map[$meta_char]; |
||
67 | }else{ |
||
68 | switch ($meta_char) { |
||
69 | |||
70 | // Attribute selectors |
||
71 | case '[': |
||
72 | list($key, $value) = self::build_attribute_selector_attribute($value); |
||
73 | break; |
||
74 | |||
75 | // Pseudo-class selectors |
||
76 | case ':': |
||
77 | list($key, $value) = self::build_pseudo_class_attribute($value); |
||
78 | break; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | if ($key){ |
||
83 | if (isset($attrs[$key])) { |
||
84 | $attrs[$key] .= ' '.$value; |
||
85 | }else{ |
||
86 | $attrs[$key] = $value; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | return $attrs; |
||
91 | } |
||
92 | |||
93 | private static function build_attribute_selector_attribute($value) |
||
108 | |||
109 | private static function build_pseudo_class_attribute($pclass='') |
||
110 | { |
||
111 | if (in_array($pclass, self::$pseudo_classes)) { |
||
112 | return array($pclass, false); |
||
113 | } |
||
114 | |||
117 | |||
118 | private static function build_tag($name, $attributes=array(), $contents='', $open_tag=true, $close_tag=true) |
||
138 | |||
139 | private static function open_tag($name, $attributes=array()) |
||
158 | |||
159 | private static function close_tag($name) |
||
163 | |||
164 | private static function html($s, $quotes=false, $double_encode=false) |
||
171 | |||
172 | private static function sanitise_attribute_metas($selector) |
||
189 | |||
190 | private static function unsanitise_attribute_metas($string) |
||
194 | |||
195 | private static function metas_from() |
||
201 | |||
202 | private static function metas_to() |
||
208 | |||
209 | } |