Conditions | 57 |
Paths | 168 |
Total Lines | 223 |
Code Lines | 148 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
385 | function simplifyRestriction($restriction) { |
||
386 | if (!is_array($restriction)) { |
||
387 | return $restriction; |
||
388 | } |
||
389 | |||
390 | switch ($restriction[0]) { |
||
391 | case RES_AND: |
||
392 | $restriction[0] = "RES_AND"; |
||
393 | if (isset($restriction[1][0]) && is_array($restriction[1][0])) { |
||
394 | foreach ($restriction[1] as &$res) { |
||
395 | $res = simplifyRestriction($res); |
||
396 | } |
||
397 | unset($res); |
||
398 | } |
||
399 | elseif (isset($restriction[1]) && $restriction[1]) { |
||
400 | $restriction[1] = simplifyRestriction($restriction[1]); |
||
401 | } |
||
402 | break; |
||
403 | |||
404 | case RES_OR: |
||
405 | $restriction[0] = "RES_OR"; |
||
406 | if (isset($restriction[1][0]) && is_array($restriction[1][0])) { |
||
407 | foreach ($restriction[1] as &$res) { |
||
408 | $res = simplifyRestriction($res); |
||
409 | } |
||
410 | unset($res); |
||
411 | } |
||
412 | elseif (isset($restriction[1]) && $restriction[1]) { |
||
413 | $restriction[1] = simplifyRestriction($restriction[1]); |
||
414 | } |
||
415 | break; |
||
416 | |||
417 | case RES_NOT: |
||
418 | $restriction[0] = "RES_NOT"; |
||
419 | $restriction[1][0] = simplifyRestriction($restriction[1][0]); |
||
420 | break; |
||
421 | |||
422 | case RES_COMMENT: |
||
423 | $restriction[0] = "RES_COMMENT"; |
||
424 | $res = simplifyRestriction($restriction[1][RESTRICTION]); |
||
425 | $props = $restriction[1][PROPS]; |
||
426 | |||
427 | foreach ($props as &$prop) { |
||
428 | $propTag = $prop[ULPROPTAG]; |
||
429 | $propValue = $prop[VALUE]; |
||
430 | |||
431 | unset($prop); |
||
432 | |||
433 | $prop["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag); |
||
434 | $prop["VALUE"] = is_array($propValue) ? $propValue[$propTag] : $propValue; |
||
435 | } |
||
436 | unset($prop, $restriction[1]); |
||
437 | |||
438 | $restriction[1]["RESTRICTION"] = $res; |
||
439 | $restriction[1]["PROPS"] = $props; |
||
440 | break; |
||
441 | |||
442 | case RES_PROPERTY: |
||
443 | $restriction[0] = "RES_PROPERTY"; |
||
444 | $propTag = $restriction[1][ULPROPTAG]; |
||
445 | $propValue = $restriction[1][VALUE]; |
||
446 | $relOp = $restriction[1][RELOP]; |
||
447 | |||
448 | unset($restriction[1]); |
||
449 | |||
450 | // relop flags |
||
451 | $relOpFlags = ""; |
||
452 | if ($relOp == RELOP_LT) { |
||
453 | $relOpFlags = "RELOP_LT"; |
||
454 | } |
||
455 | elseif ($relOp == RELOP_LE) { |
||
456 | $relOpFlags = "RELOP_LE"; |
||
457 | } |
||
458 | elseif ($relOp == RELOP_GT) { |
||
459 | $relOpFlags = "RELOP_GT"; |
||
460 | } |
||
461 | elseif ($relOp == RELOP_GE) { |
||
462 | $relOpFlags = "RELOP_GE"; |
||
463 | } |
||
464 | elseif ($relOp == RELOP_EQ) { |
||
465 | $relOpFlags = "RELOP_EQ"; |
||
466 | } |
||
467 | elseif ($relOp == RELOP_NE) { |
||
468 | $relOpFlags = "RELOP_NE"; |
||
469 | } |
||
470 | elseif ($relOp == RELOP_RE) { |
||
471 | $relOpFlags = "RELOP_RE"; |
||
472 | } |
||
473 | |||
474 | $restriction[1]["RELOP"] = $relOpFlags; |
||
475 | $restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag); |
||
476 | $restriction[1]["VALUE"] = is_array($propValue) ? $propValue[$propTag] : $propValue; |
||
477 | break; |
||
478 | |||
479 | case RES_CONTENT: |
||
480 | $restriction[0] = "RES_CONTENT"; |
||
481 | $propTag = $restriction[1][ULPROPTAG]; |
||
482 | $propValue = $restriction[1][VALUE]; |
||
483 | $fuzzyLevel = $restriction[1][FUZZYLEVEL]; |
||
484 | |||
485 | unset($restriction[1]); |
||
486 | |||
487 | // fuzzy level flags |
||
488 | $levels = []; |
||
489 | |||
490 | if (($fuzzyLevel & FL_SUBSTRING) == FL_SUBSTRING) { |
||
491 | $levels[] = "FL_SUBSTRING"; |
||
492 | } |
||
493 | elseif (($fuzzyLevel & FL_PREFIX) == FL_PREFIX) { |
||
494 | $levels[] = "FL_PREFIX"; |
||
495 | } |
||
496 | else { |
||
497 | $levels[] = "FL_FULLSTRING"; |
||
498 | } |
||
499 | |||
500 | if (($fuzzyLevel & FL_IGNORECASE) == FL_IGNORECASE) { |
||
501 | $levels[] = "FL_IGNORECASE"; |
||
502 | } |
||
503 | |||
504 | if (($fuzzyLevel & FL_IGNORENONSPACE) == FL_IGNORENONSPACE) { |
||
505 | $levels[] = "FL_IGNORENONSPACE"; |
||
506 | } |
||
507 | |||
508 | if (($fuzzyLevel & FL_LOOSE) == FL_LOOSE) { |
||
509 | $levels[] = "FL_LOOSE"; |
||
510 | } |
||
511 | |||
512 | $fuzzyLevelFlags = implode(" | ", $levels); |
||
513 | |||
514 | $restriction[1]["FUZZYLEVEL"] = $fuzzyLevelFlags; |
||
515 | $restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag); |
||
516 | $restriction[1]["VALUE"] = is_array($propValue) ? $propValue[$propTag] : $propValue; |
||
517 | break; |
||
518 | |||
519 | case RES_COMPAREPROPS: |
||
520 | $propTag1 = $restriction[1][ULPROPTAG1]; |
||
521 | $propTag2 = $restriction[1][ULPROPTAG2]; |
||
522 | |||
523 | unset($restriction[1]); |
||
524 | |||
525 | $restriction[1]["ULPROPTAG1"] = is_string($propTag1) ? $proptag1 : prop2Str($proptag1); |
||
526 | $restriction[1]["ULPROPTAG2"] = is_string($propTag2) ? $propTag2 : prop2Str($propTag2); |
||
527 | break; |
||
528 | |||
529 | case RES_BITMASK: |
||
530 | $restriction[0] = "RES_BITMASK"; |
||
531 | $propTag = $restriction[1][ULPROPTAG]; |
||
532 | $maskType = $restriction[1][ULTYPE]; |
||
533 | $maskValue = $restriction[1][ULMASK]; |
||
534 | |||
535 | unset($restriction[1]); |
||
536 | |||
537 | // relop flags |
||
538 | $maskTypeFlags = ""; |
||
539 | if ($maskType == BMR_EQZ) { |
||
540 | $maskTypeFlags = "BMR_EQZ"; |
||
541 | } |
||
542 | elseif ($maskType == BMR_NEZ) { |
||
543 | $maskTypeFlags = "BMR_NEZ"; |
||
544 | } |
||
545 | |||
546 | $restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag); |
||
547 | $restriction[1]["ULTYPE"] = $maskTypeFlags; |
||
548 | $restriction[1]["ULMASK"] = $maskValue; |
||
549 | break; |
||
550 | |||
551 | case RES_SIZE: |
||
552 | $restriction[0] = "RES_SIZE"; |
||
553 | $propTag = $restriction[1][ULPROPTAG]; |
||
554 | $propValue = $restriction[1][CB]; |
||
555 | $relOp = $restriction[1][RELOP]; |
||
556 | |||
557 | unset($restriction[1]); |
||
558 | |||
559 | // relop flags |
||
560 | $relOpFlags = ""; |
||
561 | if ($relOp == RELOP_LT) { |
||
562 | $relOpFlags = "RELOP_LT"; |
||
563 | } |
||
564 | elseif ($relOp == RELOP_LE) { |
||
565 | $relOpFlags = "RELOP_LE"; |
||
566 | } |
||
567 | elseif ($relOp == RELOP_GT) { |
||
568 | $relOpFlags = "RELOP_GT"; |
||
569 | } |
||
570 | elseif ($relOp == RELOP_GE) { |
||
571 | $relOpFlags = "RELOP_GE"; |
||
572 | } |
||
573 | elseif ($relOp == RELOP_EQ) { |
||
574 | $relOpFlags = "RELOP_EQ"; |
||
575 | } |
||
576 | elseif ($relOp == RELOP_NE) { |
||
577 | $relOpFlags = "RELOP_NE"; |
||
578 | } |
||
579 | elseif ($relOp == RELOP_RE) { |
||
580 | $relOpFlags = "RELOP_RE"; |
||
581 | } |
||
582 | |||
583 | $restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag); |
||
584 | $restriction[1]["RELOP"] = $relOpFlags; |
||
585 | $restriction[1]["CB"] = $propValue; |
||
586 | break; |
||
587 | |||
588 | case RES_EXIST: |
||
589 | $propTag = $restriction[1][ULPROPTAG]; |
||
590 | |||
591 | unset($restriction[1]); |
||
592 | |||
593 | $restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag); |
||
594 | break; |
||
595 | |||
596 | case RES_SUBRESTRICTION: |
||
597 | $propTag = $restriction[1][ULPROPTAG]; |
||
598 | $res = simplifyRestriction($restriction[1][RESTRICTION]); |
||
599 | |||
600 | unset($restriction[1]); |
||
601 | |||
602 | $restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag); |
||
603 | $restriction[1]["RESTRICTION"] = $res; |
||
604 | break; |
||
605 | } |
||
606 | |||
607 | return $restriction; |
||
608 | } |
||
609 |