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 |
||
128 | private function getMatchesFromAcceptedLanguages() |
||
129 | { |
||
130 | $matches = []; |
||
131 | |||
132 | if ($acceptLanguages = $this->request->header('Accept-Language')) { |
||
133 | $acceptLanguages = explode(',', $acceptLanguages); |
||
134 | |||
135 | $generic_matches = []; |
||
136 | foreach ($acceptLanguages as $option) { |
||
137 | $option = array_map('trim', explode(';', $option)); |
||
138 | $l = $option[0]; |
||
139 | if (isset($option[1])) { |
||
140 | $q = (float) str_replace('q=', '', $option[1]); |
||
141 | } else { |
||
142 | $q = null; |
||
143 | // Assign default low weight for generic values |
||
144 | if ($l == '*/*') { |
||
145 | $q = 0.01; |
||
146 | } elseif (substr($l, -1) == '*') { |
||
147 | $q = 0.02; |
||
148 | } |
||
149 | } |
||
150 | // Unweighted values, get high weight by their position in the |
||
151 | // list |
||
152 | $q = isset($q) ? $q : 1000 - count($matches); |
||
153 | $matches[$l] = $q; |
||
154 | |||
155 | //If for some reason the Accept-Language header only sends language with country |
||
156 | //we should make the language without country an accepted option, with a value |
||
157 | //less than it's parent. |
||
158 | $l_ops = explode('-', $l); |
||
159 | array_pop($l_ops); |
||
160 | while (!empty($l_ops)) { |
||
161 | //The new generic option needs to be slightly less important than it's base |
||
162 | $q -= 0.001; |
||
163 | $op = implode('-', $l_ops); |
||
164 | if (empty($generic_matches[$op]) || $generic_matches[$op] > $q) { |
||
165 | $generic_matches[$op] = $q; |
||
166 | } |
||
167 | array_pop($l_ops); |
||
168 | } |
||
169 | } |
||
170 | $matches = array_merge($generic_matches, $matches); |
||
171 | |||
172 | arsort($matches, SORT_NUMERIC); |
||
173 | } |
||
174 | |||
175 | return $matches; |
||
176 | } |
||
177 | } |
||
178 |