| Conditions | 32 |
| Paths | > 20000 |
| Total Lines | 158 |
| 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 |
||
| 229 | private function compileRoute(Route $route, $name, $supportsRedirections, $parentPrefix = null) |
||
| 230 | { |
||
| 231 | $code = ''; |
||
| 232 | $compiledRoute = $route->compile(); |
||
| 233 | $conditions = array(); |
||
| 234 | $hasTrailingSlash = false; |
||
| 235 | $matches = false; |
||
| 236 | $hostMatches = false; |
||
| 237 | $methods = $route->getMethods(); |
||
| 238 | |||
| 239 | $supportsTrailingSlash = $supportsRedirections && (!$methods || in_array('GET', $methods)); |
||
| 240 | $regex = $compiledRoute->getRegex(); |
||
| 241 | |||
| 242 | if (!count($compiledRoute->getPathVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#'.('u' === substr($regex, -1) ? 'u' : ''), $regex, $m)) { |
||
| 243 | if ($supportsTrailingSlash && '/' === substr($m['url'], -1)) { |
||
| 244 | $conditions[] = sprintf('%s === $trimmedPathinfo', var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true)); |
||
| 245 | $hasTrailingSlash = true; |
||
| 246 | } else { |
||
| 247 | $conditions[] = sprintf('%s === $pathinfo', var_export(str_replace('\\', '', $m['url']), true)); |
||
| 248 | } |
||
| 249 | } else { |
||
| 250 | if ($compiledRoute->getStaticPrefix() && $compiledRoute->getStaticPrefix() !== $parentPrefix) { |
||
| 251 | $conditions[] = sprintf('0 === strpos($pathinfo, %s)', var_export($compiledRoute->getStaticPrefix(), true)); |
||
| 252 | } |
||
| 253 | |||
| 254 | if ($supportsTrailingSlash && $pos = strpos($regex, '/$')) { |
||
| 255 | $regex = substr($regex, 0, $pos).'/?$'.substr($regex, $pos + 2); |
||
| 256 | $hasTrailingSlash = true; |
||
| 257 | } |
||
| 258 | $conditions[] = sprintf('preg_match(%s, $pathinfo, $matches)', var_export($regex, true)); |
||
| 259 | |||
| 260 | $matches = true; |
||
| 261 | } |
||
| 262 | |||
| 263 | if ($compiledRoute->getHostVariables()) { |
||
| 264 | $hostMatches = true; |
||
| 265 | } |
||
| 266 | |||
| 267 | if ($route->getCondition()) { |
||
| 268 | $conditions[] = $this->getExpressionLanguage()->compile($route->getCondition(), array('context', 'request')); |
||
| 269 | } |
||
| 270 | |||
| 271 | $conditions = implode(' && ', $conditions); |
||
| 272 | |||
| 273 | $code .= <<<EOF |
||
| 274 | // $name |
||
| 275 | if ($conditions) { |
||
| 276 | |||
| 277 | EOF; |
||
| 278 | |||
| 279 | $gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name); |
||
| 280 | |||
| 281 | // the offset where the return value is appended below, with indendation |
||
| 282 | $retOffset = 12 + strlen($code); |
||
| 283 | |||
| 284 | // optimize parameters array |
||
| 285 | if ($matches || $hostMatches) { |
||
| 286 | $vars = array(); |
||
| 287 | if ($hostMatches) { |
||
| 288 | $vars[] = '$hostMatches'; |
||
| 289 | } |
||
| 290 | if ($matches) { |
||
| 291 | $vars[] = '$matches'; |
||
| 292 | } |
||
| 293 | $vars[] = "array('_route' => '$name')"; |
||
| 294 | |||
| 295 | $code .= sprintf( |
||
| 296 | " \$ret = \$this->mergeDefaults(array_replace(%s), %s);\n", |
||
| 297 | implode(', ', $vars), |
||
| 298 | str_replace("\n", '', var_export($route->getDefaults(), true)) |
||
| 299 | ); |
||
| 300 | } elseif ($route->getDefaults()) { |
||
| 301 | $code .= sprintf(" \$ret = %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true))); |
||
| 302 | } else { |
||
| 303 | $code .= sprintf(" \$ret = array('_route' => '%s');\n", $name); |
||
| 304 | } |
||
| 305 | |||
| 306 | if ($hasTrailingSlash) { |
||
| 307 | $code .= <<<EOF |
||
| 308 | if ('/' === substr(\$pathinfo, -1)) { |
||
| 309 | // no-op |
||
| 310 | } elseif ('GET' !== \$canonicalMethod) { |
||
| 311 | goto $gotoname; |
||
| 312 | } else { |
||
| 313 | return array_replace(\$ret, \$this->redirect(\$rawPathinfo.'/', '$name')); |
||
| 314 | } |
||
| 315 | |||
| 316 | |||
| 317 | EOF; |
||
| 318 | } |
||
| 319 | |||
| 320 | if ($methods) { |
||
| 321 | $methodVariable = in_array('GET', $methods) ? '$canonicalMethod' : '$requestMethod'; |
||
| 322 | $methods = implode("', '", $methods); |
||
| 323 | } |
||
| 324 | |||
| 325 | if ($schemes = $route->getSchemes()) { |
||
| 326 | if (!$supportsRedirections) { |
||
| 327 | throw new \LogicException('The "schemes" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.'); |
||
| 328 | } |
||
| 329 | $schemes = str_replace("\n", '', var_export(array_flip($schemes), true)); |
||
| 330 | if ($methods) { |
||
| 331 | $code .= <<<EOF |
||
| 332 | \$requiredSchemes = $schemes; |
||
| 333 | \$hasRequiredScheme = isset(\$requiredSchemes[\$context->getScheme()]); |
||
| 334 | if (!in_array($methodVariable, array('$methods'))) { |
||
| 335 | if (\$hasRequiredScheme) { |
||
| 336 | \$allow = array_merge(\$allow, array('$methods')); |
||
| 337 | } |
||
| 338 | goto $gotoname; |
||
| 339 | } |
||
| 340 | if (!\$hasRequiredScheme) { |
||
| 341 | if ('GET' !== \$canonicalMethod) { |
||
| 342 | goto $gotoname; |
||
| 343 | } |
||
| 344 | |||
| 345 | return array_replace(\$ret, \$this->redirect(\$rawPathinfo, '$name', key(\$requiredSchemes))); |
||
| 346 | } |
||
| 347 | |||
| 348 | |||
| 349 | EOF; |
||
| 350 | } else { |
||
| 351 | $code .= <<<EOF |
||
| 352 | \$requiredSchemes = $schemes; |
||
| 353 | if (!isset(\$requiredSchemes[\$context->getScheme()])) { |
||
| 354 | if ('GET' !== \$canonicalMethod) { |
||
| 355 | goto $gotoname; |
||
| 356 | } |
||
| 357 | |||
| 358 | return array_replace(\$ret, \$this->redirect(\$rawPathinfo, '$name', key(\$requiredSchemes))); |
||
| 359 | } |
||
| 360 | |||
| 361 | |||
| 362 | EOF; |
||
| 363 | } |
||
| 364 | } elseif ($methods) { |
||
| 365 | $code .= <<<EOF |
||
| 366 | if (!in_array($methodVariable, array('$methods'))) { |
||
| 367 | \$allow = array_merge(\$allow, array('$methods')); |
||
| 368 | goto $gotoname; |
||
| 369 | } |
||
| 370 | |||
| 371 | |||
| 372 | EOF; |
||
| 373 | } |
||
| 374 | |||
| 375 | if ($hasTrailingSlash || $schemes || $methods) { |
||
| 376 | $code .= " return \$ret;\n"; |
||
| 377 | } else { |
||
| 378 | $code = substr_replace($code, 'return', $retOffset, 6); |
||
| 379 | } |
||
| 380 | $code .= " }\n"; |
||
| 381 | |||
| 382 | if ($hasTrailingSlash || $schemes || $methods) { |
||
| 383 | $code .= " $gotoname:\n"; |
||
| 384 | } |
||
| 385 | |||
| 386 | return $code; |
||
| 387 | } |
||
| 430 |