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