Code Duplication    Length = 47-47 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

@@ 3326-3372 (lines=47) @@
3323
   *
3324
   * @return int|false false if is't not UTF16, 1 for UTF-16LE, 2 for UTF-16BE.
3325
   */
3326
  public static function is_utf16($str)
3327
  {
3328
    if (self::is_binary($str)) {
3329
      self::checkForSupport();
3330
3331
      $maybeUTF16LE = 0;
3332
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE');
3333
      if ($test !== false && strlen($test) > 1) {
3334
        $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8');
3335
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE');
3336
        if ($test3 === $test) {
3337
          $strChars = self::count_chars($str);
3338
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3339
            if (in_array($test3char, $strChars, true) === true) {
3340
              $maybeUTF16LE++;
3341
            }
3342
          }
3343
        }
3344
      }
3345
3346
      $maybeUTF16BE = 0;
3347
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE');
3348
      if ($test !== false && strlen($test) > 1) {
3349
        $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8');
3350
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE');
3351
        if ($test3 === $test) {
3352
          $strChars = self::count_chars($str);
3353
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3354
            if (in_array($test3char, $strChars, true) === true) {
3355
              $maybeUTF16BE++;
3356
            }
3357
          }
3358
        }
3359
      }
3360
3361
      if ($maybeUTF16BE !== $maybeUTF16LE) {
3362
        if ($maybeUTF16LE > $maybeUTF16BE) {
3363
          return 1;
3364
        } else {
3365
          return 2;
3366
        }
3367
      }
3368
3369
    }
3370
3371
    return false;
3372
  }
3373
3374
  /**
3375
   * Check if the string is UTF-32.
@@ 3381-3427 (lines=47) @@
3378
   *
3379
   * @return int|false false if is't not UTF16, 1 for UTF-32LE, 2 for UTF-32BE.
3380
   */
3381
  public static function is_utf32($str)
3382
  {
3383
    if (self::is_binary($str)) {
3384
      self::checkForSupport();
3385
3386
      $maybeUTF32LE = 0;
3387
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE');
3388
      if ($test !== false && strlen($test) > 1) {
3389
        $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8');
3390
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE');
3391
        if ($test3 === $test) {
3392
          $strChars = self::count_chars($str);
3393
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3394
            if (in_array($test3char, $strChars, true) === true) {
3395
              $maybeUTF32LE++;
3396
            }
3397
          }
3398
        }
3399
      }
3400
3401
      $maybeUTF32BE = 0;
3402
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE');
3403
      if ($test !== false && strlen($test) > 1) {
3404
        $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8');
3405
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE');
3406
        if ($test3 === $test) {
3407
          $strChars = self::count_chars($str);
3408
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3409
            if (in_array($test3char, $strChars, true) === true) {
3410
              $maybeUTF32BE++;
3411
            }
3412
          }
3413
        }
3414
      }
3415
3416
      if ($maybeUTF32BE !== $maybeUTF32LE) {
3417
        if ($maybeUTF32LE > $maybeUTF32BE) {
3418
          return 1;
3419
        } else {
3420
          return 2;
3421
        }
3422
      }
3423
3424
    }
3425
3426
    return false;
3427
  }
3428
3429
  /**
3430
   * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters.