| Conditions | 16 |
| Paths | 278 |
| Total Lines | 72 |
| Code Lines | 42 |
| 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 |
||
| 55 | public function rewrite(array $urls, $mode) |
||
| 56 | { |
||
| 57 | $mappings = $this->aliasing->getAliasingMap( |
||
| 58 | array_map( |
||
| 59 | [$this, 'extractPath'], |
||
| 60 | $urls |
||
| 61 | ), |
||
| 62 | $mode |
||
| 63 | ); |
||
| 64 | |||
| 65 | $ret = []; |
||
| 66 | foreach ($urls as $url) { |
||
| 67 | if (isset($mappings[$url])) { |
||
| 68 | // early match, if the value can be mapping directly, used that. |
||
| 69 | $ret[$url] = $mappings[$url]; |
||
| 70 | continue; |
||
| 71 | } |
||
| 72 | |||
| 73 | $parts = $this->parseUrl($url); |
||
| 74 | |||
| 75 | if (!isset($parts['path'])) { |
||
| 76 | // no path, nothing to map. |
||
| 77 | $ret[$url] = $url; |
||
| 78 | continue; |
||
| 79 | } |
||
| 80 | |||
| 81 | if (isset($parts['host'])) { |
||
| 82 | if (!in_array($parts['host'], $this->localDomains)) { |
||
| 83 | // external url, don't do mapping. |
||
| 84 | $ret[$url] = $url; |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | // don't rewrite this. |
||
| 90 | if (isset($parts['user']) || isset($parts['password'])) { |
||
| 91 | $ret[$url]= $url; |
||
| 92 | continue; |
||
| 93 | } |
||
| 94 | |||
| 95 | $rewritten = ''; |
||
| 96 | if (isset($parts['scheme'])) { |
||
| 97 | $rewritten .= $parts['scheme'] . ':'; |
||
| 98 | } |
||
| 99 | if (isset($parts['host'])) { |
||
| 100 | $rewritten .= '//' . $parts['host']; |
||
| 101 | } |
||
| 102 | if (isset($parts['port'])) { |
||
| 103 | $rewritten .= ':' . $parts['port']; |
||
| 104 | } |
||
| 105 | if (isset($parts['path'])) { |
||
| 106 | if (isset($mappings[$parts['path']])) { |
||
| 107 | $rewritten .= $mappings[$parts['path']]; |
||
| 108 | } else { |
||
| 109 | // no match on path level, keep as-is |
||
| 110 | $ret[$url] = $url; |
||
| 111 | continue; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | if (isset($parts['params'])) { |
||
| 115 | $rewritten .= '/' . $parts['params']; |
||
| 116 | } |
||
| 117 | if (isset($parts['query'])) { |
||
| 118 | $rewritten .= '?' . $parts['query']; |
||
| 119 | } |
||
| 120 | if (isset($parts['fragment'])) { |
||
| 121 | $rewritten .= '#' . $parts['fragment']; |
||
| 122 | } |
||
| 123 | |||
| 124 | $ret[$url] = $rewritten; |
||
| 125 | } |
||
| 126 | return $ret; |
||
| 127 | } |
||
| 215 |