Complex classes like URI 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 URI, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class URI { |
||
| 5 | |||
| 6 | /** |
||
| 7 | * List of cached uri segments |
||
| 8 | * |
||
| 9 | * @var array |
||
| 10 | * @access public |
||
| 11 | * |
||
| 12 | */ |
||
| 13 | var $keyval=array (); |
||
|
|
|||
| 14 | /** |
||
| 15 | * Current uri string |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | * @access public |
||
| 19 | * |
||
| 20 | */ |
||
| 21 | var $uri_string; |
||
| 22 | /** |
||
| 23 | * List of uri segments |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | * @access public |
||
| 27 | * |
||
| 28 | */ |
||
| 29 | var $segments=array (); |
||
| 30 | /** |
||
| 31 | * Re-indexed list of uri segments |
||
| 32 | * Starts at 1 instead of 0 |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | * @access public |
||
| 36 | * |
||
| 37 | */ |
||
| 38 | var $rsegments=array (); |
||
| 39 | var $permitted_uri_chars="a-z 0-9~%.:_\-"; |
||
| 40 | var $enable_query_strings=false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * | 'AUTO' Default - auto detects |
||
| 44 | * | 'PATH_INFO' Uses the PATH_INFO |
||
| 45 | * | 'QUERY_STRING' Uses the QUERY_STRING |
||
| 46 | * | 'REQUEST_URI' Uses the REQUEST_URI |
||
| 47 | * | 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO |
||
| 48 | */ |
||
| 49 | var $uri_protocol="AUTO"; |
||
| 50 | // |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Constructor |
||
| 54 | * |
||
| 55 | * Simply globalizes the $RTR object. The front |
||
| 56 | * loads the Router class early on so it's not available |
||
| 57 | * normally as other classes are. |
||
| 58 | * |
||
| 59 | * @access public |
||
| 60 | * |
||
| 61 | */ |
||
| 62 | function __construct() { |
||
| 66 | |||
| 67 | // -------------------------------------------------------------------- |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get the URI String |
||
| 71 | * |
||
| 72 | * @access private |
||
| 73 | * @return string |
||
| 74 | * |
||
| 75 | */ |
||
| 76 | function _fetch_uri_string() { |
||
| 125 | |||
| 126 | // -------------------------------------------------------------------- |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Set the URI String |
||
| 130 | * |
||
| 131 | * @access public |
||
| 132 | * @param string |
||
| 133 | * @return string |
||
| 134 | * |
||
| 135 | */ |
||
| 136 | function _set_uri_string($str) { |
||
| 143 | |||
| 144 | // -------------------------------------------------------------------- |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Detects the URI |
||
| 148 | * |
||
| 149 | * This function will detect the URI automatically and fix the query string |
||
| 150 | * if necessary. |
||
| 151 | * |
||
| 152 | * @access private |
||
| 153 | * @return string |
||
| 154 | * |
||
| 155 | */ |
||
| 156 | private function _detect_uri() { |
||
| 192 | |||
| 193 | // -------------------------------------------------------------------- |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Parse cli arguments |
||
| 197 | * |
||
| 198 | * Take each command line argument and assume it is a URI segment. |
||
| 199 | * |
||
| 200 | * @access private |
||
| 201 | * @return string |
||
| 202 | * |
||
| 203 | */ |
||
| 204 | private function _parse_cli_args() { |
||
| 209 | |||
| 210 | // -------------------------------------------------------------------- |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Filter segments for malicious characters |
||
| 214 | * |
||
| 215 | * @access private |
||
| 216 | * @param string |
||
| 217 | * @return string |
||
| 218 | * |
||
| 219 | */ |
||
| 220 | function _filter_uri($str) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Explode the URI Segments. |
||
| 238 | * The individual segments will |
||
| 239 | * be stored in the $this->segments array. |
||
| 240 | * |
||
| 241 | * @access private |
||
| 242 | * @return void |
||
| 243 | * |
||
| 244 | */ |
||
| 245 | function _explode_segments() { |
||
| 255 | |||
| 256 | // -------------------------------------------------------------------- |
||
| 257 | /** |
||
| 258 | * Re-index Segments |
||
| 259 | * |
||
| 260 | * This function re-indexes the $this->segment array so that it |
||
| 261 | * starts at 1 rather than 0. Doing so makes it simpler to |
||
| 262 | * use functions like $this->uri->segment(n) since there is |
||
| 263 | * a 1:1 relationship between the segment array and the actual segments. |
||
| 264 | * |
||
| 265 | * @access private |
||
| 266 | * @return void |
||
| 267 | * |
||
| 268 | */ |
||
| 269 | function _reindex_segments() { |
||
| 275 | |||
| 276 | // -------------------------------------------------------------------- |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Fetch a URI Segment |
||
| 280 | * |
||
| 281 | * This function returns the URI segment based on the number provided. |
||
| 282 | * |
||
| 283 | * @access public |
||
| 284 | * @param integer |
||
| 285 | * @param bool |
||
| 286 | * @return string |
||
| 287 | * |
||
| 288 | */ |
||
| 289 | function segment($n, $no_result=FALSE) { |
||
| 292 | |||
| 293 | // -------------------------------------------------------------------- |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Fetch a URI "routed" Segment |
||
| 297 | * |
||
| 298 | * This function returns the re-routed URI segment (assuming routing rules are used) |
||
| 299 | * based on the number provided. If there is no routing this function returns the |
||
| 300 | * same result as $this->segment() |
||
| 301 | * |
||
| 302 | * @access public |
||
| 303 | * @param integer |
||
| 304 | * @param bool |
||
| 305 | * @return string |
||
| 306 | * |
||
| 307 | */ |
||
| 308 | function rsegment($n, $no_result=FALSE) { |
||
| 311 | |||
| 312 | // -------------------------------------------------------------------- |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Generate a key value pair from the URI string |
||
| 316 | * |
||
| 317 | * This function generates and associative array of URI data starting |
||
| 318 | * at the supplied segment. For example, if this is your URI: |
||
| 319 | * |
||
| 320 | * example.com/user/search/name/joe/location/UK/gender/male |
||
| 321 | * |
||
| 322 | * You can use this function to generate an array with this prototype: |
||
| 323 | * |
||
| 324 | * array ( |
||
| 325 | * name => joe |
||
| 326 | * location => UK |
||
| 327 | * gender => male |
||
| 328 | * ) |
||
| 329 | * |
||
| 330 | * @access public |
||
| 331 | * @param integer the starting segment number |
||
| 332 | * @param array an array of default values |
||
| 333 | * @return array |
||
| 334 | * |
||
| 335 | */ |
||
| 336 | function uri_to_assoc($n=3, $default=array()) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Identical to above only it uses the re-routed segment array |
||
| 342 | * |
||
| 343 | * @access public |
||
| 344 | * @param integer the starting segment number |
||
| 345 | * @param array an array of default values |
||
| 346 | * @return array |
||
| 347 | * |
||
| 348 | * |
||
| 349 | */ |
||
| 350 | function ruri_to_assoc($n=3, $default=array()) { |
||
| 353 | |||
| 354 | // -------------------------------------------------------------------- |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Generate a key value pair from the URI string or Re-routed URI string |
||
| 358 | * |
||
| 359 | * @access private |
||
| 360 | * @param integer the starting segment number |
||
| 361 | * @param array an array of default values |
||
| 362 | * @param string which array we should use |
||
| 363 | * @return array |
||
| 364 | * |
||
| 365 | */ |
||
| 366 | function _uri_to_assoc($n=3, $default=array(), $which='segment') { |
||
| 423 | |||
| 424 | // -------------------------------------------------------------------- |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Generate a URI string from an associative array |
||
| 428 | * |
||
| 429 | * |
||
| 430 | * @access public |
||
| 431 | * @param array an associative array of key/values |
||
| 432 | * @return array |
||
| 433 | * |
||
| 434 | */ |
||
| 435 | function assoc_to_uri($array) { |
||
| 444 | |||
| 445 | // -------------------------------------------------------------------- |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Fetch a URI Segment and add a trailing slash |
||
| 449 | * |
||
| 450 | * @access public |
||
| 451 | * @param integer |
||
| 452 | * @param string |
||
| 453 | * @return string |
||
| 454 | * |
||
| 455 | */ |
||
| 456 | function slash_segment($n, $where='trailing') { |
||
| 459 | |||
| 460 | // -------------------------------------------------------------------- |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Fetch a URI Segment and add a trailing slash |
||
| 464 | * |
||
| 465 | * @access public |
||
| 466 | * @param integer |
||
| 467 | * @param string |
||
| 468 | * @return string |
||
| 469 | * |
||
| 470 | */ |
||
| 471 | function slash_rsegment($n, $where='trailing') { |
||
| 474 | |||
| 475 | // -------------------------------------------------------------------- |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Fetch a URI Segment and add a trailing slash - helper function |
||
| 479 | * |
||
| 480 | * @access private |
||
| 481 | * @param integer |
||
| 482 | * @param string |
||
| 483 | * @param string |
||
| 484 | * @return string |
||
| 485 | * |
||
| 486 | */ |
||
| 487 | function _slash_segment($n, $where='trailing', $which='segment') { |
||
| 499 | |||
| 500 | // -------------------------------------------------------------------- |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Segment Array |
||
| 504 | * |
||
| 505 | * @access public |
||
| 506 | * @return array |
||
| 507 | * |
||
| 508 | */ |
||
| 509 | function segment_array() { |
||
| 512 | |||
| 513 | // -------------------------------------------------------------------- |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Routed Segment Array |
||
| 517 | * |
||
| 518 | * @access public |
||
| 519 | * @return array |
||
| 520 | * |
||
| 521 | */ |
||
| 522 | function rsegment_array() { |
||
| 525 | |||
| 526 | // -------------------------------------------------------------------- |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Total number of segments |
||
| 530 | * |
||
| 531 | * @access public |
||
| 532 | * @return integer |
||
| 533 | * |
||
| 534 | */ |
||
| 535 | function total_segments() { |
||
| 538 | |||
| 539 | // -------------------------------------------------------------------- |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Total number of routed segments |
||
| 543 | * |
||
| 544 | * @access public |
||
| 545 | * @return integer |
||
| 546 | * |
||
| 547 | */ |
||
| 548 | function total_rsegments() { |
||
| 551 | |||
| 552 | // -------------------------------------------------------------------- |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Fetch the entire URI string |
||
| 556 | * |
||
| 557 | * @access public |
||
| 558 | * @return string |
||
| 559 | * |
||
| 560 | */ |
||
| 561 | function uri_string() { |
||
| 564 | |||
| 565 | // -------------------------------------------------------------------- |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Fetch the entire Re-routed URI string |
||
| 569 | * |
||
| 570 | * @access public |
||
| 571 | * @return string |
||
| 572 | * |
||
| 573 | */ |
||
| 574 | function ruri_string() { |
||
| 577 | } |
||
| 578 | // END URI Class |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.