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, IteratorAggregate, IQuery { |
||
|
|
|||
| 16 | /// Properties /// |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var IQuery[] |
||
| 20 | */ |
||
| 21 | protected $nodes = array(); |
||
| 22 | |||
| 23 | /// Methods /// |
||
| 24 | |||
| 25 | 36 | public function __construct($nodes = array()) { |
|
| 28 | |||
| 29 | 3 | public function addClass($classname) { |
|
| 35 | |||
| 36 | 1 | public function after($content) { |
|
| 42 | |||
| 43 | 1 | public function append($content) { |
|
| 49 | |||
| 50 | 7 | public function attr($name, $value = null) { |
|
| 63 | 1 | ||
| 64 | 1 | public function before($content) { |
|
| 70 | 1 | ||
| 71 | 1 | public function clear() { |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Get the count of matched elements. |
||
| 80 | * |
||
| 81 | 8 | * @return int Returns the count of matched elements. |
|
| 82 | 8 | */ |
|
| 83 | public function count() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Format/beautify a DOM. |
||
| 89 | * |
||
| 90 | * @param pQuery\DomNode $dom The dom to format. |
||
| 91 | * @param array $options Extra formatting options. See {@link pQuery\HtmlFormatter::$options}. |
||
| 92 | * @return bool Returns `true` on sucess and `false` on failure. |
||
| 93 | */ |
||
| 94 | // public static function format($dom, $options = array()) { |
||
| 95 | // $formatter = new pQuery\HtmlFormatter($options); |
||
| 96 | // return $formatter->format($dom); |
||
| 97 | 3 | // } |
|
| 98 | 3 | ||
| 99 | public function getIterator() { |
||
| 102 | 2 | ||
| 103 | 2 | public function hasClass($classname) { |
|
| 111 | 3 | ||
| 112 | public function html($value = null) { |
||
| 125 | 2 | ||
| 126 | 2 | public function offsetExists($offset) { |
|
| 129 | 2 | ||
| 130 | public function offsetGet($offset) { |
||
| 133 | |||
| 134 | 1 | public function offsetSet($offset, $value) { |
|
| 142 | 1 | ||
| 143 | 1 | public function offsetUnset($offset) { |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Query a file or url. |
||
| 152 | 9 | * |
|
| 153 | 9 | * @param string $path The path to the url. |
|
| 154 | 9 | * @param resource $context A context suitable to be passed into {@link file_get_contents} |
|
| 155 | * @return pQuery\DomNode Returns the root dom node for the html file. |
||
| 156 | */ |
||
| 157 | public static function parseFile($path, $context = null) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | 37 | * Query a string of html. |
|
| 164 | 37 | * |
|
| 165 | 37 | * @param string $html |
|
| 166 | * @return pQuery\DomNode Returns the root dom node for the html string. |
||
| 167 | */ |
||
| 168 | 1 | public static function parseStr($html) { |
|
| 172 | 1 | ||
| 173 | public function prepend($content = null) { |
||
| 179 | 4 | ||
| 180 | 4 | public function prop($name, $value = null) { |
|
| 193 | |||
| 194 | 1 | public function remove($selector = null) { |
|
| 204 | 2 | ||
| 205 | 2 | public function removeAttr($name) { |
|
| 211 | 1 | ||
| 212 | 1 | public function removeClass($classname) { |
|
| 218 | 1 | ||
| 219 | 1 | public function replaceWith($content) { |
|
| 225 | |||
| 226 | public function tagName($value = null) { |
||
| 235 | 2 | ||
| 236 | 2 | public function text($value = null) { |
|
| 249 | 1 | ||
| 250 | 1 | public function toggleClass($classname, $switch = null) { |
|
| 257 | |||
| 258 | 5 | public function unwrap() { |
|
| 264 | |||
| 265 | public function val($value = null) { |
||
| 278 | |||
| 279 | public function wrap($wrapping_element) { |
||
| 285 | |||
| 286 | public function wrapInner($wrapping_element) { |
||
| 292 | } |
||
| 293 |
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.