| Conditions | 38 |
| Paths | 438 |
| Total Lines | 143 |
| Code Lines | 107 |
| 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 | protected function parse() |
||
| 129 | { |
||
| 130 | // Normalize new lines. |
||
| 131 | $this->content = str_replace(["\r\n", "\r"], "\n", $this->content); |
||
| 132 | |||
| 133 | // RFC2425 5.8.1. Line delimiting and folding |
||
| 134 | // Unfolding is accomplished by regarding CRLF immediately followed by |
||
| 135 | // a white space character (namely HTAB ASCII decimal 9 or. SPACE ASCII |
||
| 136 | // decimal 32) as equivalent to no characters at all (i.e., the CRLF |
||
| 137 | // and single white space character are removed). |
||
| 138 | $this->content = preg_replace("/\n(?:[ \t])/", "", $this->content); |
||
| 139 | $lines = explode("\n", $this->content); |
||
| 140 | |||
| 141 | // Parse the VCard, line by line. |
||
| 142 | foreach ($lines as $line) { |
||
| 143 | $line = trim($line); |
||
| 144 | |||
| 145 | if (strtoupper($line) == "BEGIN:VCARD") { |
||
| 146 | $cardData = new \stdClass(); |
||
| 147 | } elseif (strtoupper($line) == "END:VCARD") { |
||
| 148 | $this->vcardObjects[] = $cardData; |
||
| 149 | } elseif (!empty($line)) { |
||
| 150 | // Strip grouping information. We don't use the group names. We |
||
| 151 | // simply use a list for entries that have multiple values. |
||
| 152 | // As per RFC, group names are alphanumerical, and end with a |
||
| 153 | // period (.). |
||
| 154 | $line = preg_replace('/^\w+\./', '', $line); |
||
| 155 | |||
| 156 | $type = ''; |
||
| 157 | $value = ''; |
||
| 158 | @list($type, $value) = explode(':', $line, 2); |
||
| 159 | |||
| 160 | $types = explode(';', $type); |
||
| 161 | $element = strtoupper($types[0]); |
||
| 162 | |||
| 163 | array_shift($types); |
||
| 164 | |||
| 165 | // Normalize types. A type can either be a type-param directly, |
||
| 166 | // or can be prefixed with "type=". E.g.: "INTERNET" or |
||
| 167 | // "type=INTERNET". |
||
| 168 | if (!empty($types)) { |
||
| 169 | $types = array_map(function($type) { |
||
| 170 | return preg_replace('/^type=/i', '', $type); |
||
| 171 | }, $types); |
||
| 172 | } |
||
| 173 | |||
| 174 | $i = 0; |
||
| 175 | $rawValue = false; |
||
| 176 | foreach ($types as $type) { |
||
| 177 | if (preg_match('/base64/', strtolower($type))) { |
||
| 178 | $value = base64_decode($value); |
||
| 179 | unset($types[$i]); |
||
| 180 | $rawValue = true; |
||
| 181 | } elseif (preg_match('/encoding=b/', strtolower($type))) { |
||
| 182 | $value = base64_decode($value); |
||
| 183 | unset($types[$i]); |
||
| 184 | $rawValue = true; |
||
| 185 | } elseif (preg_match('/quoted-printable/', strtolower($type))) { |
||
| 186 | $value = quoted_printable_decode($value); |
||
| 187 | unset($types[$i]); |
||
| 188 | $rawValue = true; |
||
| 189 | } elseif (strpos(strtolower($type), 'charset=') === 0) { |
||
| 190 | try { |
||
| 191 | $value = mb_convert_encoding($value, "UTF-8", substr($type, 8)); |
||
| 192 | } catch (\Exception $e) { |
||
| 193 | } |
||
| 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 = []; |
||
| 214 | } |
||
| 215 | $key = !empty($types) ? implode(';', $types) : 'WORK;POSTAL'; |
||
| 216 | $cardData->address[$key][] = $this->parseAddress($value); |
||
| 217 | break; |
||
| 218 | case 'TEL': |
||
| 219 | if (!isset($cardData->phone)) { |
||
| 220 | $cardData->phone = []; |
||
| 221 | } |
||
| 222 | $key = !empty($types) ? implode(';', $types) : 'default'; |
||
| 223 | $cardData->phone[$key][] = $value; |
||
| 224 | break; |
||
| 225 | case 'EMAIL': |
||
| 226 | if (!isset($cardData->email)) { |
||
| 227 | $cardData->email = []; |
||
| 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 | case 'URL': |
||
| 242 | if (!isset($cardData->url)) { |
||
| 243 | $cardData->url = []; |
||
| 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 'LOGO': |
||
| 259 | if ($rawValue) { |
||
| 260 | $cardData->rawLogo = $value; |
||
| 261 | } else { |
||
| 262 | $cardData->logo = $value; |
||
| 263 | } |
||
| 264 | break; |
||
| 265 | case 'NOTE': |
||
| 266 | $cardData->note = $this->unescape($value); |
||
| 267 | break; |
||
| 268 | case 'CATEGORIES': |
||
| 269 | $cardData->categories = array_map('trim', explode(',', $value)); |
||
| 270 | break; |
||
| 271 | } |
||
| 334 |