Conditions | 5 |
Paths | 6 |
Total Lines | 43 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Tests | 17 |
CRAP Score | 5.0042 |
Changes | 7 | ||
Bugs | 2 | Features | 0 |
1 | <?php |
||
5336 | 35 | public static function replace_diamond_question_mark( |
|
5337 | string $str, |
||
5338 | string $replacement_char = '', |
||
5339 | bool $process_invalid_utf8_chars = true |
||
5340 | ): string { |
||
5341 | 35 | if ($str === '') { |
|
5342 | 9 | return ''; |
|
5343 | } |
||
5344 | |||
5345 | 35 | if ($process_invalid_utf8_chars) { |
|
5346 | 35 | if ($replacement_char === '') { |
|
5347 | 35 | $replacement_char_helper = 'none'; |
|
5348 | } else { |
||
5349 | 2 | $replacement_char_helper = \ord($replacement_char); |
|
5350 | } |
||
5351 | |||
5352 | 35 | if (self::$SUPPORT['mbstring'] === false) { |
|
5353 | // if there is no native support for "mbstring", |
||
5354 | // then we need to clean the string before ... |
||
5355 | $str = self::clean($str); |
||
5356 | } |
||
5357 | |||
5358 | /** |
||
5359 | * @psalm-suppress ImpureFunctionCall - we will reset the value in the next step |
||
5360 | */ |
||
5361 | 35 | $save = \mb_substitute_character(); |
|
5362 | /** @noinspection PhpUsageOfSilenceOperatorInspection - ignore "Unknown character" warnings, it's working anyway */ |
||
5363 | 35 | @\mb_substitute_character($replacement_char_helper); |
|
5364 | // the polyfill maybe return false, so cast to string |
||
5365 | 35 | $str = (string) \mb_convert_encoding($str, 'UTF-8', 'UTF-8'); |
|
5366 | 35 | \mb_substitute_character($save); |
|
5367 | } |
||
5368 | |||
5369 | 35 | return \str_replace( |
|
5370 | [ |
||
5371 | 35 | "\xEF\xBF\xBD", |
|
5372 | '�', |
||
5373 | ], |
||
5374 | [ |
||
5375 | 35 | $replacement_char, |
|
5376 | 35 | $replacement_char, |
|
5377 | ], |
||
5378 | 35 | $str |
|
5379 | ); |
||
13706 |