Browse Source

Utils - Add multibyte and UTF-8 support

John Paul E. Balandan, CPA 3 years ago
parent
commit
d3637aaec8

+ 2 - 2
composer.json

@@ -26,6 +26,7 @@
         "symfony/filesystem": "^4.4.20 || ^5.0",
         "symfony/finder": "^4.4.20 || ^5.0",
         "symfony/options-resolver": "^4.4.20 || ^5.0",
+        "symfony/polyfill-mbstring": "^1.23",
         "symfony/polyfill-php72": "^1.23",
         "symfony/polyfill-php80": "^1.23",
         "symfony/polyfill-php81": "^1.23",
@@ -50,8 +51,7 @@
     },
     "suggest": {
         "ext-dom": "For handling output formats in XML",
-        "ext-mbstring": "For handling non-UTF8 characters.",
-        "symfony/polyfill-mbstring": "When enabling `ext-mbstring` is not possible."
+        "ext-mbstring": "For handling non-UTF8 characters."
     },
     "config": {
         "sort-packages": true

+ 0 - 4
src/Cache/Signature.php

@@ -91,10 +91,6 @@ final class Signature implements SignatureInterface
 
     private static function utf8Encode(array $data): array
     {
-        if (!\function_exists('mb_detect_encoding')) {
-            return $data;
-        }
-
         array_walk_recursive($data, static function (&$item): void {
             if (\is_string($item) && !mb_detect_encoding($item, 'utf-8', true)) {
                 $item = utf8_encode($item);

+ 1 - 1
src/Fixer/Alias/MbStrFunctionsFixer.php

@@ -58,7 +58,7 @@ final class MbStrFunctionsFixer extends AbstractFunctionReferenceFixer
         $this->functions = array_filter(
             self::$functionsMap,
             static function (array $mapping): bool {
-                return \function_exists($mapping['alternativeName']) && (new \ReflectionFunction($mapping['alternativeName']))->isInternal();
+                return (new \ReflectionFunction($mapping['alternativeName']))->isInternal();
             }
         );
     }

+ 1 - 5
src/Fixer/Phpdoc/PhpdocAnnotationWithoutDotFixer.php

@@ -120,11 +120,7 @@ function foo ($bar) {}
                 $content = Preg::replaceCallback(
                     '/^(\s*\*\s*@\w+\s+'.$optionalTypeRegEx.')(\p{Lu}?(?=\p{Ll}|\p{Zs}))(.*)$/',
                     static function (array $matches): string {
-                        if (\function_exists('mb_strtolower')) {
-                            return $matches[1].mb_strtolower($matches[2]).$matches[3];
-                        }
-
-                        return $matches[1].strtolower($matches[2]).$matches[3];
+                        return $matches[1].mb_strtolower($matches[2]).$matches[3];
                     },
                     $startLine->getContent(),
                     1

+ 1 - 1
src/Utils.php

@@ -41,7 +41,7 @@ final class Utils
      */
     public static function camelCaseToUnderscore(string $string): string
     {
-        return strtolower(Preg::replace('/(?<!^)((?=[A-Z][^A-Z])|(?<![A-Z])(?=[A-Z]))/', '_', $string));
+        return mb_strtolower(Preg::replace('/(?<!^)((?=[\p{Lu}][^\p{Lu}])|(?<![\p{Lu}])(?=[\p{Lu}]))/', '_', $string));
     }
 
     /**

+ 4 - 0
tests/UtilsTest.php

@@ -104,6 +104,10 @@ final class UtilsTest extends TestCase
                 'mr_t',
                 'MrT',
             ],
+            [
+                'voyage_éclair',
+                'VoyageÉclair',
+            ],
         ];
     }