Conditions | 48 |
Paths | > 20000 |
Total Lines | 166 |
Code Lines | 96 |
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 |
||
262 | public function gen($route, array $params = array(), $options = array()) |
||
263 | { |
||
264 | /** @var WebRequest $req */ |
||
265 | $req = $this->context->getRequest(); |
||
266 | |||
267 | if (substr($route, -1) == '*') { |
||
268 | $options['refill_all_parameters'] = true; |
||
269 | $route = substr($route, 0, -1); |
||
270 | } |
||
271 | |||
272 | $options = $this->resolveGenOptions($options); |
||
273 | |||
274 | $aso = $this->argSeparatorOutput; |
||
275 | if ($options['separator'] != $aso) { |
||
276 | $aso = $options['separator']; |
||
277 | } |
||
278 | |||
279 | if ($options['use_trans_sid'] === true && defined('SID') && SID !== '') { |
||
280 | $params = array_merge($params, array(session_name() => session_id())); |
||
281 | } |
||
282 | |||
283 | if ($route === null && empty($params)) { |
||
284 | $retval = $req->getRequestUri(); |
||
285 | $retval = str_replace(array('[', ']', '\''), array('%5B', '%5D', '%27'), $retval); |
||
286 | // much quicker than str_replace($this->argSeparatorInput, array_fill(0, count($this->argSeparatorInput), $aso), $retval) |
||
287 | foreach ($this->argSeparatorInput as $char) { |
||
288 | $retval = str_replace($char, $aso, $retval); |
||
289 | } |
||
290 | } else { |
||
291 | if ($this->isEnabled()) { |
||
292 | // the route exists and routing is enabled, the parent method handles it |
||
293 | |||
294 | $append = ''; |
||
295 | |||
296 | list($path, $usedParams, $options, $extraParams, $isNullRoute) = parent::gen($route, $params, $options); |
||
297 | |||
298 | if ($isNullRoute) { |
||
299 | // add the incoming parameters from the request uri for gen(null) and friends |
||
300 | $extraParams = array_merge($this->inputParameters, $extraParams); |
||
301 | } |
||
302 | if (count($extraParams) > 0) { |
||
303 | $append = http_build_query($extraParams, '', $aso); |
||
304 | if ($append !== '') { |
||
305 | $append = '?' . $append; |
||
306 | } |
||
307 | } |
||
308 | } else { |
||
309 | // the route exists, but we must create a normal index.php?foo=bar URL. |
||
310 | |||
311 | $isNullRoute = false; |
||
312 | $routes = $this->getAffectedRoutes($route, $isNullRoute); |
||
313 | if ($isNullRoute) { |
||
314 | $params = array_merge($this->inputParameters, $params); |
||
315 | } |
||
316 | if (count($routes) == 0) { |
||
317 | $path = $route; |
||
318 | } |
||
319 | |||
320 | // we collect the default parameters from the route and make sure |
||
321 | // new parameters don't overwrite already defined parameters |
||
322 | $defaults = array(); |
||
323 | |||
324 | $ma = $req->getParameter('module_accessor'); |
||
325 | $aa = $req->getParameter('controller_accessor'); |
||
326 | |||
327 | foreach ($routes as $route) { |
||
328 | if (isset($this->routes[$route])) { |
||
329 | $r = $this->routes[$route]; |
||
330 | $myDefaults = array(); |
||
331 | |||
332 | foreach ($r['opt']['defaults'] as $key => $default) { |
||
333 | $myDefaults[$key] = $default->getValue(); |
||
334 | } |
||
335 | if ($r['opt']['module']) { |
||
336 | $myDefaults[$ma] = $r['opt']['module']; |
||
337 | } |
||
338 | if ($r['opt']['controller']) { |
||
339 | $myDefaults[$aa] = $r['opt']['controller']; |
||
340 | } |
||
341 | |||
342 | $defaults = array_merge($myDefaults, $defaults); |
||
343 | } |
||
344 | } |
||
345 | |||
346 | $params = array_merge($defaults, $params); |
||
347 | } |
||
348 | |||
349 | if (!isset($path)) { |
||
350 | // the route does not exist. we generate a normal index.php?foo=bar URL. |
||
351 | $path = $_SERVER['SCRIPT_NAME']; |
||
352 | } |
||
353 | |||
354 | if (!isset($path)) { |
||
355 | // routing was off; the name of the route is the input |
||
356 | } |
||
357 | if (!isset($append)) { |
||
358 | $append = '?' . http_build_query($params, '', $aso); |
||
359 | } |
||
360 | |||
361 | $retval = $path . $append; |
||
362 | } |
||
363 | |||
364 | if (!$options['relative'] || |
||
365 | ($options['relative'] && ( |
||
366 | $options['scheme'] !== null || |
||
367 | $options['authority'] !== null || |
||
368 | $options['host'] !== null || |
||
369 | $options['port'] !== null |
||
370 | )) |
||
371 | ) { |
||
372 | $scheme = false; |
||
373 | if ($options['scheme'] !== false) { |
||
374 | $scheme = ($options['scheme'] === null ? $req->getUrlScheme() : $options['scheme']); |
||
375 | } |
||
376 | |||
377 | $authority = ''; |
||
378 | |||
379 | if ($options['authority'] === null) { |
||
380 | if ($options['host'] !== null && $options['host'] !== false) { |
||
381 | $authority = $options['host']; |
||
382 | } elseif ($options['host'] === false) { |
||
383 | $authority = ''; |
||
384 | } else { |
||
385 | $authority = $req->getUrlHost(); |
||
386 | } |
||
387 | $port = null; |
||
388 | if ($options['port'] !== null && $options['port'] !== false) { |
||
389 | if (Toolkit::isPortNecessary($options['scheme'] !== null && $options['scheme'] !== false ? $options['scheme'] : $req->getUrlScheme(), $options['port'])) { |
||
390 | $port = $options['port']; |
||
391 | } else { |
||
392 | $port = null; |
||
393 | } |
||
394 | } elseif ($options['port'] === false) { |
||
395 | $port = null; |
||
396 | } elseif ($options['scheme'] === null) { |
||
397 | if (!Toolkit::isPortNecessary($req->getUrlScheme(), $port = $req->getUrlPort())) { |
||
398 | $port = null; |
||
399 | } |
||
400 | } |
||
401 | if ($port !== null) { |
||
402 | $authority .= ':' . $port; |
||
403 | } |
||
404 | } elseif ($options['authority'] !== false) { |
||
405 | $authority = $options['authority']; |
||
406 | } |
||
407 | |||
408 | if ($scheme === false) { |
||
409 | // nothing at all, e.g. when displaying a URL without the "http://" prefix |
||
410 | $scheme = ''; |
||
411 | } elseif (trim($scheme) === '') { |
||
412 | // a protocol-relative URL (see #1224) |
||
413 | $scheme = '//'; |
||
414 | } else { |
||
415 | // given scheme plus "://" |
||
416 | $scheme = $scheme . '://'; |
||
417 | } |
||
418 | |||
419 | $retval = $scheme . $authority . $retval; |
||
420 | } |
||
421 | |||
422 | if ($options['fragment'] !== null) { |
||
423 | $retval .= '#' . $options['fragment']; |
||
424 | } |
||
425 | |||
426 | return $retval; |
||
427 | } |
||
428 | |||
444 |
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: