Complex classes like Driver_Simple 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 Driver_Simple, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Driver_Simple extends Driver { |
||
21 | |||
22 | /** |
||
23 | * The name of the driver |
||
24 | * @var string |
||
25 | */ |
||
26 | public $name = 'simple'; |
||
27 | |||
28 | /** |
||
29 | * Wait time is meaningless in simple driver as it does not support javascript |
||
30 | * @var integer |
||
31 | */ |
||
32 | public $default_wait_time = 10; |
||
33 | |||
34 | /** |
||
35 | * The raw html content of the page |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $_content; |
||
39 | |||
40 | /** |
||
41 | * The DOMDocument of the current page |
||
42 | * @var DOMDocument |
||
43 | */ |
||
44 | protected $_dom; |
||
45 | |||
46 | /** |
||
47 | * The DOMXpath object for finding elements on the page |
||
48 | * @var DOMXpath |
||
49 | */ |
||
50 | protected $_xpath; |
||
51 | |||
52 | /** |
||
53 | * Object for handling forms on the page |
||
54 | * @var Driver_Simple_Forms |
||
55 | */ |
||
56 | protected $_forms; |
||
57 | |||
58 | /** |
||
59 | * Environment object for handling backups of environment state |
||
60 | * @var Environment |
||
61 | */ |
||
62 | protected $_environment; |
||
63 | |||
64 | /** |
||
65 | * Driver_Simple_RequestFactory object for opening new pages |
||
66 | * @var Driver_Simple_RequestFactory |
||
67 | */ |
||
68 | protected $_request_factory; |
||
69 | |||
70 | 57 | function __construct() |
|
81 | |||
82 | /** |
||
83 | * Getter / Setter for the request factory object for the driver |
||
84 | * @param Driver_Simple_RequestFactory $request_factory |
||
85 | * @return Driver_Simple_RequestFactory|Driver_Simple |
||
86 | */ |
||
87 | 18 | public function request_factory(Driver_Simple_RequestFactory $request_factory = NULL) |
|
88 | { |
||
89 | 18 | if ($request_factory !== NULL) |
|
90 | { |
||
91 | 14 | $this->_request_factory = $request_factory; |
|
92 | 14 | return $this; |
|
93 | } |
||
94 | |||
95 | 9 | return $this->_request_factory; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Getter for the current environment |
||
100 | * @return Environment |
||
101 | */ |
||
102 | 7 | public function environment() |
|
106 | |||
107 | /** |
||
108 | * Restore the environment |
||
109 | * @return Driver_Simple $this |
||
110 | */ |
||
111 | 1 | public function clear() |
|
112 | { |
||
113 | 1 | $this->environment()->restore(); |
|
114 | |||
115 | 1 | return $this; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Getter / Setter of the raw content html |
||
120 | * @param string $content |
||
121 | * @return string|Driver_Simple |
||
122 | */ |
||
123 | 48 | public function content($content = NULL) |
|
134 | |||
135 | /** |
||
136 | * Initialze the dom, xpath and forms objects, based on the content string |
||
137 | */ |
||
138 | 48 | public function initialize() |
|
145 | |||
146 | /** |
||
147 | * Getter the current forms object |
||
148 | * @return Driver_Simple_Forms |
||
149 | */ |
||
150 | 7 | public function forms() |
|
151 | { |
||
152 | 7 | return $this->_forms; |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Getter for the current xpath object for the page |
||
157 | * @return DOMXpath |
||
158 | */ |
||
159 | 20 | public function xpath() |
|
160 | { |
||
161 | 20 | return $this->_xpath; |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * Get the DOMElement for the current id, or root if no id is given |
||
166 | * @param string $id |
||
167 | * @return DOMElement |
||
168 | */ |
||
169 | 15 | public function dom($id = NULL) |
|
170 | { |
||
171 | 15 | return $id ? $this->xpath()->find($id) : $this->_dom; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * Initiate a get request to a given uri |
||
176 | * @param string $uri |
||
177 | * @param array $query an array for the http query |
||
178 | * @return Driver_Simple $this |
||
179 | */ |
||
180 | 5 | public function get($uri, array $query = array()) |
|
184 | |||
185 | /** |
||
186 | * Initiate a post request to a given uri |
||
187 | * @param string $uri |
||
188 | * @param array $query |
||
189 | * @param array $post |
||
190 | * @param array $files set the $_FILES array appropriately |
||
191 | * @return Driver_Simple $this |
||
192 | */ |
||
193 | 1 | public function post($uri, array $query = array(), array $post = array(), array $files = array()) |
|
194 | { |
||
195 | 1 | return $this->request('POST', $uri, $query, $post, $files); |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * Initiate a request with a custom method |
||
200 | * @param string $method |
||
201 | * @param string $uri |
||
202 | * @param array $query |
||
203 | * @param array $post |
||
204 | * @param array $files set the $_FILES array |
||
205 | * @return Driver_Simple $this |
||
206 | */ |
||
207 | 5 | public function request($method, $uri, array $query = array(), array $post = array(), array $files = array()) |
|
233 | |||
234 | /** |
||
235 | * NODE GETTERS |
||
236 | * ===================================== |
||
237 | */ |
||
238 | |||
239 | /** |
||
240 | * Get the tag name of a Node with id. e.g. DIV, SPAN ... |
||
241 | * @param string $id |
||
242 | * @return string |
||
243 | */ |
||
244 | 3 | public function tag_name($id) |
|
245 | { |
||
246 | 3 | return $this->dom($id)->tagName; |
|
247 | } |
||
248 | |||
249 | /** |
||
250 | * Get the attribute of a Node with id. If the attribute does not exist, returns NULL |
||
251 | * @param string $id |
||
252 | * @param string $name |
||
253 | * @return string |
||
254 | */ |
||
255 | 4 | public function attribute($id, $name) |
|
256 | { |
||
257 | 4 | $node = $this->dom($id); |
|
258 | |||
259 | 4 | return $node->hasAttribute($name) ? $node->getAttribute($name) : NULL; |
|
260 | } |
||
261 | |||
262 | /** |
||
263 | * Return the raw html of a Node with id, along with all of its children. |
||
264 | * @param string $id |
||
265 | * @return string |
||
266 | */ |
||
267 | 3 | public function html($id) |
|
268 | { |
||
269 | 3 | if ($id === NULL) |
|
270 | return $this->dom()->saveHTML(); |
||
271 | |||
272 | 3 | $node = $this->dom($id); |
|
273 | |||
274 | 3 | return $node->ownerDocument->saveXml($node); |
|
275 | } |
||
276 | |||
277 | /** |
||
278 | * Return the text of a Node with id, with all the spaces collapsed, similar to browser rendering. |
||
279 | * @param string $id |
||
280 | * @return string |
||
281 | */ |
||
282 | 3 | public function text($id) |
|
283 | { |
||
284 | 3 | $text = $this->dom($id)->textContent; |
|
285 | 3 | $text = preg_replace('/[ \s\f\n\r\t\v ]+/u', ' ', $text); |
|
286 | |||
287 | 3 | return trim($text); |
|
288 | } |
||
289 | |||
290 | /** |
||
291 | * Return the value of a Node of a form element, e.g. INPUT, TEXTAREA or SELECT |
||
292 | * @param string $id |
||
293 | * @return string |
||
294 | */ |
||
295 | 5 | public function value($id) |
|
296 | { |
||
297 | 5 | return $this->forms()->get_value($id); |
|
298 | } |
||
299 | |||
300 | /** |
||
301 | * Check if a Node with id is visible. |
||
302 | * A Node is considered hidden if it has a "display:none" inline style or its a script or head tag. |
||
303 | * @param string $id |
||
304 | * @return boolean |
||
305 | */ |
||
306 | 2 | public function is_visible($id) |
|
307 | { |
||
308 | 2 | $node = $this->dom($id); |
|
309 | |||
310 | 2 | $hidden_nodes = $this->xpath()->query("./ancestor-or-self::*[contains(@style, 'display:none') or contains(@style, 'display: none') or name()='script' or name()='head']", $node); |
|
311 | 2 | return $hidden_nodes->length == 0; |
|
312 | } |
||
313 | |||
314 | /** |
||
315 | * Check if a Node with id of an option element is selected |
||
316 | * @param string $id |
||
317 | * @return boolean |
||
318 | */ |
||
319 | 2 | public function is_selected($id) |
|
320 | { |
||
321 | 2 | return (bool) $this->dom($id)->getAttribute('selected'); |
|
322 | } |
||
323 | |||
324 | /** |
||
325 | * Check if a Node with id of an input element (radio or checkbox) is checked |
||
326 | * @param string $id |
||
327 | * @return boolean |
||
328 | */ |
||
329 | 2 | public function is_checked($id) |
|
330 | { |
||
331 | 2 | return (bool) $this->dom($id)->getAttribute('checked'); |
|
332 | } |
||
333 | |||
334 | /** |
||
335 | * Set the value of a Node with id of a form element |
||
336 | * @param string $id |
||
337 | * @param string $value |
||
338 | */ |
||
339 | 4 | public function set($id, $value) |
|
340 | { |
||
341 | 4 | $this->forms()->set_value($id, $value); |
|
342 | 4 | } |
|
343 | |||
344 | /** |
||
345 | * Set the option value that is selected of a Node of a select element |
||
346 | * @param string $id |
||
347 | * @param string $value |
||
348 | */ |
||
349 | 4 | public function select_option($id, $value) |
|
350 | { |
||
351 | 4 | $this->forms()->set_value($id, $value); |
|
352 | 4 | } |
|
353 | |||
354 | /** |
||
355 | * Return the serialized representation of the input values of a form. |
||
356 | * Do not include files or disabled inputs, as well as submit inputs and buttons |
||
357 | * @param string $id |
||
358 | * @return string |
||
359 | */ |
||
360 | 1 | public function serialize_form($id) |
|
361 | { |
||
362 | 1 | return $this->forms()->serialize_form($id); |
|
363 | } |
||
364 | |||
365 | /** |
||
366 | * Click on a Node with id, triggering a link or form submit |
||
367 | * @param string $id |
||
368 | * @throws Exception_Driver If not a clickable element |
||
369 | */ |
||
370 | 3 | public function click($id) |
|
371 | { |
||
372 | 3 | $node = $this->dom($id); |
|
373 | |||
374 | 3 | if ($node->hasAttribute('href')) |
|
375 | { |
||
376 | 3 | $this->get($node->getAttribute('href')); |
|
377 | } |
||
378 | 2 | elseif (($node->tagName == 'input' AND $node->getAttribute('type') == 'submit') OR $node->tagName == 'button') |
|
379 | { |
||
380 | 2 | $form = $this->xpath()->find('./ancestor::form', $node); |
|
381 | |||
382 | 2 | $action = $form->hasAttribute('action') ? $form->getAttribute('action') : $this->request->uri(); |
|
383 | |||
384 | 2 | $method = $form->hasAttribute('method') ? strtoupper($form->getAttribute('method')) : 'GET'; |
|
385 | |||
386 | 2 | $post = $this->forms()->serialize_form($form); |
|
387 | |||
388 | 2 | $files = $this->forms()->serialize_files($form); |
|
389 | |||
390 | 2 | if (in_array($node->tagName, array('button', 'input')) AND $node->hasAttribute('name')) |
|
391 | { |
||
392 | 2 | $post = $post.'&'.$node->getAttribute('name').'='.$node->getAttribute('value'); |
|
393 | } |
||
394 | 2 | parse_str($post, $post); |
|
395 | 2 | parse_str($files, $files); |
|
396 | |||
397 | 2 | if ($method == 'GET') |
|
398 | { |
||
399 | 1 | $this->get($action, $post); |
|
400 | } |
||
401 | else |
||
402 | { |
||
403 | 2 | $this->post($action, array(), $post, $files); |
|
404 | } |
||
405 | } |
||
406 | else |
||
407 | { |
||
408 | 1 | throw new Exception_Driver('The html tag :tag cannot be clicked', array(':tag' => $node->tagName)); |
|
409 | } |
||
410 | 3 | } |
|
411 | |||
412 | /** |
||
413 | * Go to a given url address |
||
414 | * @param string $uri |
||
415 | * @param array $query |
||
416 | * @return Driver_Simple $this |
||
417 | */ |
||
418 | 1 | public function visit($uri, array $query = array()) |
|
419 | { |
||
420 | 1 | return $this->get($uri, $query); |
|
421 | } |
||
422 | |||
423 | /** |
||
424 | * Get the current path (without host and protocol) |
||
425 | * @return string |
||
426 | */ |
||
427 | 2 | public function current_path() |
|
431 | |||
432 | /** |
||
433 | * Get the current url |
||
434 | * @return string |
||
435 | */ |
||
436 | 2 | public function current_url() |
|
440 | |||
441 | /** |
||
442 | * Find all ids of a given XPath |
||
443 | * @param string $xpath |
||
444 | * @param string $parent id of the parent node |
||
445 | * @return array |
||
446 | */ |
||
447 | 15 | public function all($xpath, $parent = NULL) |
|
448 | { |
||
449 | 15 | $xpath = $parent.$xpath; |
|
450 | 15 | $ids = array(); |
|
451 | 15 | foreach ($this->xpath()->query($xpath) as $index => $elmenets) |
|
452 | { |
||
453 | 15 | $ids[] = "($xpath)[".($index+1)."]"; |
|
454 | } |
||
455 | 15 | return $ids; |
|
456 | } |
||
457 | |||
458 | /** |
||
459 | * Check if anything has been loaded |
||
460 | * @return boolean |
||
461 | */ |
||
462 | 1 | public function is_page_active() |
|
463 | { |
||
464 | 1 | return (bool) $this->content(); |
|
465 | } |
||
466 | |||
467 | /** |
||
468 | * Return the current user agent |
||
469 | * @return string |
||
470 | */ |
||
471 | 2 | public function user_agent() |
|
475 | |||
476 | /** |
||
477 | * Set the cookie (by setting the $_COOKIE array directly) |
||
478 | * @param string $name |
||
479 | * @param mixed $value |
||
480 | * @param array $parameters - not utalized by this driver |
||
481 | */ |
||
482 | 1 | public function cookie($name, $value, array $parameters = array()) |
|
483 | { |
||
484 | 1 | $_COOKIE[$name] = $value; |
|
485 | 1 | } |
|
486 | |||
487 | } |
||
488 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.