| Conditions | 45 |
| Paths | > 20000 |
| Total Lines | 232 |
| Code Lines | 138 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 42 | public function parseRequest(KernelMessageUri $uri = null) |
||
| 43 | { |
||
| 44 | $this->uri = is_null($uri) ? new KernelMessageUri() : $uri; |
||
|
|
|||
| 45 | $uriSegments = $this->uri->getSegments()->getParts(); |
||
| 46 | $uriString = $this->uri->getSegments()->getString(); |
||
| 47 | |||
| 48 | if ($this->uri->getSegments()->getTotalParts()) { |
||
| 49 | if (strpos(end($uriSegments), '.json') !== false) { |
||
| 50 | output()->setContentType('application/json'); |
||
| 51 | $endSegment = str_replace('.json', '', end($uriSegments)); |
||
| 52 | array_pop($uriSegments); |
||
| 53 | array_push($uriSegments, $endSegment); |
||
| 54 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
| 55 | $uriString = $this->uri->getSegments()->getString(); |
||
| 56 | } elseif (strpos(end($uriSegments), '.xml') !== false) { |
||
| 57 | output()->setContentType('application/xml'); |
||
| 58 | $endSegment = str_replace('.xml', '', end($uriSegments)); |
||
| 59 | array_pop($uriSegments); |
||
| 60 | array_push($uriSegments, $endSegment); |
||
| 61 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
| 62 | $uriString = $this->uri->getSegments()->getString(); |
||
| 63 | } |
||
| 64 | } else { |
||
| 65 | $uriPath = urldecode( |
||
| 66 | parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH) |
||
| 67 | ); |
||
| 68 | |||
| 69 | $uriPathParts = explode('public/', $uriPath); |
||
| 70 | $uriPath = end($uriPathParts); |
||
| 71 | |||
| 72 | if ($uriPath !== '/') { |
||
| 73 | $uriString = $uriPath; |
||
| 74 | $uriSegments = array_filter(explode('/', $uriString)); |
||
| 75 | |||
| 76 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
| 77 | $uriString = $this->uri->getSegments()->getString(); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | // Load app addresses config |
||
| 82 | $this->addresses = config()->loadFile('addresses', true); |
||
| 83 | |||
| 84 | if ($this->addresses instanceof KernelAddresses) { |
||
| 85 | // Domain routing |
||
| 86 | if (null !== ($domain = $this->addresses->getDomain())) { |
||
| 87 | if (is_array($domain)) { |
||
| 88 | $uriSegments = array_merge($domain, $uriSegments); |
||
| 89 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
| 90 | $uriString = $this->uri->getSegments()->getString(); |
||
| 91 | $domain = reset($uriSegments); |
||
| 92 | } |
||
| 93 | |||
| 94 | if (false !== ($app = modules()->getApp($domain))) { |
||
| 95 | $this->registerModule($app); |
||
| 96 | } elseif (false !== ($module = modules()->getModule($domain))) { |
||
| 97 | $this->registerModule($module); |
||
| 98 | } |
||
| 99 | } elseif (false !== ($subdomain = $this->uri->getSubdomain())) { |
||
| 100 | if (false !== ($app = modules()->getApp($subdomain))) { |
||
| 101 | $this->registerModule($app); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | // Module routing |
||
| 107 | if ($uriTotalSegments = count($uriSegments)) { |
||
| 108 | for ($i = 0; $i <= $uriTotalSegments; $i++) { |
||
| 109 | $uriRoutedSegments = array_diff($uriSegments, |
||
| 110 | array_slice($uriSegments, ($uriTotalSegments - $i))); |
||
| 111 | |||
| 112 | if ( ! empty($app)) { |
||
| 113 | if (reset($uriSegments) !== $app->getParameter()) { |
||
| 114 | array_unshift($uriRoutedSegments, $app->getParameter()); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | if (false !== ($module = modules()->getModule($uriRoutedSegments))) { |
||
| 119 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
| 120 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
| 121 | $uriString = $this->uri->getSegments()->getString(); |
||
| 122 | |||
| 123 | $this->registerModule($module); |
||
| 124 | |||
| 125 | break; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | // Try to translate from uri string |
||
| 131 | if (false !== ($action = $this->addresses->getTranslation($uriString))) { |
||
| 132 | if ( ! $action->isValidHttpMethod(input()->server('REQUEST_METHOD')) && ! $action->isAnyHttpMethod()) { |
||
| 133 | output()->sendError(405); |
||
| 134 | } else { |
||
| 135 | // Checks if action closure is an array |
||
| 136 | if (is_array($closureSegments = $action->getClosure())) { |
||
| 137 | // Closure App Routing |
||
| 138 | if (false !== ($app = modules()->getModule(reset($closureSegments)))) { |
||
| 139 | array_shift($closureSegments); |
||
| 140 | $this->registerModule($app); |
||
| 141 | } |
||
| 142 | |||
| 143 | // Closure Module routing |
||
| 144 | if ($numOfClosureSegments = count($closureSegments)) { |
||
| 145 | for ($i = 0; $i <= $numOfClosureSegments; $i++) { |
||
| 146 | $closureRoutedSegments = array_diff($closureSegments, |
||
| 147 | array_slice($closureSegments, ($numOfClosureSegments - $i))); |
||
| 148 | |||
| 149 | if ( ! empty($app)) { |
||
| 150 | if (reset($closureSegments) !== $app->getParameter()) { |
||
| 151 | array_unshift($closureRoutedSegments, $app->getParameter()); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | if (false !== ($module = modules()->getModule($closureRoutedSegments))) { |
||
| 156 | $uriSegments = array_diff($closureSegments, $closureRoutedSegments); |
||
| 157 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($closureSegments)); |
||
| 158 | $uriString = $this->uri->getSegments()->getString(); |
||
| 159 | |||
| 160 | $this->registerModule($module); |
||
| 161 | |||
| 162 | break; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } else { |
||
| 167 | if (false !== ($parseSegments = $action->getParseUriString($uriString))) { |
||
| 168 | $uriSegments = $parseSegments; |
||
| 169 | } else { |
||
| 170 | $uriSegments = []; |
||
| 171 | } |
||
| 172 | |||
| 173 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
| 174 | $uriString = $this->uri->getSegments()->getString(); |
||
| 175 | |||
| 176 | $this->parseAction($action, $uriSegments); |
||
| 177 | if ( ! empty(services()->has('controller'))) { |
||
| 178 | return true; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | // Try to get route from controller & page |
||
| 185 | if ($uriTotalSegments = count($uriSegments)) { |
||
| 186 | for ($i = 0; $i <= $uriTotalSegments; $i++) { |
||
| 187 | $uriRoutedSegments = array_slice($uriSegments, 0, ($uriTotalSegments - $i)); |
||
| 188 | $modules = modules()->getArrayCopy(); |
||
| 189 | |||
| 190 | foreach ($modules as $module) { |
||
| 191 | $controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
||
| 192 | if ($module->getNamespace() === 'O2System\Framework\\') { |
||
| 193 | $controllerNamespace = 'O2System\Framework\Http\Controllers\\'; |
||
| 194 | } |
||
| 195 | |||
| 196 | if (false !== ($pagesDir = $module->getDir('pages', true))) { |
||
| 197 | $pageFilePath = $pagesDir . implode(DIRECTORY_SEPARATOR, |
||
| 198 | array_map('dash', $uriRoutedSegments)) . '.phtml'; |
||
| 199 | |||
| 200 | if ( ! class_exists($controllerClassName = $controllerNamespace . 'Pages')) { |
||
| 201 | $controllerClassName = '\O2System\Framework\Http\Controllers\Pages'; |
||
| 202 | } else { |
||
| 203 | $controller = new $controllerClassName(); |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Try to find from database |
||
| 207 | */ |
||
| 208 | $modelClassName = str_replace('Controllers', 'Models', $controllerClassName); |
||
| 209 | |||
| 210 | if (class_exists($modelClassName)) { |
||
| 211 | models()->load($modelClassName, 'controller'); |
||
| 212 | |||
| 213 | if (false !== ($page = models('controller')->find($uriString, 'segments'))) { |
||
| 214 | if (isset($page->content)) { |
||
| 215 | presenter()->partials->offsetSet('content', $page->content); |
||
| 216 | |||
| 217 | $this->setController( |
||
| 218 | (new KernelControllerDataStructure($controller)) |
||
| 219 | ->setRequestMethod('index') |
||
| 220 | ); |
||
| 221 | |||
| 222 | return true; |
||
| 223 | break; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Try to find from page file |
||
| 231 | */ |
||
| 232 | if (is_file($pageFilePath)) { |
||
| 233 | presenter()->page->setFile($pageFilePath); |
||
| 234 | } else { |
||
| 235 | $pageFilePath = str_replace('.phtml', DIRECTORY_SEPARATOR . 'index.phtml', $pageFilePath); |
||
| 236 | if(is_file($pageFilePath)) { |
||
| 237 | presenter()->page->setFile($pageFilePath); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | if(presenter()->page->file instanceof SplFileInfo) { |
||
| 242 | $this->setController( |
||
| 243 | (new KernelControllerDataStructure($controllerClassName)) |
||
| 244 | ->setRequestMethod('index') |
||
| 245 | ); |
||
| 246 | |||
| 247 | return true; |
||
| 248 | break; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | if (class_exists($controllerClassName = $controllerNamespace . implode('\\', |
||
| 253 | array_map('studlycase', $uriRoutedSegments)))) { |
||
| 254 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
| 255 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
| 256 | $uriSegments); |
||
| 257 | break; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | // break the loop if the controller has been set |
||
| 262 | if (services()->has('controller')) { |
||
| 263 | return true; |
||
| 264 | break; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | if (class_exists($controllerClassName = modules()->current()->getDefaultControllerClassName())) { |
||
| 270 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
| 271 | $uriSegments); |
||
| 272 | |||
| 273 | return true; |
||
| 274 | } |
||
| 419 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..