Conditions | 8 |
Paths | 27 |
Total Lines | 51 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | 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 |
||
37 | public function relativePathFor( $name, array $data = [], array $queryParams = [] ) |
||
38 | { |
||
39 | $route = $this->getNamedRoute($name); |
||
40 | $pattern = $route->getPattern(); |
||
41 | |||
42 | $routeDatas = $this->routeParser->parse($pattern); |
||
43 | // $routeDatas is an array of all possible routes that can be made. There is |
||
44 | // one routedata for each optional parameter plus one for no optional parameters. |
||
45 | // |
||
46 | // The most specific is last, so we look for that first. |
||
47 | $routeDatas = array_reverse($routeDatas); |
||
48 | |||
49 | $segments = $segmentKeys = []; |
||
50 | foreach ( $routeDatas as $routeData ) { |
||
51 | foreach ( $routeData as $item ) { |
||
52 | if ( is_string($item) ) { |
||
53 | // this segment is a static string |
||
54 | $segments[] = $item; |
||
55 | continue; |
||
56 | } |
||
57 | |||
58 | // This segment has a parameter: first element is the name |
||
59 | if ( !array_key_exists($item[0], $data) ) { |
||
60 | // we don't have a data element for this segment: cancel |
||
61 | // testing this routeData item, so that we can try a less |
||
62 | // specific routeData item. |
||
63 | $segments = []; |
||
64 | $segmentName = $item[0]; |
||
65 | break; |
||
66 | } |
||
67 | $segments[] = $data[$item[0]]; |
||
68 | $segmentKeys[$item[0]] = true; |
||
69 | } |
||
70 | if ( !empty($segments) ) { |
||
71 | // we found all the parameters for this route data, no need to check |
||
72 | // less specific ones |
||
73 | break; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | if ( empty($segments) ) { |
||
78 | throw new InvalidArgumentException('Missing data for URL segment: ' . $segmentName); |
||
|
|||
79 | } |
||
80 | $url = implode('', $segments); |
||
81 | |||
82 | $params = array_merge(array_diff_key($data, $segmentKeys), $queryParams); |
||
83 | if ( $params ) { |
||
84 | $url .= '?' . http_build_query($params); |
||
85 | } |
||
86 | |||
87 | return $url; |
||
88 | } |
||
90 |