| Conditions | 10 |
| Paths | 2 |
| Total Lines | 49 |
| Code Lines | 29 |
| 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 |
||
| 100 | private function getMatchesFromAcceptedLanguages() |
||
| 101 | { |
||
| 102 | $matches = []; |
||
| 103 | |||
| 104 | if ($acceptLanguages = $this->request->header('Accept-Language')) { |
||
| 105 | $acceptLanguages = explode(',', $acceptLanguages); |
||
| 106 | |||
| 107 | $generic_matches = []; |
||
| 108 | foreach ($acceptLanguages as $option) { |
||
| 109 | $option = array_map('trim', explode(';', $option)); |
||
| 110 | $l = $option[0]; |
||
| 111 | if (isset($option[1])) { |
||
| 112 | $q = (float) str_replace('q=', '', $option[1]); |
||
| 113 | } else { |
||
| 114 | $q = null; |
||
| 115 | // Assign default low weight for generic values |
||
| 116 | if ($l == '*/*') { |
||
| 117 | $q = 0.01; |
||
| 118 | } elseif (substr($l, -1) == '*') { |
||
| 119 | $q = 0.02; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | // Unweighted values, get high weight by their position in the |
||
| 123 | // list |
||
| 124 | $q = isset($q) ? $q : 1000 - count($matches); |
||
| 125 | $matches[$l] = $q; |
||
| 126 | |||
| 127 | //If for some reason the Accept-Language header only sends language with country |
||
| 128 | //we should make the language without country an accepted option, with a value |
||
| 129 | //less than it's parent. |
||
| 130 | $l_ops = explode('-', $l); |
||
| 131 | array_pop($l_ops); |
||
| 132 | while (!empty($l_ops)) { |
||
| 133 | //The new generic option needs to be slightly less important than it's base |
||
| 134 | $q -= 0.001; |
||
| 135 | $op = implode('-', $l_ops); |
||
| 136 | if (empty($generic_matches[$op]) || $generic_matches[$op] > $q) { |
||
| 137 | $generic_matches[$op] = $q; |
||
| 138 | } |
||
| 139 | array_pop($l_ops); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | $matches = array_merge($generic_matches, $matches); |
||
| 143 | |||
| 144 | arsort($matches, SORT_NUMERIC); |
||
| 145 | } |
||
| 146 | |||
| 147 | return $matches; |
||
| 148 | } |
||
| 149 | } |
||
| 150 |