| Conditions | 11 |
| Paths | 19 |
| Total Lines | 45 |
| 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 |
||
| 93 | protected function getLanguages() |
||
| 94 | { |
||
| 95 | if ($this->languages !== null) { |
||
| 96 | return $this->languages; |
||
| 97 | } |
||
| 98 | |||
| 99 | $this->languages = []; |
||
| 100 | |||
| 101 | if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
||
| 102 | return $this->languages; |
||
| 103 | } |
||
| 104 | |||
| 105 | foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) { |
||
| 106 | // Cut off any q-value that might come after a semi-colon |
||
| 107 | if ($pos = strpos($lang, ';')) { |
||
| 108 | $lang = trim(substr($lang, 0, $pos)); |
||
| 109 | } |
||
| 110 | |||
| 111 | if (strstr($lang, '-')) { |
||
| 112 | $codes = explode('-', $lang); |
||
| 113 | if ($codes[0] == 'i') { |
||
| 114 | // Language not listed in ISO 639 that are not variants |
||
| 115 | // of any listed language, which can be registerd with the |
||
| 116 | // i-prefix, such as i-cherokee |
||
| 117 | if (count($codes) > 1) { |
||
| 118 | $lang = $codes[1]; |
||
| 119 | } |
||
| 120 | } else { |
||
| 121 | for ($i = 0, $k = count($codes); $i < $k; ++$i) { |
||
| 122 | if ($i == 0) { |
||
| 123 | $lang = strtolower($codes[0]); |
||
| 124 | } else { |
||
| 125 | $lang .= '_' . strtoupper($codes[$i]); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($this->getIsValidLocale($lang)) { |
||
| 132 | $this->languages[] = $lang; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | return $this->languages; |
||
| 137 | } |
||
| 138 | |||
| 181 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: