From 67e5e0a195f9e2e7c422994454d3eedd5f2f0652 Mon Sep 17 00:00:00 2001 From: Peter van der Wal Date: Mon, 27 May 2024 15:42:32 +0200 Subject: [PATCH] Replace deprecated utf8_encode with mb_convert_encoding Error: Function utf8_encode() is deprecated in /srv/esb/vendor/monolog/monolog/src/Monolog/Utils.php on line 179 Resolved with: Implementing Monolog\Utils::detectAndCleanUtf8() from Monolog version 2.8 --- .github/workflows/ci.yaml | 2 +- composer-require-checker.json | 8 +++++++- composer.json | 3 +++ src/NonUtf8Cleaner.php | 29 ++++++++++++++++++++++++----- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 36becbc..b3ab3ef 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -68,7 +68,7 @@ jobs: run: composer install --no-interaction - name: Composer Require Checker - run: composer-require-checker + run: composer-require-checker --config-file=composer-require-checker.json - name: Run Easy Coding Standard run: vendor/bin/ecs check diff --git a/composer-require-checker.json b/composer-require-checker.json index 6d5bb51..7dfd157 100644 --- a/composer-require-checker.json +++ b/composer-require-checker.json @@ -1,3 +1,9 @@ { - "scan-files" : ["bin/esb"] + "scan-files" : ["bin/esb"], + "symbol-whitelist": [ + "null", "true", "false", "static", "self", "parent", + "array", "string", "int", "float", "bool", "iterable", "callable", "void", "object", "mixed", "never", + + "mb_convert_encoding" + ] } diff --git a/composer.json b/composer.json index 8d6307d..473f144 100644 --- a/composer.json +++ b/composer.json @@ -38,6 +38,9 @@ "symfony/deprecation-contracts": "^2.1", "amphp/http-server-form-parser": "^1.1" }, + "suggest": { + "ext-mbstring": "*" + }, "autoload": { "psr-4": { "Webgriffe\\Esb\\": ["src/"] diff --git a/src/NonUtf8Cleaner.php b/src/NonUtf8Cleaner.php index 88a9b1b..b1e296d 100644 --- a/src/NonUtf8Cleaner.php +++ b/src/NonUtf8Cleaner.php @@ -4,8 +4,6 @@ namespace Webgriffe\Esb; -use Monolog\Utils; - /** * @internal */ @@ -17,13 +15,34 @@ class NonUtf8Cleaner */ public static function clean(array $data): array { - array_walk_recursive($data, [Utils::class, 'detectAndCleanUtf8']); + array_walk_recursive($data, [__CLASS__, 'cleanString']); return $data; } public static function cleanString(string $data): string { - Utils::detectAndCleanUtf8($data); - return $data; + // Implementation borrowed from Monolog\Utils::detectAndCleanUtf8() which is no longer a public method as of version 2 + + if (preg_match('//u', $data)) { + return $data; + } + + $data = preg_replace_callback( + '/[\x80-\xFF]+/', + function ($m) { + return function_exists('mb_convert_encoding') ? mb_convert_encoding($m[0], 'UTF-8', 'ISO-8859-1') : utf8_encode($m[0]); + }, + $data + ); + + if (!is_string($data)) { + throw new \RuntimeException('Failed to preg_replace_callback: ' . preg_last_error()); + } + + return str_replace( + ['¤', '¦', '¨', '´', '¸', '¼', '½', '¾'], + ['€', 'Š', 'š', 'Ž', 'ž', 'Œ', 'œ', 'Ÿ'], + $data + ); } }