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