Conditions | 73 |
Paths | 2 |
Total Lines | 319 |
Code Lines | 179 |
Lines | 48 |
Ratio | 15.05 % |
Changes | 38 | ||
Bugs | 5 | Features | 7 |
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 |
||
198 | public function get_carddav_entries() |
||
199 | { |
||
200 | $entries = array(); |
||
201 | $snum = 0; |
||
202 | |||
203 | if(is_array($this->config['carddav'])) |
||
204 | { |
||
205 | foreach($this->config['carddav'] as $conf) |
||
206 | { |
||
207 | print " [" . $snum . "]: " . $conf['url'] . " "; |
||
208 | $carddav = new CardDavPHP\CardDavBackend($conf['url']); |
||
209 | $carddav->setAuth($conf['user'], $conf['pw']); |
||
210 | |||
211 | // set the vcard extension in case the user |
||
212 | // defined it in the config |
||
213 | if(isset($conf['extension'])) |
||
214 | $carddav->setVcardExtension($conf['extension']); |
||
215 | |||
216 | // retrieve data from the CardDAV server now |
||
217 | $xmldata = $carddav->get(); |
||
218 | |||
219 | // identify if we received UTF-8 encoded data from the |
||
220 | // CardDAV server and if not reencode it since the FRITZ!Box |
||
221 | // requires UTF-8 encoded data |
||
222 | if(iconv('utf-8', 'utf-8//IGNORE', $xmldata) != $xmldata) |
||
223 | $xmldata = utf8_encode($xmldata); |
||
224 | |||
225 | // read raw_vcard data from xml response |
||
226 | $raw_vcards = array(); |
||
227 | $xmlvcard = new SimpleXMLElement($xmldata); |
||
228 | |||
229 | foreach($xmlvcard->element as $vcard_element) |
||
230 | { |
||
231 | $id = $vcard_element->id->__toString(); |
||
232 | $value = (string)$vcard_element->vcard->__toString(); |
||
233 | $raw_vcards[$id] = $value; |
||
234 | } |
||
235 | |||
236 | print " " . count($raw_vcards) . " VCards retrieved." . PHP_EOL; |
||
237 | |||
238 | // parse raw_vcards |
||
239 | $quick_dial_arr = array(); |
||
240 | foreach($raw_vcards as $v) |
||
241 | { |
||
242 | $vcard_obj = new vCard(false, $v); |
||
243 | $name_arr = null; |
||
244 | if(isset($vcard_obj->n[0])) |
||
|
|||
245 | $name_arr = $vcard_obj->n[0]; |
||
246 | $org_arr = null; |
||
247 | if(isset($vcard_obj->org[0])) |
||
248 | $org_arr = $vcard_obj->org[0]; |
||
249 | $addnames = ''; |
||
250 | $prefix = ''; |
||
251 | $suffix = ''; |
||
252 | $orgname = ''; |
||
253 | $formattedname = ''; |
||
254 | |||
255 | // Build name Parts if existing ans switch to true in config |
||
256 | if(isset($name_arr['prefixes']) and $this->config['prefix']) |
||
257 | $prefix = trim($name_arr['prefixes']); |
||
258 | |||
259 | if(isset($name_arr['suffixes']) and $this->config['suffix']) |
||
260 | $suffix = trim($name_arr['suffixes']); |
||
261 | |||
262 | if(isset($name_arr['additionalnames']) and $this->config['addnames']) |
||
263 | $addnames = trim($name_arr['additionalnames']); |
||
264 | |||
265 | if(isset($org_arr['name']) and $this->config['orgname']) |
||
266 | $orgname = trim($org_arr['name']); |
||
267 | |||
268 | if (isset($vcard_obj->fn[0])) |
||
269 | $formattedname = $vcard_obj->fn[0]; |
||
270 | |||
271 | $firstname = trim($name_arr['firstname']); |
||
272 | $lastname = trim($name_arr['lastname']); |
||
273 | |||
274 | // the following section implemented different ways of constructing the |
||
275 | // final phonebook name entry depending on user preferred settings |
||
276 | // selectable in the config file. Possible options are: |
||
277 | // |
||
278 | // $this->config['fullname_format']: |
||
279 | // |
||
280 | // 0: "Prefix Lastname, Firstname AdditionalNames Suffix (orgname)" |
||
281 | // 1: "Prefix Firstname Lastname AdditionalNames Suffix (orgname)" |
||
282 | // 2: "Prefix Firstname AdditionalNames Lastname Suffix (orgname)" |
||
283 | // |
||
284 | $name = ''; |
||
285 | $format = $this->config['fullname_format']; |
||
286 | |||
287 | // Prefix |
||
288 | if(!empty($prefix)) |
||
289 | $name .= $prefix; |
||
290 | |||
291 | View Code Duplication | if($format == 0) |
|
292 | { |
||
293 | // Lastname |
||
294 | if(!empty($name) and !empty($lastname)) |
||
295 | $name .= ' ' . $lastname; |
||
296 | else |
||
297 | $name .= $lastname; |
||
298 | } |
||
299 | else |
||
300 | { |
||
301 | // Firstname |
||
302 | if(!empty($name) and !empty($firstname)) |
||
303 | $name .= ' ' . $firstname; |
||
304 | else |
||
305 | $name .= $firstname; |
||
306 | } |
||
307 | |||
308 | View Code Duplication | if($format == 2) |
|
309 | { |
||
310 | // AdditionalNames |
||
311 | if(!empty($name) and !empty($addnames)) |
||
312 | $name .= ' ' . $addnames; |
||
313 | else |
||
314 | $name .= $addnames; |
||
315 | } |
||
316 | |||
317 | View Code Duplication | if($format == 0) |
|
318 | { |
||
319 | // Firstname |
||
320 | if(!empty($name) and !empty($firstname)) |
||
321 | $name .= ', ' . $firstname; |
||
322 | else |
||
323 | $name .= $firstname; |
||
324 | } |
||
325 | else |
||
326 | { |
||
327 | // Lastname |
||
328 | if(!empty($name) and !empty($lastname)) |
||
329 | $name .= ' ' . $lastname; |
||
330 | else |
||
331 | $name .= $lastname; |
||
332 | } |
||
333 | |||
334 | View Code Duplication | if($format != 2) |
|
335 | { |
||
336 | // AdditionalNames |
||
337 | if(!empty($name) and !empty($addnames)) |
||
338 | $name .= ' ' . $addnames; |
||
339 | else |
||
340 | $name .= $addnames; |
||
341 | } |
||
342 | |||
343 | // Suffix |
||
344 | if(!empty($name) and !empty($suffix)) |
||
345 | $name .= ' ' . $suffix; |
||
346 | else |
||
347 | $name .= $suffix; |
||
348 | |||
349 | // OrgName |
||
350 | if(!empty($name) and !empty($orgname)) |
||
351 | $name .= ' (' . $orgname . ')'; |
||
352 | else |
||
353 | $name .= $orgname; |
||
354 | |||
355 | // make sure to trim whitespaces and double spaces |
||
356 | $name = trim(str_replace(' ', ' ', $name)); |
||
357 | |||
358 | // perform a fallback to formatted name, if we don't have any name and formatted name is available |
||
359 | if(empty($name) and !empty($formattedname)) |
||
360 | $name = $formattedname; |
||
361 | |||
362 | if(empty($name)) |
||
363 | { |
||
364 | print ' WARNING: No fullname, lastname, orgname or formatted name found!' . PHP_EOL; |
||
365 | $name = 'UNKNOWN'; |
||
366 | } |
||
367 | |||
368 | // format filename of contact photo; remove special letters |
||
369 | if($vcard_obj->photo) |
||
370 | { |
||
371 | $photo = str_replace(array(',', '&', ' ', '/', 'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß', 'á', 'à', 'ó', 'ò', 'ú', 'ù', 'í', 'ø'), |
||
372 | array('', '_', '_', '_', 'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss', 'a', 'a', 'o', 'o', 'u', 'u', 'i', 'oe'), $name); |
||
373 | } |
||
374 | else |
||
375 | $photo = ''; |
||
376 | |||
377 | // phone |
||
378 | $phone_no = array(); |
||
379 | if($vcard_obj->categories) |
||
380 | $categories = $vcard_obj->categories[0]; |
||
381 | else |
||
382 | $categories = array(); |
||
383 | |||
384 | // check for quickdial entry |
||
385 | if(isset($vcard_obj->note[0])) |
||
386 | { |
||
387 | $note = $vcard_obj->note[0]; |
||
388 | $notes = explode($this->config['quickdial_keyword'], $note); |
||
389 | foreach($notes as $linenr => $linecontent) |
||
390 | { |
||
391 | $found = strrpos($linecontent, ":**7"); |
||
392 | if($found > 0) |
||
393 | { |
||
394 | $pos_qd_start = strrpos($linecontent, ":**7"); |
||
395 | $quick_dial_for_nr = preg_replace("/[^0-9+]/", "", substr($linecontent, 0, $pos_qd_start)); |
||
396 | $quick_dial_nr = intval(substr($linecontent, $pos_qd_start + 4, 3)); |
||
397 | $quick_dial_arr[$quick_dial_for_nr] = $quick_dial_nr; |
||
398 | } |
||
399 | } |
||
400 | } |
||
401 | |||
402 | // e-mail addresses |
||
403 | $email_add = array(); |
||
404 | $vip = isset($this->config['group_vip']) && in_array((string)$this->config['group_vip'], $categories); |
||
405 | |||
406 | if(array_key_exists('group_filter', $this->config) && is_array($this->config['group_filter'])) |
||
407 | { |
||
408 | $add_entry = 0; |
||
409 | foreach($this->config['group_filter'] as $group_filter) |
||
410 | { |
||
411 | if(in_array($group_filter, $categories)) |
||
412 | { |
||
413 | $add_entry = 1; |
||
414 | break; |
||
415 | } |
||
416 | } |
||
417 | } |
||
418 | else |
||
419 | $add_entry = 1; |
||
420 | |||
421 | if($add_entry == 1) |
||
422 | { |
||
423 | foreach($vcard_obj->tel as $t) |
||
424 | { |
||
425 | $prio = 0; |
||
426 | $quickdial = null; |
||
427 | |||
428 | if(!is_array($t) || empty($t['type'])) |
||
429 | { |
||
430 | $type = "mobile"; |
||
431 | $phone_number = $t; |
||
432 | } |
||
433 | else |
||
434 | { |
||
435 | $phone_number = $t['value']; |
||
436 | |||
437 | $phone_number_clean = preg_replace("/[^0-9+]/", "", $phone_number); |
||
438 | foreach($quick_dial_arr as $qd_phone_nr => $value) |
||
439 | { |
||
440 | if($qd_phone_nr == $phone_number_clean) |
||
441 | { |
||
442 | //Set quickdial |
||
443 | if($value == 1) |
||
444 | print "\nWARNING: Quickdial value 1 (**701) is not possible but used! \n"; |
||
445 | elseif($value >= 100) |
||
446 | print "\nWARNING: Quickdial value bigger than 99 (**799) is not possible but used! \n"; |
||
447 | |||
448 | $quickdial = $value; |
||
449 | } |
||
450 | } |
||
451 | |||
452 | $typearr_lower = unserialize(strtolower(serialize($t['type']))); |
||
453 | |||
454 | // find out priority |
||
455 | if(in_array("pref", $typearr_lower)) |
||
456 | $prio = 1; |
||
457 | |||
458 | // set the proper type |
||
459 | if(in_array("cell", $typearr_lower)) |
||
460 | $type = "mobile"; |
||
461 | elseif(in_array("home", $typearr_lower)) |
||
462 | $type = "home"; |
||
463 | elseif(in_array("fax", $typearr_lower)) |
||
464 | $type = "fax_work"; |
||
465 | elseif(in_array("work", $typearr_lower)) |
||
466 | $type = "work"; |
||
467 | elseif(in_array("other", $typearr_lower)) |
||
468 | $type = "other"; |
||
469 | elseif(in_array("dom", $typearr_lower)) |
||
470 | $type = "other"; |
||
471 | else |
||
472 | continue; |
||
473 | } |
||
474 | $phone_no[] = array("type"=>$type, "prio"=>$prio, "quickdial"=>$quickdial, "value" => $this->_clear_phone_number($phone_number)); |
||
475 | } |
||
476 | |||
477 | // request email address and type |
||
478 | if($vcard_obj->email) |
||
479 | { |
||
480 | foreach($vcard_obj->email as $e) |
||
481 | { |
||
482 | if(empty($e['type'])) |
||
483 | { |
||
484 | $type_email = "work"; |
||
485 | $email = $e; |
||
486 | } |
||
487 | else |
||
488 | { |
||
489 | $email = $e['value']; |
||
490 | $typearr_lower = unserialize(strtolower(serialize($e['type']))); |
||
491 | if(in_array("work", $typearr_lower)) |
||
492 | $type_email = "work"; |
||
493 | elseif(in_array("home", $typearr_lower)) |
||
494 | $type_email = "home"; |
||
495 | elseif(in_array("other", $typearr_lower)) |
||
496 | $type_email = "other"; |
||
497 | else |
||
498 | continue; |
||
499 | } |
||
500 | |||
501 | // DEBUG: print out the email address on the console |
||
502 | //print $type_email.": ".$email."\n"; |
||
503 | |||
504 | $email_add[] = array("type"=>$type_email, "value" => $email); |
||
505 | } |
||
506 | } |
||
507 | $entries[] = array("realName" => $name, "telephony" => $phone_no, "email" => $email_add, "vip" => $vip, "photo" => $photo, "photo_data" => $vcard_obj->photo); |
||
508 | } |
||
509 | } |
||
510 | |||
511 | $snum++; |
||
512 | } |
||
513 | } |
||
514 | |||
515 | $this->entries = $entries; |
||
516 | } |
||
517 | |||
830 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.