Skip to content

Commit

Permalink
Replace deprecated utf8_encode with mb_convert_encoding
Browse files Browse the repository at this point in the history
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
  • Loading branch information
youwe-petervanderwal authored and mmenozzi committed Oct 29, 2024
1 parent 1aba779 commit 67e5e0a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"]
Expand Down
29 changes: 24 additions & 5 deletions src/NonUtf8Cleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Webgriffe\Esb;

use Monolog\Utils;

/**
* @internal
*/
Expand All @@ -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
);
}
}

0 comments on commit 67e5e0a

Please sign in to comment.