| Conditions | 34 | 
| Paths | 200 | 
| Total Lines | 120 | 
| Code Lines | 94 | 
| Lines | 21 | 
| Ratio | 17.5 % | 
| Changes | 2 | ||
| Bugs | 0 | Features | 1 | 
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 | ||
| 145 | protected function parse() | ||
| 146 |     { | ||
| 147 | // Normalize new lines. | ||
| 148 |         $this->content = str_replace(array("\r\n", "\r"), "\n", $this->content); | ||
| 149 | |||
| 150 | // RFC2425 5.8.1. Line delimiting and folding | ||
| 151 | // Unfolding is accomplished by regarding CRLF immediately followed by | ||
| 152 | // a white space character (namely HTAB ASCII decimal 9 or. SPACE ASCII | ||
| 153 | // decimal 32) as equivalent to no characters at all (i.e., the CRLF | ||
| 154 | // and single white space character are removed). | ||
| 155 |         $this->content = preg_replace("/\n(?:[ \t])/", "", $this->content); | ||
| 156 |         $lines = explode("\n", $this->content); | ||
| 157 | |||
| 158 | // Parse the VCard, line by line. | ||
| 159 |         foreach ($lines as $line) { | ||
| 160 | $line = trim($line); | ||
| 161 | |||
| 162 |             if (strtoupper($line) == "BEGIN:VCARD") { | ||
| 163 | $cardData = new \stdClass(); | ||
| 164 |             } elseif (strtoupper($line) == "END:VCARD") { | ||
| 165 | $this->vcardObjects[] = $cardData; | ||
|  | |||
| 166 |             } elseif (!empty($line)) { | ||
| 167 | $type = ''; | ||
| 168 | $value = ''; | ||
| 169 |                 @list($type, $value) = explode(':', $line, 2); | ||
| 170 | |||
| 171 |                 $types = explode(';', $type); | ||
| 172 | $element = strtoupper($types[0]); | ||
| 173 | |||
| 174 | array_shift($types); | ||
| 175 | $i = 0; | ||
| 176 | $rawValue = false; | ||
| 177 |                 foreach ($types as $type) { | ||
| 178 |                     if (preg_match('/base64/', strtolower($type))) { | ||
| 179 | $value = base64_decode($value); | ||
| 180 | unset($types[$i]); | ||
| 181 | $rawValue = true; | ||
| 182 |                     } elseif (preg_match('/encoding=b/', strtolower($type))) { | ||
| 183 | $value = base64_decode($value); | ||
| 184 | unset($types[$i]); | ||
| 185 | $rawValue = true; | ||
| 186 |                     } elseif (preg_match('/quoted-printable/', strtolower($type))) { | ||
| 187 | $value = quoted_printable_decode($value); | ||
| 188 | unset($types[$i]); | ||
| 189 | $rawValue = true; | ||
| 190 |                     } elseif (strpos(strtolower($type), 'charset=') === 0) { | ||
| 191 |                         try { | ||
| 192 | $value = mb_convert_encoding($value, "UTF-8", substr($type, 8)); | ||
| 193 |                         } catch (\Exception $e) { } | ||
| 194 | unset($types[$i]); | ||
| 195 | } | ||
| 196 | $i++; | ||
| 197 | } | ||
| 198 | |||
| 199 |                 switch (strtoupper($element)) { | ||
| 200 | case 'FN': | ||
| 201 | $cardData->fullname = $value; | ||
| 202 | break; | ||
| 203 | case 'N': | ||
| 204 |                         foreach($this->parseName($value) as $key => $val) { | ||
| 205 |                             $cardData->{$key} = $val; | ||
| 206 | } | ||
| 207 | break; | ||
| 208 | case 'BDAY': | ||
| 209 | $cardData->birthday = $this->parseBirthday($value); | ||
| 210 | break; | ||
| 211 | case 'ADR': | ||
| 212 |                         if (!isset($cardData->address)) { | ||
| 213 | $cardData->address = array(); | ||
| 214 | } | ||
| 215 |                         $key = !empty($types) ? implode(';', $types) : 'WORK;POSTAL'; | ||
| 216 | $cardData->address[$key][] = $this->parseAddress($value); | ||
| 217 | break; | ||
| 218 | View Code Duplication | case 'TEL': | |
| 219 |                         if (!isset($cardData->phone)) { | ||
| 220 | $cardData->phone = array(); | ||
| 221 | } | ||
| 222 |                         $key = !empty($types) ? implode(';', $types) : 'default'; | ||
| 223 | $cardData->phone[$key][] = $value; | ||
| 224 | break; | ||
| 225 | View Code Duplication | case 'EMAIL': | |
| 226 |                         if (!isset($cardData->email)) { | ||
| 227 | $cardData->email = array(); | ||
| 228 | } | ||
| 229 |                         $key = !empty($types) ? implode(';', $types) : 'default'; | ||
| 230 | $cardData->email[$key][] = $value; | ||
| 231 | break; | ||
| 232 | case 'REV': | ||
| 233 | $cardData->revision = $value; | ||
| 234 | break; | ||
| 235 | case 'VERSION': | ||
| 236 | $cardData->version = $value; | ||
| 237 | break; | ||
| 238 | case 'ORG': | ||
| 239 | $cardData->organization = $value; | ||
| 240 | break; | ||
| 241 | View Code Duplication | case 'URL': | |
| 242 |                         if (!isset($cardData->url)) { | ||
| 243 | $cardData->url = array(); | ||
| 244 | } | ||
| 245 |                         $key = !empty($types) ? implode(';', $types) : 'default'; | ||
| 246 | $cardData->url[$key][] = $value; | ||
| 247 | break; | ||
| 248 | case 'TITLE': | ||
| 249 | $cardData->title = $value; | ||
| 250 | break; | ||
| 251 | case 'PHOTO': | ||
| 252 |                         if ($rawValue) { | ||
| 253 | $cardData->rawPhoto = $value; | ||
| 254 |                         } else { | ||
| 255 | $cardData->photo = $value; | ||
| 256 | } | ||
| 257 | break; | ||
| 258 | case 'NOTE': | ||
| 259 | $cardData->note = $this->unescape($value); | ||
| 260 | break; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 324 | 
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: