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 | 35 | 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) { |
|
| 61 | |||
| 62 | 1 | public function before($content) { |
|
| 68 | |||
| 69 | 1 | public function clear() { |
|
| 75 | |||
| 76 | 7 | /** |
|
| 77 | 7 | * Get the count of matched elements. |
|
| 78 | * |
||
| 79 | * @return int Returns the count of matched elements. |
||
| 80 | */ |
||
| 81 | public function count() { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Format/beautify a DOM. |
||
| 87 | * |
||
| 88 | * @param pQuery\DomNode $dom The dom to format. |
||
| 89 | * @param array $options Extra formatting options. See {@link pQuery\HtmlFormatter::$options}. |
||
| 90 | * @return bool Returns `true` on sucess and `false` on failure. |
||
| 91 | */ |
||
| 92 | 3 | // public static function format($dom, $options = array()) { |
|
| 93 | 3 | // $formatter = new pQuery\HtmlFormatter($options); |
|
| 94 | // return $formatter->format($dom); |
||
| 95 | // } |
||
| 96 | 2 | ||
| 97 | 2 | public function getIterator() { |
|
| 100 | 2 | ||
| 101 | 2 | public function hasClass($classname) { |
|
| 108 | 3 | ||
| 109 | 3 | public function html($value = null) { |
|
| 120 | 2 | ||
| 121 | 2 | public function offsetExists($offset) { |
|
| 124 | 2 | ||
| 125 | public function offsetGet($offset) { |
||
| 128 | |||
| 129 | 1 | public function offsetSet($offset, $value) { |
|
| 137 | 1 | ||
| 138 | 1 | public function offsetUnset($offset) { |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Query a file or url. |
||
| 147 | 9 | * |
|
| 148 | 9 | * @param string $path The path to the url. |
|
| 149 | 9 | * @param resource $context A context suitable to be passed into {@link file_get_contents} |
|
| 150 | * @return pQuery\DomNode Returns the root dom node for the html file. |
||
| 151 | */ |
||
| 152 | public static function parseFile($path, $context = null) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | 36 | * Query a string of html. |
|
| 159 | 36 | * |
|
| 160 | 36 | * @param string $html |
|
| 161 | * @return pQuery\DomNode Returns the root dom node for the html string. |
||
| 162 | */ |
||
| 163 | 1 | public static function parseStr($html) { |
|
| 167 | 1 | ||
| 168 | public function prepend($content = null) { |
||
| 174 | 4 | ||
| 175 | 4 | public function prop($name, $value = null) { |
|
| 186 | 1 | ||
| 187 | 1 | public function remove($selector = null) { |
|
| 196 | 1 | ||
| 197 | public function removeAttr($name) { |
||
| 203 | 2 | ||
| 204 | public function removeClass($classname) { |
||
| 210 | 1 | ||
| 211 | public function replaceWith($content) { |
||
| 217 | 1 | ||
| 218 | 1 | public function tagName($value = null) { |
|
| 226 | 3 | ||
| 227 | 3 | public function text($value = null) { |
|
| 238 | |||
| 239 | 1 | public function toggleClass($classname, $switch = null) { |
|
| 246 | 1 | ||
| 247 | public function unwrap() { |
||
| 253 | 5 | ||
| 254 | 5 | public function val($value = null) { |
|
| 265 | 2 | ||
| 266 | public function wrap($wrapping_element) { |
||
| 272 | 1 | ||
| 273 | public function wrapInner($wrapping_element) { |
||
| 279 | } |
||
| 280 |
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.