Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php defined('SYSPATH') or die('No direct access allowed.'); |
||
| 12 | class request_Core |
||
| 13 | { |
||
| 14 | |||
| 15 | // Possible HTTP methods |
||
| 16 | protected static $http_methods = array('get', 'head', 'options', 'post', 'put', 'delete'); |
||
| 17 | |||
| 18 | // Content types from client's HTTP Accept request header (array) |
||
| 19 | protected static $accept_types; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Returns the HTTP referrer, or the default if the referrer is not set. |
||
| 23 | * |
||
| 24 | * @param mixed default to return |
||
| 25 | * @return string |
||
| 26 | */ |
||
| 27 | public static function referrer($default = false) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Returns the current request protocol, based on $_SERVER['https']. In CLI |
||
| 44 | * mode, NULL will be returned. |
||
| 45 | * |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | public static function protocol() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Tests if the current request is an AJAX request by checking the X-Requested-With HTTP |
||
| 61 | * request header that most popular JS frameworks now set for AJAX calls. |
||
| 62 | * |
||
| 63 | * @return boolean |
||
| 64 | */ |
||
| 65 | public static function is_ajax() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns current request method. |
||
| 72 | * |
||
| 73 | * @throws Kohana_Exception in case of an unknown request method |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public static function method() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns boolean of whether client accepts content type. |
||
| 89 | * |
||
| 90 | * @param string content type |
||
| 91 | * @param boolean set to TRUE to disable wildcard checking |
||
| 92 | * @return boolean |
||
| 93 | */ |
||
| 94 | public static function accepts($type = null, $explicit_check = false) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Compare the q values for given array of content types and return the one with the highest value. |
||
| 107 | * If items are found to have the same q value, the first one encountered in the given array wins. |
||
| 108 | * If all items in the given array have a q value of 0, FALSE is returned. |
||
| 109 | * |
||
| 110 | * @param array content types |
||
| 111 | * @param boolean set to TRUE to disable wildcard checking |
||
| 112 | * @return mixed string mime type with highest q value, FALSE if none of the given types are accepted |
||
| 113 | */ |
||
| 114 | public static function preferred_accept($types, $explicit_check = false) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns quality factor at which the client accepts content type. |
||
| 139 | * |
||
| 140 | * @param string content type (e.g. "image/jpg", "jpg") |
||
| 141 | * @param boolean set to TRUE to disable wildcard checking |
||
| 142 | * @return integer|float |
||
| 143 | */ |
||
| 144 | public static function accepts_at_quality($type = null, $explicit_check = false) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Parses client's HTTP Accept request header, and builds array structure representing it. |
||
| 189 | * |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | protected static function parse_accept_header() |
||
| 231 | } // End request |
||
| 232 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: