Code Duplication    Length = 47-47 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

@@ 3398-3444 (lines=47) @@
3395
   *
3396
   * @return int|false false if is't not UTF16, 1 for UTF-16LE, 2 for UTF-16BE.
3397
   */
3398
  public static function is_utf16($str)
3399
  {
3400
    if (self::is_binary($str)) {
3401
      self::checkForSupport();
3402
3403
      $maybeUTF16LE = 0;
3404
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE');
3405
      if ($test !== false && strlen($test) > 1) {
3406
        $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8');
3407
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE');
3408
        if ($test3 === $test) {
3409
          $strChars = self::count_chars($str);
3410
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3411
            if (in_array($test3char, $strChars, true) === true) {
3412
              $maybeUTF16LE++;
3413
            }
3414
          }
3415
        }
3416
      }
3417
3418
      $maybeUTF16BE = 0;
3419
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE');
3420
      if ($test !== false && strlen($test) > 1) {
3421
        $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8');
3422
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE');
3423
        if ($test3 === $test) {
3424
          $strChars = self::count_chars($str);
3425
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3426
            if (in_array($test3char, $strChars, true) === true) {
3427
              $maybeUTF16BE++;
3428
            }
3429
          }
3430
        }
3431
      }
3432
3433
      if ($maybeUTF16BE !== $maybeUTF16LE) {
3434
        if ($maybeUTF16LE > $maybeUTF16BE) {
3435
          return 1;
3436
        } else {
3437
          return 2;
3438
        }
3439
      }
3440
3441
    }
3442
3443
    return false;
3444
  }
3445
3446
  /**
3447
   * Check if the string is UTF-32.
@@ 3453-3499 (lines=47) @@
3450
   *
3451
   * @return int|false false if is't not UTF16, 1 for UTF-32LE, 2 for UTF-32BE.
3452
   */
3453
  public static function is_utf32($str)
3454
  {
3455
    if (self::is_binary($str)) {
3456
      self::checkForSupport();
3457
3458
      $maybeUTF32LE = 0;
3459
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE');
3460
      if ($test !== false && strlen($test) > 1) {
3461
        $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8');
3462
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE');
3463
        if ($test3 === $test) {
3464
          $strChars = self::count_chars($str);
3465
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3466
            if (in_array($test3char, $strChars, true) === true) {
3467
              $maybeUTF32LE++;
3468
            }
3469
          }
3470
        }
3471
      }
3472
3473
      $maybeUTF32BE = 0;
3474
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE');
3475
      if ($test !== false && strlen($test) > 1) {
3476
        $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8');
3477
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE');
3478
        if ($test3 === $test) {
3479
          $strChars = self::count_chars($str);
3480
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3481
            if (in_array($test3char, $strChars, true) === true) {
3482
              $maybeUTF32BE++;
3483
            }
3484
          }
3485
        }
3486
      }
3487
3488
      if ($maybeUTF32BE !== $maybeUTF32LE) {
3489
        if ($maybeUTF32LE > $maybeUTF32BE) {
3490
          return 1;
3491
        } else {
3492
          return 2;
3493
        }
3494
      }
3495
3496
    }
3497
3498
    return false;
3499
  }
3500
3501
  /**
3502
   * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters.