Code Duplication    Length = 48-48 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

@@ 2264-2311 (lines=48) @@
2261
   *                   <strong>2</strong> for UTF-16BE.
2262
   *                   </p>
2263
   */
2264
  public static function is_utf16(string $str)
2265
  {
2266
    $str = self::remove_bom($str);
2267
2268
    if (self::is_binary($str) === true) {
2269
2270
      $maybeUTF16LE = 0;
2271
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE');
2272
      if ($test) {
2273
        $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8');
2274
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE');
2275
        if ($test3 === $test) {
2276
          $strChars = self::count_chars($str, true);
2277
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
2278
            if (\in_array($test3char, $strChars, true) === true) {
2279
              $maybeUTF16LE++;
2280
            }
2281
          }
2282
        }
2283
      }
2284
2285
      $maybeUTF16BE = 0;
2286
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE');
2287
      if ($test) {
2288
        $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8');
2289
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE');
2290
        if ($test3 === $test) {
2291
          $strChars = self::count_chars($str, true);
2292
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
2293
            if (\in_array($test3char, $strChars, true) === true) {
2294
              $maybeUTF16BE++;
2295
            }
2296
          }
2297
        }
2298
      }
2299
2300
      if ($maybeUTF16BE !== $maybeUTF16LE) {
2301
        if ($maybeUTF16LE > $maybeUTF16BE) {
2302
          return 1;
2303
        }
2304
2305
        return 2;
2306
      }
2307
2308
    }
2309
2310
    return false;
2311
  }
2312
2313
  /**
2314
   * Check if the string is UTF-32.
@@ 2324-2371 (lines=48) @@
2321
   *                   <strong>2</strong> for UTF-32BE.
2322
   *                   </p>
2323
   */
2324
  public static function is_utf32(string $str)
2325
  {
2326
    $str = self::remove_bom($str);
2327
2328
    if (self::is_binary($str) === true) {
2329
2330
      $maybeUTF32LE = 0;
2331
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE');
2332
      if ($test) {
2333
        $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8');
2334
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE');
2335
        if ($test3 === $test) {
2336
          $strChars = self::count_chars($str, true);
2337
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
2338
            if (\in_array($test3char, $strChars, true) === true) {
2339
              $maybeUTF32LE++;
2340
            }
2341
          }
2342
        }
2343
      }
2344
2345
      $maybeUTF32BE = 0;
2346
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE');
2347
      if ($test) {
2348
        $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8');
2349
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE');
2350
        if ($test3 === $test) {
2351
          $strChars = self::count_chars($str, true);
2352
          foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) {
2353
            if (\in_array($test3char, $strChars, true) === true) {
2354
              $maybeUTF32BE++;
2355
            }
2356
          }
2357
        }
2358
      }
2359
2360
      if ($maybeUTF32BE !== $maybeUTF32LE) {
2361
        if ($maybeUTF32LE > $maybeUTF32BE) {
2362
          return 1;
2363
        }
2364
2365
        return 2;
2366
      }
2367
2368
    }
2369
2370
    return false;
2371
  }
2372
2373
  /**
2374
   * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters.