Complex classes like pQuery 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 pQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class pQuery implements ArrayAccess, Countable, IteratorAggregate, IQuery { |
||
|
|||
16 | /// Properties /// |
||
17 | |||
18 | /** |
||
19 | * @var IQuery[] |
||
20 | */ |
||
21 | protected $nodes = array(); |
||
22 | |||
23 | /// Methods /// |
||
24 | |||
25 | public function __construct($nodes = array()) { |
||
28 | |||
29 | public function addClass($classname) { |
||
35 | |||
36 | public function after($content) { |
||
42 | |||
43 | public function append($content) { |
||
49 | |||
50 | public function attr($name, $value = null) { |
||
61 | |||
62 | public function before($content) { |
||
68 | |||
69 | public function clear() { |
||
75 | |||
76 | public function count() { |
||
79 | |||
80 | /** |
||
81 | * Format/beautify a DOM. |
||
82 | * |
||
83 | * @param pQuery\DomNode $dom The dom to format. |
||
84 | * @param array $options Extra formatting options. See {@link pQuery\HtmlFormatter::$options}. |
||
85 | * @return bool Returns `true` on sucess and `false` on failure. |
||
86 | */ |
||
87 | // public static function format($dom, $options = array()) { |
||
88 | // $formatter = new pQuery\HtmlFormatter($options); |
||
89 | // return $formatter->format($dom); |
||
90 | // } |
||
91 | |||
92 | public function getIterator() { |
||
95 | |||
96 | public function hasClass($classname) { |
||
103 | |||
104 | public function html($value = null) { |
||
115 | |||
116 | public function offsetExists($offset) { |
||
119 | |||
120 | public function offsetGet($offset) { |
||
123 | |||
124 | public function offsetSet($offset, $value) { |
||
132 | |||
133 | public function offsetUnset($offset) { |
||
139 | |||
140 | /** |
||
141 | * Query a file or url. |
||
142 | * |
||
143 | * @param string $path The path to the url. |
||
144 | * @param resource $context A context suitable to be passed into {@link file_get_contents} |
||
145 | * @return pQuery\DomNode Returns the root dom node for the html file. |
||
146 | */ |
||
147 | public static function parseFile($path, $context = null) { |
||
151 | |||
152 | /** |
||
153 | * Query a string of html. |
||
154 | * |
||
155 | * @param string $html |
||
156 | * @return pQuery\DomNode Returns the root dom node for the html string. |
||
157 | */ |
||
158 | public static function parseStr($html) { |
||
162 | |||
163 | public function prepend($content = null) { |
||
169 | |||
170 | public function prop($name, $value = null) { |
||
181 | |||
182 | public function remove($selector = null) { |
||
191 | |||
192 | public function removeAttr($name) { |
||
198 | |||
199 | public function removeClass($classname) { |
||
205 | |||
206 | public function replaceWith($content) { |
||
212 | |||
213 | public function tagName($value = null) { |
||
221 | |||
222 | public function text($value = null) { |
||
233 | |||
234 | public function toggleClass($classname, $switch = null) { |
||
241 | |||
242 | public function unwrap() { |
||
248 | |||
249 | public function val($value = null) { |
||
260 | |||
261 | public function wrap($wrapping_element) { |
||
267 | |||
268 | public function wrapInner($wrapping_element) { |
||
274 | } |
||
275 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.