Code Duplication    Length = 47-47 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

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