Code Duplication    Length = 48-48 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

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