| Conditions | 21 |
| Paths | 3460 |
| Total Lines | 119 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php defined('SYSPATH') or die('No direct access allowed.'); |
||
| 36 | public static function setup() |
||
|
|
|||
| 37 | { |
||
| 38 | if (! empty($_SERVER['QUERY_STRING'])) { |
||
| 39 | // Set the query string to the current query string |
||
| 40 | Router::$query_string = '?'.trim($_SERVER['QUERY_STRING'], '&/'); |
||
| 41 | } |
||
| 42 | |||
| 43 | if (Router::$routes === null) { |
||
| 44 | // Load routes |
||
| 45 | Router::$routes = Kohana::config('routes'); |
||
| 46 | } |
||
| 47 | |||
| 48 | // Default route status |
||
| 49 | $default_route = false; |
||
| 50 | |||
| 51 | if (Router::$current_uri === '') { |
||
| 52 | // Make sure the default route is set |
||
| 53 | if (! isset(Router::$routes['_default'])) { |
||
| 54 | throw new Kohana_Exception('core.no_default_route'); |
||
| 55 | } |
||
| 56 | |||
| 57 | // Use the default route when no segments exist |
||
| 58 | Router::$current_uri = Router::$routes['_default']; |
||
| 59 | |||
| 60 | // Default route is in use |
||
| 61 | $default_route = true; |
||
| 62 | } |
||
| 63 | |||
| 64 | // Make sure the URL is not tainted with HTML characters |
||
| 65 | Router::$current_uri = html::specialchars(Router::$current_uri, false); |
||
| 66 | |||
| 67 | // Remove all dot-paths from the URI, they are not valid |
||
| 68 | Router::$current_uri = preg_replace('#\.[\s./]*/#', '', Router::$current_uri); |
||
| 69 | |||
| 70 | // At this point segments, rsegments, and current URI are all the same |
||
| 71 | Router::$segments = Router::$rsegments = Router::$current_uri = trim(Router::$current_uri, '/'); |
||
| 72 | |||
| 73 | // Set the complete URI |
||
| 74 | Router::$complete_uri = Router::$current_uri.Router::$query_string; |
||
| 75 | |||
| 76 | // Explode the segments by slashes |
||
| 77 | Router::$segments = ($default_route === true or Router::$segments === '') ? array() : explode('/', Router::$segments); |
||
| 78 | |||
| 79 | if ($default_route === false and count(Router::$routes) > 1) { |
||
| 80 | // Custom routing |
||
| 81 | Router::$rsegments = Router::routed_uri(Router::$current_uri); |
||
| 82 | } |
||
| 83 | |||
| 84 | // The routed URI is now complete |
||
| 85 | Router::$routed_uri = Router::$rsegments; |
||
| 86 | |||
| 87 | // Routed segments will never be empty |
||
| 88 | Router::$rsegments = explode('/', Router::$rsegments); |
||
| 89 | |||
| 90 | // Prepare to find the controller |
||
| 91 | $controller_path = ''; |
||
| 92 | $method_segment = null; |
||
| 93 | |||
| 94 | // Paths to search |
||
| 95 | $paths = Kohana::include_paths(); |
||
| 96 | |||
| 97 | foreach (Router::$rsegments as $key => $segment) { |
||
| 98 | // Add the segment to the search path |
||
| 99 | $controller_path .= $segment; |
||
| 100 | |||
| 101 | $found = false; |
||
| 102 | foreach ($paths as $dir) { |
||
| 103 | // Search within controllers only |
||
| 104 | $dir .= 'controllers/'; |
||
| 105 | |||
| 106 | if (is_dir($dir.$controller_path) or is_file($dir.$controller_path.EXT)) { |
||
| 107 | // Valid path |
||
| 108 | $found = true; |
||
| 109 | |||
| 110 | // The controller must be a file that exists with the search path |
||
| 111 | if ($c = str_replace('\\', '/', realpath($dir.$controller_path.EXT)) |
||
| 112 | and is_file($c) and strpos($c, $dir) === 0) { |
||
| 113 | // Set controller name |
||
| 114 | Router::$controller = $segment; |
||
| 115 | |||
| 116 | // Change controller path |
||
| 117 | Router::$controller_path = $c; |
||
| 118 | |||
| 119 | // Set the method segment |
||
| 120 | $method_segment = $key + 1; |
||
| 121 | |||
| 122 | // Stop searching |
||
| 123 | break; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | if ($found === false) { |
||
| 129 | // Maximum depth has been reached, stop searching |
||
| 130 | break; |
||
| 131 | } |
||
| 132 | |||
| 133 | // Add another slash |
||
| 134 | $controller_path .= '/'; |
||
| 135 | } |
||
| 136 | |||
| 137 | if ($method_segment !== null and isset(Router::$rsegments[$method_segment])) { |
||
| 138 | // Set method |
||
| 139 | Router::$method = Router::$rsegments[$method_segment]; |
||
| 140 | |||
| 141 | if (isset(Router::$rsegments[$method_segment + 1])) { |
||
| 142 | // Set arguments |
||
| 143 | Router::$arguments = array_slice(Router::$rsegments, $method_segment + 1); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | // Last chance to set routing before a 404 is triggered |
||
| 148 | Event::run('system.post_routing'); |
||
| 149 | |||
| 150 | if (Router::$controller === null) { |
||
| 151 | // No controller was found, so no page can be rendered |
||
| 152 | Event::run('system.404'); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 271 |
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: