Code Duplication    Length = 49-49 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

@@ 3508-3556 (lines=49) @@
3505
   *
3506
   * @return int|false false if is't not UTF-16, 1 for UTF-16LE, 2 for UTF-16BE.
3507
   */
3508
  public static function is_utf16($str)
3509
  {
3510
    $str = self::remove_bom($str);
3511
3512
    if (self::is_binary($str)) {
3513
      self::checkForSupport();
3514
3515
      $maybeUTF16LE = 0;
3516
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE');
3517
      if ($test) {
3518
        $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8');
3519
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE');
3520
        if ($test3 === $test) {
3521
          $strChars = self::count_chars($str, true);
3522
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
3523
            if (in_array($test3char, $strChars, true) === true) {
3524
              $maybeUTF16LE++;
3525
            }
3526
          }
3527
        }
3528
      }
3529
3530
      $maybeUTF16BE = 0;
3531
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE');
3532
      if ($test) {
3533
        $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8');
3534
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE');
3535
        if ($test3 === $test) {
3536
          $strChars = self::count_chars($str, true);
3537
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
3538
            if (in_array($test3char, $strChars, true) === true) {
3539
              $maybeUTF16BE++;
3540
            }
3541
          }
3542
        }
3543
      }
3544
3545
      if ($maybeUTF16BE !== $maybeUTF16LE) {
3546
        if ($maybeUTF16LE > $maybeUTF16BE) {
3547
          return 1;
3548
        } else {
3549
          return 2;
3550
        }
3551
      }
3552
3553
    }
3554
3555
    return false;
3556
  }
3557
3558
  /**
3559
   * Check if the string is UTF-32.
@@ 3565-3613 (lines=49) @@
3562
   *
3563
   * @return int|false false if is't not UTF-16, 1 for UTF-32LE, 2 for UTF-32BE.
3564
   */
3565
  public static function is_utf32($str)
3566
  {
3567
    $str = self::remove_bom($str);
3568
3569
    if (self::is_binary($str)) {
3570
      self::checkForSupport();
3571
3572
      $maybeUTF32LE = 0;
3573
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE');
3574
      if ($test) {
3575
        $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8');
3576
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE');
3577
        if ($test3 === $test) {
3578
          $strChars = self::count_chars($str, true);
3579
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
3580
            if (in_array($test3char, $strChars, true) === true) {
3581
              $maybeUTF32LE++;
3582
            }
3583
          }
3584
        }
3585
      }
3586
3587
      $maybeUTF32BE = 0;
3588
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE');
3589
      if ($test) {
3590
        $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8');
3591
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE');
3592
        if ($test3 === $test) {
3593
          $strChars = self::count_chars($str, true);
3594
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
3595
            if (in_array($test3char, $strChars, true) === true) {
3596
              $maybeUTF32BE++;
3597
            }
3598
          }
3599
        }
3600
      }
3601
3602
      if ($maybeUTF32BE !== $maybeUTF32LE) {
3603
        if ($maybeUTF32LE > $maybeUTF32BE) {
3604
          return 1;
3605
        } else {
3606
          return 2;
3607
        }
3608
      }
3609
3610
    }
3611
3612
    return false;
3613
  }
3614
3615
  /**
3616
   * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters.