Code Duplication    Length = 48-48 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

@@ 3524-3571 (lines=48) @@
3521
   *
3522
   * @return int|false false if is't not UTF-16, 1 for UTF-16LE, 2 for UTF-16BE.
3523
   */
3524
  public static function is_utf16($str)
3525
  {
3526
    $str = self::remove_bom($str);
3527
3528
    if (self::is_binary($str)) {
3529
3530
      $maybeUTF16LE = 0;
3531
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE');
3532
      if ($test) {
3533
        $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8');
3534
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE');
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
              $maybeUTF16LE++;
3540
            }
3541
          }
3542
        }
3543
      }
3544
3545
      $maybeUTF16BE = 0;
3546
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE');
3547
      if ($test) {
3548
        $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8');
3549
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE');
3550
        if ($test3 === $test) {
3551
          $strChars = self::count_chars($str, true);
3552
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
3553
            if (in_array($test3char, $strChars, true) === true) {
3554
              $maybeUTF16BE++;
3555
            }
3556
          }
3557
        }
3558
      }
3559
3560
      if ($maybeUTF16BE !== $maybeUTF16LE) {
3561
        if ($maybeUTF16LE > $maybeUTF16BE) {
3562
          return 1;
3563
        } else {
3564
          return 2;
3565
        }
3566
      }
3567
3568
    }
3569
3570
    return false;
3571
  }
3572
3573
  /**
3574
   * Check if the string is UTF-32.
@@ 3580-3627 (lines=48) @@
3577
   *
3578
   * @return int|false false if is't not UTF-16, 1 for UTF-32LE, 2 for UTF-32BE.
3579
   */
3580
  public static function is_utf32($str)
3581
  {
3582
    $str = self::remove_bom($str);
3583
3584
    if (self::is_binary($str)) {
3585
3586
      $maybeUTF32LE = 0;
3587
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE');
3588
      if ($test) {
3589
        $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8');
3590
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE');
3591
        if ($test3 === $test) {
3592
          $strChars = self::count_chars($str, true);
3593
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
3594
            if (in_array($test3char, $strChars, true) === true) {
3595
              $maybeUTF32LE++;
3596
            }
3597
          }
3598
        }
3599
      }
3600
3601
      $maybeUTF32BE = 0;
3602
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE');
3603
      if ($test) {
3604
        $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8');
3605
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE');
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
              $maybeUTF32BE++;
3611
            }
3612
          }
3613
        }
3614
      }
3615
3616
      if ($maybeUTF32BE !== $maybeUTF32LE) {
3617
        if ($maybeUTF32LE > $maybeUTF32BE) {
3618
          return 1;
3619
        } else {
3620
          return 2;
3621
        }
3622
      }
3623
3624
    }
3625
3626
    return false;
3627
  }
3628
3629
  /**
3630
   * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters.