| Conditions | 10 |
| Paths | 2 |
| Total Lines | 46 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 117 | private function getMatchesFromAcceptedLanguages() |
||
| 118 | { |
||
| 119 | $matches = []; |
||
| 120 | |||
| 121 | if ($acceptLanguages = $this->request->header('Accept-Language')) { |
||
| 122 | $acceptLanguages = explode(',', $acceptLanguages); |
||
| 123 | $generic_matches = []; |
||
| 124 | |||
| 125 | foreach ($acceptLanguages as $option) { |
||
| 126 | $option = array_map('trim', explode(';', $option)); |
||
| 127 | $l = $option[0]; |
||
| 128 | if (isset($option[1])) { |
||
| 129 | $q = (float) str_replace('q=', '', $option[1]); |
||
| 130 | } else { |
||
| 131 | $q = null; |
||
| 132 | // Assign default low weight for generic values |
||
| 133 | if ($l == '*/*') { |
||
| 134 | $q = 0.01; |
||
| 135 | } elseif (substr($l, -1) == '*') { |
||
| 136 | $q = 0.02; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | // Unweighted values, get high weight by their position in the |
||
| 140 | // list |
||
| 141 | $q = isset($q) ? $q : 1000 - count($matches); |
||
| 142 | $matches[$l] = $q; |
||
| 143 | //If for some reason the Accept-Language header only sends language with country |
||
| 144 | //we should make the language without country an accepted option, with a value |
||
| 145 | //less than it's parent. |
||
| 146 | $l_ops = explode('-', $l); |
||
| 147 | array_pop($l_ops); |
||
| 148 | while (! empty($l_ops)) { |
||
| 149 | //The new generic option needs to be slightly less important than it's base |
||
| 150 | $q -= 0.001; |
||
| 151 | $op = implode('-', $l_ops); |
||
| 152 | if (empty($generic_matches[$op]) || $generic_matches[$op] > $q) { |
||
| 153 | $generic_matches[$op] = $q; |
||
| 154 | } |
||
| 155 | array_pop($l_ops); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | $matches = array_merge($generic_matches, $matches); |
||
| 159 | arsort($matches, SORT_NUMERIC); |
||
| 160 | } |
||
| 161 | |||
| 162 | return $matches; |
||
| 163 | } |
||
| 192 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: