Code Duplication    Length = 49-49 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

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