Code Duplication    Length = 49-49 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

@@ 3537-3585 (lines=49) @@
3534
   *
3535
   * @return int|false false if is't not UTF-16, 1 for UTF-16LE, 2 for UTF-16BE.
3536
   */
3537
  public static function is_utf16($str)
3538
  {
3539
    $str = self::remove_bom($str);
3540
3541
    if (self::is_binary($str)) {
3542
      self::checkForSupport();
3543
3544
      $maybeUTF16LE = 0;
3545
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE');
3546
      if ($test) {
3547
        $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8');
3548
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE');
3549
        if ($test3 === $test) {
3550
          $strChars = self::count_chars($str, true);
3551
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
3552
            if (in_array($test3char, $strChars, true) === true) {
3553
              $maybeUTF16LE++;
3554
            }
3555
          }
3556
        }
3557
      }
3558
3559
      $maybeUTF16BE = 0;
3560
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE');
3561
      if ($test) {
3562
        $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8');
3563
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE');
3564
        if ($test3 === $test) {
3565
          $strChars = self::count_chars($str, true);
3566
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
3567
            if (in_array($test3char, $strChars, true) === true) {
3568
              $maybeUTF16BE++;
3569
            }
3570
          }
3571
        }
3572
      }
3573
3574
      if ($maybeUTF16BE !== $maybeUTF16LE) {
3575
        if ($maybeUTF16LE > $maybeUTF16BE) {
3576
          return 1;
3577
        } else {
3578
          return 2;
3579
        }
3580
      }
3581
3582
    }
3583
3584
    return false;
3585
  }
3586
3587
  /**
3588
   * Check if the string is UTF-32.
@@ 3594-3642 (lines=49) @@
3591
   *
3592
   * @return int|false false if is't not UTF-16, 1 for UTF-32LE, 2 for UTF-32BE.
3593
   */
3594
  public static function is_utf32($str)
3595
  {
3596
    $str = self::remove_bom($str);
3597
3598
    if (self::is_binary($str)) {
3599
      self::checkForSupport();
3600
3601
      $maybeUTF32LE = 0;
3602
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE');
3603
      if ($test) {
3604
        $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8');
3605
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE');
3606
        if ($test3 === $test) {
3607
          $strChars = self::count_chars($str, true);
3608
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
3609
            if (in_array($test3char, $strChars, true) === true) {
3610
              $maybeUTF32LE++;
3611
            }
3612
          }
3613
        }
3614
      }
3615
3616
      $maybeUTF32BE = 0;
3617
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE');
3618
      if ($test) {
3619
        $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8');
3620
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE');
3621
        if ($test3 === $test) {
3622
          $strChars = self::count_chars($str, true);
3623
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
3624
            if (in_array($test3char, $strChars, true) === true) {
3625
              $maybeUTF32BE++;
3626
            }
3627
          }
3628
        }
3629
      }
3630
3631
      if ($maybeUTF32BE !== $maybeUTF32LE) {
3632
        if ($maybeUTF32LE > $maybeUTF32BE) {
3633
          return 1;
3634
        } else {
3635
          return 2;
3636
        }
3637
      }
3638
3639
    }
3640
3641
    return false;
3642
  }
3643
3644
  /**
3645
   * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters.