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) { |
|
| 61 | |||
| 62 | 1 | public function before($content) { |
|
| 68 | |||
| 69 | 1 | public function clear() { |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Get the count of matched elements. |
||
| 78 | * |
||
| 79 | * @return int Returns the count of matched elements. |
||
| 80 | */ |
||
| 81 | 8 | public function count() { |
|
| 82 | 8 | return count($this->nodes); |
|
| 83 | } |
||
| 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 | // public static function format($dom, $options = array()) { |
||
| 93 | // $formatter = new pQuery\HtmlFormatter($options); |
||
| 94 | // return $formatter->format($dom); |
||
| 95 | // } |
||
| 96 | |||
| 97 | 3 | public function getIterator() { |
|
| 98 | 3 | return new ArrayIterator($this->nodes); |
|
| 99 | } |
||
| 100 | |||
| 101 | 2 | public function hasClass($classname) { |
|
| 102 | 2 | foreach ($this->nodes as $node) { |
|
| 103 | 2 | if ($node->hasClass($classname)) |
|
| 104 | 2 | return true; |
|
| 105 | 2 | } |
|
| 106 | 2 | return false; |
|
| 107 | } |
||
| 108 | |||
| 109 | 3 | public function html($value = null) { |
|
| 110 | 3 | if (empty($this->nodes) && $value === null) |
|
| 111 | 3 | return ''; |
|
| 112 | |||
| 113 | 3 | foreach ($this->nodes as $node) { |
|
| 114 | 3 | if ($value === null) |
|
| 115 | 3 | return $node->html(); |
|
| 116 | 1 | $node->html($value); |
|
| 117 | 1 | } |
|
| 118 | 1 | return $this; |
|
| 119 | } |
||
| 120 | |||
| 121 | 1 | public function offsetExists($offset) { |
|
| 122 | 1 | return isset($this->nodes[$offset]); |
|
| 123 | } |
||
| 124 | |||
| 125 | 2 | public function offsetGet($offset) { |
|
| 128 | |||
| 129 | 2 | public function offsetSet($offset, $value) { |
|
| 130 | |||
| 131 | 2 | if (is_null($offset) || !isset($this->nodes[$offset])) { |
|
| 132 | 1 | throw new \BadMethodCallException("You are not allowed to add new nodes to the pQuery object."); |
|
| 133 | } else { |
||
| 134 | 1 | $this->nodes[$offset]->replaceWith($value); |
|
| 135 | } |
||
| 136 | 1 | } |
|
| 137 | |||
| 138 | 1 | public function offsetUnset($offset) { |
|
| 139 | 1 | if (isset($this->nodes[$offset])) { |
|
| 140 | 1 | $this->nodes[$offset]->remove(); |
|
| 141 | 1 | unset($this->nodes[$offset]); |
|
| 142 | 1 | } |
|
| 143 | 1 | } |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Query a file or url. |
||
| 147 | * |
||
| 148 | * @param string $path The path to the url. |
||
| 149 | * @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 | 9 | public static function parseFile($path, $context = null) { |
|
| 153 | 9 | $html_str = file_get_contents($path, false, $context); |
|
| 154 | 9 | return static::parseStr($html_str); |
|
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Query a string of html. |
||
| 159 | * |
||
| 160 | * @param string $html |
||
| 161 | * @return pQuery\DomNode Returns the root dom node for the html string. |
||
| 162 | */ |
||
| 163 | 37 | public static function parseStr($html) { |
|
| 164 | 37 | $parser = new pQuery\Html5Parser($html); |
|
| 165 | 37 | return $parser->root; |
|
| 166 | } |
||
| 167 | |||
| 168 | 1 | public function prepend($content = null) { |
|
| 169 | 1 | foreach ($this->nodes as $node) { |
|
| 170 | 1 | $node->prepend($content); |
|
| 171 | 1 | } |
|
| 172 | 1 | return $this; |
|
| 173 | } |
||
| 174 | |||
| 175 | 4 | public function prop($name, $value = null) { |
|
| 186 | |||
| 187 | 1 | public function remove($selector = null) { |
|
| 188 | 1 | foreach ($this->nodes as $node) { |
|
| 189 | 1 | $node->remove($selector); |
|
| 190 | 1 | } |
|
| 191 | 1 | if ($selector === null) |
|
| 192 | 1 | $this->nodes = array(); |
|
| 193 | |||
| 194 | 1 | return $this; |
|
| 195 | } |
||
| 196 | |||
| 197 | 1 | public function removeAttr($name) { |
|
| 198 | 1 | foreach ($this->nodes as $node) { |
|
| 199 | 1 | $node->removeAttr($name); |
|
| 200 | 1 | } |
|
| 201 | 1 | return $this; |
|
| 202 | } |
||
| 203 | |||
| 204 | 2 | public function removeClass($classname) { |
|
| 205 | 2 | foreach ($this->nodes as $node) { |
|
| 206 | 2 | $node->removeClass($classname); |
|
| 207 | 2 | } |
|
| 208 | 2 | return $this; |
|
| 209 | } |
||
| 210 | |||
| 211 | 1 | public function replaceWith($content) { |
|
| 212 | 1 | foreach ($this->nodes as &$node) { |
|
| 213 | 1 | $node = $node->replaceWith($content); |
|
| 214 | 1 | } |
|
| 215 | 1 | return $this; |
|
| 216 | } |
||
| 217 | |||
| 218 | 1 | public function tagName($value = null) { |
|
| 219 | 1 | foreach ($this->nodes as $node) { |
|
| 220 | 1 | if ($value === null) |
|
| 221 | 1 | return $node->tagName(); |
|
| 222 | 1 | $node->tagName($value); |
|
| 223 | 1 | } |
|
| 224 | 1 | return $this; |
|
| 225 | } |
||
| 226 | |||
| 227 | 3 | public function text($value = null) { |
|
| 238 | |||
| 239 | 1 | public function toggleClass($classname, $switch = null) { |
|
| 246 | |||
| 247 | 1 | public function unwrap() { |
|
| 248 | 1 | foreach ($this->nodes as $node) { |
|
| 249 | 1 | $node->unwrap(); |
|
| 250 | 1 | } |
|
| 251 | 1 | return $this; |
|
| 252 | } |
||
| 253 | |||
| 254 | 5 | public function val($value = null) { |
|
| 265 | |||
| 266 | 2 | public function wrap($wrapping_element) { |
|
| 267 | 2 | foreach ($this->nodes as $node) { |
|
| 268 | 2 | $node->wrap($wrapping_element); |
|
| 269 | 2 | } |
|
| 270 | 2 | return $this; |
|
| 271 | } |
||
| 272 | |||
| 273 | 1 | public function wrapInner($wrapping_element) { |
|
| 274 | 1 | foreach ($this->nodes as $node) { |
|
| 275 | 1 | $node->wrapInner($wrapping_element); |
|
| 276 | 1 | } |
|
| 277 | 1 | return $this; |
|
| 278 | } |
||
| 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.