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 URI_Core extends Router |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Returns a singleton instance of URI. |
||
| 17 | * |
||
| 18 | * @return object |
||
|
|
|||
| 19 | */ |
||
| 20 | public static function instance() |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Retrieve a specific URI segment. |
||
| 34 | * |
||
| 35 | * @param integer|string segment number or label |
||
| 36 | * @param mixed default value returned if segment does not exist |
||
| 37 | * @return string |
||
| 38 | */ |
||
| 39 | View Code Duplication | public function segment($index = 1, $default = false) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Retrieve a specific routed URI segment. |
||
| 56 | * |
||
| 57 | * @param integer|string rsegment number or label |
||
| 58 | * @param mixed default value returned if segment does not exist |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function rsegment($index = 1, $default = false) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Retrieve a specific URI argument. |
||
| 78 | * This is the part of the segments that does not indicate controller or method |
||
| 79 | * |
||
| 80 | * @param integer|string argument number or label |
||
| 81 | * @param mixed default value returned if segment does not exist |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function argument($index = 1, $default = false) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Returns an array containing all the URI segments. |
||
| 101 | * |
||
| 102 | * @param integer segment offset |
||
| 103 | * @param boolean return an associative array |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function segment_array($offset = 0, $associative = false) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns an array containing all the re-routed URI segments. |
||
| 113 | * |
||
| 114 | * @param integer rsegment offset |
||
| 115 | * @param boolean return an associative array |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | public function rsegment_array($offset = 0, $associative = false) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns an array containing all the URI arguments. |
||
| 125 | * |
||
| 126 | * @param integer segment offset |
||
| 127 | * @param boolean return an associative array |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | public function argument_array($offset = 0, $associative = false) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Creates a simple or associative array from an array and an offset. |
||
| 137 | * Used as a helper for (r)segment_array and argument_array. |
||
| 138 | * |
||
| 139 | * @param array array to rebuild |
||
| 140 | * @param integer offset to start from |
||
| 141 | * @param boolean create an associative array |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function build_array($array, $offset = 0, $associative = false) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns the complete URI as a string. |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function string() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Magic method for converting an object to a string. |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function __toString() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Returns the total number of URI segments. |
||
| 189 | * |
||
| 190 | * @return integer |
||
| 191 | */ |
||
| 192 | public function total_segments() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Returns the total number of re-routed URI segments. |
||
| 199 | * |
||
| 200 | * @return integer |
||
| 201 | */ |
||
| 202 | public function total_rsegments() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the total number of URI arguments. |
||
| 209 | * |
||
| 210 | * @return integer |
||
| 211 | */ |
||
| 212 | public function total_arguments() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Returns the last URI segment. |
||
| 219 | * |
||
| 220 | * @param mixed default value returned if segment does not exist |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function last_segment($default = false) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns the last re-routed URI segment. |
||
| 234 | * |
||
| 235 | * @param mixed default value returned if segment does not exist |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public function last_rsegment($default = false) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Returns the path to the current controller (not including the actual |
||
| 249 | * controller), as a web path. |
||
| 250 | * |
||
| 251 | * @param boolean return a full url, or only the path specifically |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function controller_path($full = true) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Returns the current controller, as a web path. |
||
| 261 | * |
||
| 262 | * @param boolean return a full url, or only the controller specifically |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function controller($full = true) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Returns the current method, as a web path. |
||
| 272 | * |
||
| 273 | * @param boolean return a full url, or only the method specifically |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function method($full = true) |
||
| 280 | } // End URI Class |
||
| 281 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.