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) |
|
97 | |||
98 | /** |
||
99 | * Getter for the current environment |
||
100 | * @return Environment |
||
101 | */ |
||
102 | 7 | public function environment() |
|
103 | 1 | { |
|
104 | 7 | return $this->_environment; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Restore the environment |
||
109 | * @return Driver_Simple $this |
||
110 | */ |
||
111 | 1 | public function clear() |
|
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() |
|
154 | |||
155 | /** |
||
156 | * Getter for the current xpath object for the page |
||
157 | * @return DOMXpath |
||
158 | */ |
||
159 | 20 | public function xpath() |
|
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) |
|
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()) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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()) |
|
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) |
|
457 | |||
458 | /** |
||
459 | * Check if anything has been loaded |
||
460 | * @return boolean |
||
461 | */ |
||
462 | 1 | public function is_page_active() |
|
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()) |
|
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.