Complex classes like Driver_Phantomjs 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_Phantomjs, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Driver_Phantomjs extends Driver { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Driver name |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | public $name = 'phantomjs'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Array containing parameters to be added on the next request |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | public $_next_query = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * User agent string cache |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $_user_agent; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Variable holding the current Driver_Phantomjs_Connection |
||
| 36 | * @var Driver_Phantomjs_Connection |
||
| 37 | */ |
||
| 38 | protected $_connection; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The base URL, to be prefixed on each request |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $_base_url = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Getter / Setter of the base_url, that will be prefixed on each request |
||
| 48 | * @param string $base_url |
||
| 49 | * @return string|Driver_PHantomjs |
||
| 50 | */ |
||
| 51 | 3 | public function base_url($base_url = NULL) |
|
| 52 | { |
||
| 53 | 3 | if ($base_url !== NULL) |
|
| 54 | { |
||
| 55 | 1 | $this->_base_url = (string) $base_url; |
|
| 56 | 1 | return $this; |
|
| 57 | } |
||
| 58 | 3 | return $this->_base_url; |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Getter / Setter of the Driver_Phantomjs_Connection object. |
||
| 63 | * Use this to customize the connection, otherwise a default one on a random port will be used |
||
| 64 | * |
||
| 65 | * @param Driver_Phantomjs_Connection $connection |
||
| 66 | * @return Driver_Phantomjs_Connection|Driver_Phantomjs |
||
| 67 | */ |
||
| 68 | 13 | public function connection(Driver_Phantomjs_Connection $connection = NULL) |
|
| 69 | { |
||
| 70 | 13 | if ($connection !== NULL) |
|
| 71 | { |
||
| 72 | 1 | $this->_connection = $connection; |
|
| 73 | 1 | return $this; |
|
| 74 | } |
||
| 75 | |||
| 76 | 13 | if ( ! $this->_connection) |
|
| 77 | { |
||
| 78 | 1 | $this->_connection = new Driver_Phantomjs_Connection(); |
|
| 79 | 1 | $this->_connection->start(); |
|
| 80 | } |
||
| 81 | |||
| 82 | 13 | return $this->_connection; |
|
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * If a connection has been started, stop it |
||
| 87 | */ |
||
| 88 | 2 | public function __destruct() |
|
| 89 | { |
||
| 90 | 2 | if ($this->_connection AND $this->_connection->is_started()) |
|
| 91 | { |
||
| 92 | 1 | $this->_connection->stop(); |
|
| 93 | } |
||
| 94 | 2 | } |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Clear the connection by deleting all cookies |
||
| 98 | */ |
||
| 99 | 1 | public function clear() |
|
| 100 | { |
||
| 101 | 1 | $this->connection()->delete('cookies'); |
|
|
|
|||
| 102 | 1 | } |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Getter of the raw content html, this driver does not allow "setting" |
||
| 106 | * |
||
| 107 | * @param string $content |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | 1 | public function content($content = NULL) |
|
| 111 | { |
||
| 112 | 1 | return $this->connection()->get('source'); |
|
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * NODE GETTERS |
||
| 117 | * ===================================== |
||
| 118 | */ |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get the tag name of a Node with id. e.g. DIV, SPAN ... |
||
| 122 | * @param string $id |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | 2 | public function tag_name($id) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Get the attribute of a Node with id. If the attribute does not exist, returns NULL |
||
| 132 | * @param string $id |
||
| 133 | * @param string $name |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | 2 | public function attribute($id, $name) |
|
| 137 | { |
||
| 138 | 2 | return $this->connection()->get("element/$id/attribute/$name"); |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Return the raw html of a Node with id, along with all of its children. |
||
| 143 | * @param string $id |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | 1 | public function html($id) |
|
| 147 | { |
||
| 148 | 1 | if ($id === NULL) |
|
| 149 | return $this->content(); |
||
| 150 | |||
| 151 | 1 | return $this->connection()->get("element/$id/html"); |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Return the text of a Node with id, with all the spaces collapsed, similar to browser rendering. |
||
| 156 | * @param string $id |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | 2 | public function text($id) |
|
| 160 | { |
||
| 161 | 2 | $text = $this->connection()->get("element/$id/text"); |
|
| 162 | 2 | $text = preg_replace('/[ \s\f\n\r\t\v ]+/u', ' ', $text); |
|
| 163 | 2 | return trim($text); |
|
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Return the value of a Node of a form element, e.g. INPUT, TEXTAREA or SELECT |
||
| 168 | * @param string $id |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | 2 | public function value($id) |
|
| 172 | { |
||
| 173 | 2 | return $this->connection()->get("element/$id/value"); |
|
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Check if a Node with id is visible. |
||
| 178 | * @param string $id |
||
| 179 | * @return boolean |
||
| 180 | */ |
||
| 181 | 1 | public function is_visible($id) |
|
| 182 | { |
||
| 183 | 1 | return $this->connection()->get("element/$id/visible"); |
|
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Check if a Node with id of an option element is selected |
||
| 188 | * @param string $id |
||
| 189 | * @return boolean |
||
| 190 | */ |
||
| 191 | 2 | public function is_selected($id) |
|
| 192 | { |
||
| 193 | 2 | return $this->connection()->get("element/$id/selected"); |
|
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Check if a Node with id of an input element (radio or checkbox) is checked |
||
| 198 | * @param string $id |
||
| 199 | * @return boolean |
||
| 200 | */ |
||
| 201 | 1 | public function is_checked($id) |
|
| 202 | { |
||
| 203 | 1 | return $this->connection()->get("element/$id/checked"); |
|
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set the value of a Node with id of a form element |
||
| 208 | * @param string $id |
||
| 209 | * @param string $value |
||
| 210 | */ |
||
| 211 | 1 | public function set($id, $value) |
|
| 212 | { |
||
| 213 | 1 | $tag_name = $this->tag_name($id); |
|
| 214 | |||
| 215 | 1 | if ($tag_name == 'textarea') |
|
| 216 | { |
||
| 217 | 1 | $this->connection()->post("element/$id/value", array('value' => $value)); |
|
| 218 | } |
||
| 219 | 1 | elseif ($tag_name == 'input') |
|
| 220 | { |
||
| 221 | 1 | $type = $this->attribute($id, 'type'); |
|
| 222 | 1 | if ($type == 'checkbox' OR $type == 'radio') |
|
| 223 | { |
||
| 224 | 1 | $this->connection()->post("element/$id/click", array()); |
|
| 225 | } |
||
| 226 | 1 | elseif ($type == 'file') |
|
| 227 | { |
||
| 228 | 1 | $this->connection()->post("element/$id/upload", array('value' => $value)); |
|
| 229 | } |
||
| 230 | else |
||
| 231 | { |
||
| 232 | 1 | $this->connection()->post("element/$id/value", array('value' => $value)); |
|
| 233 | } |
||
| 234 | } |
||
| 235 | 1 | elseif ($tag_name == 'option') |
|
| 236 | { |
||
| 237 | 1 | $this->connection()->post("element/$id/selected", array('value' => $value)); |
|
| 238 | } |
||
| 239 | 1 | } |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Set the option value that is selected of a Node of a select element |
||
| 243 | * @param string $id |
||
| 244 | * @param string $value |
||
| 245 | */ |
||
| 246 | 1 | public function select_option($id, $value) |
|
| 247 | { |
||
| 248 | 1 | $this->connection()->post("element/$id/selected", array('value' => $value)); |
|
| 249 | 1 | } |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Click on a Node with id, triggering a link or form submit |
||
| 253 | * @param string $id |
||
| 254 | * @throws Exception_Driver If not a clickable element |
||
| 255 | */ |
||
| 256 | 1 | public function click($id) |
|
| 257 | { |
||
| 258 | 1 | $this->connection()->post("element/$id/click", array()); |
|
| 259 | 1 | } |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Go to a given url address, use next_query along with the provided query array |
||
| 263 | * @param string $uri |
||
| 264 | * @param array $query |
||
| 265 | */ |
||
| 266 | 2 | public function visit($uri, array $query = array()) |
|
| 267 | { |
||
| 268 | 2 | $query = array_merge((array) $this->_next_query, (array) $query); |
|
| 269 | |||
| 270 | 2 | $this->_next_query = NULL; |
|
| 271 | |||
| 272 | // Check for implicit query string |
||
| 273 | 2 | if (strpos($uri, '?') !== FALSE) |
|
| 274 | { |
||
| 275 | $query = array_merge(self::extract_query_from_uri($uri), $query); |
||
| 276 | $uri = substr($uri, 0, strpos($uri, '?')); |
||
| 277 | } |
||
| 278 | |||
| 279 | 2 | $url = $this->base_url().$uri.($query ? '?'.http_build_query($query) : ''); |
|
| 280 | |||
| 281 | 2 | $this->connection()->post('url', array('value' => $url)); |
|
| 282 | 2 | } |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Get the current path (without host and protocol) |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | 1 | public function current_path() |
|
| 289 | { |
||
| 290 | 1 | $url = parse_url($this->connection()->get('url')); |
|
| 291 | |||
| 292 | 1 | return $url['path'].(isset($url['query']) ? '?'.$url['query'] : ''); |
|
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get the current url |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | 2 | public function current_url() |
|
| 300 | { |
||
| 301 | 2 | return urldecode($this->connection()->get('url')); |
|
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Find all ids of a given XPath |
||
| 306 | * @param string $xpath |
||
| 307 | * @param string $parent id of the parent node |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | 7 | public function all($xpath, $parent = NULL) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Setter for the next_query variable, to be added to the next visit's query |
||
| 317 | * @param array $query |
||
| 318 | */ |
||
| 319 | 1 | public function next_query(array $query) |
|
| 320 | { |
||
| 321 | 1 | $this->_next_query = $query; |
|
| 322 | 1 | } |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Check if a connection is active |
||
| 326 | * @return boolean |
||
| 327 | */ |
||
| 328 | 1 | public function is_page_active() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Return all javascript errors for the current page |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | 1 | public function javascript_errors() |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Return all console messages for the current page |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | 1 | public function javascript_messages() |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Do a screenshot of the current page into a file |
||
| 353 | * @param string $file |
||
| 354 | */ |
||
| 355 | 1 | public function screenshot($file) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Get the current user agent |
||
| 362 | * @return string |
||
| 363 | */ |
||
| 364 | 1 | public function user_agent() |
|
| 365 | { |
||
| 366 | 1 | if ( ! $this->_user_agent) |
|
| 367 | { |
||
| 368 | 1 | $settings = $this->connection()->get('settings'); |
|
| 369 | 1 | $this->_user_agent = isset($settings['userAgent']) ? $settings['userAgent'] : NULL; |
|
| 370 | } |
||
| 371 | |||
| 372 | 1 | return $this->_user_agent; |
|
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Execute raw javascript. it will be executed in the context of a given node ('this' will point to the node) and the return of the script will be the return of this method |
||
| 377 | * @param string $id |
||
| 378 | * @param string $script |
||
| 379 | * @return mixed |
||
| 380 | */ |
||
| 381 | 2 | public function execute($id, $script) |
|
| 382 | { |
||
| 383 | 2 | return $this->connection()->post('execute', array( |
|
| 384 | 2 | 'value' => $script, |
|
| 385 | 2 | 'id' => $id, |
|
| 386 | )); |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Return all the current cookies |
||
| 391 | * @return array |
||
| 392 | */ |
||
| 393 | 1 | public function cookies() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Set a cookie. Use parameters to set "expires", "path", "domain", "secure" and "httponly" |
||
| 400 | * @param string $name |
||
| 401 | * @param mixed $value |
||
| 402 | * @param array $parameters |
||
| 403 | */ |
||
| 404 | 1 | public function cookie($name, $value, array $parameters = array()) |
|
| 405 | { |
||
| 416 | } |
||
| 417 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: