Conditions | 5 |
Paths | 6 |
Total Lines | 43 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Tests | 15 |
CRAP Score | 5.0061 |
Changes | 7 | ||
Bugs | 2 | Features | 0 |
1 | <?php |
||
5352 | 35 | public static function replace_diamond_question_mark( |
|
5353 | string $str, |
||
5354 | string $replacement_char = '', |
||
5355 | bool $process_invalid_utf8_chars = true |
||
5356 | ): string { |
||
5357 | 35 | if ($str === '') { |
|
5358 | 9 | return ''; |
|
5359 | } |
||
5360 | |||
5361 | 35 | if ($process_invalid_utf8_chars) { |
|
5362 | 35 | if ($replacement_char === '') { |
|
5363 | 35 | $replacement_char_helper = 'none'; |
|
5364 | } else { |
||
5365 | 2 | $replacement_char_helper = \ord($replacement_char); |
|
5366 | } |
||
5367 | |||
5368 | 35 | if (self::$SUPPORT['mbstring'] === false) { |
|
5369 | // if there is no native support for "mbstring", |
||
5370 | // then we need to clean the string before ... |
||
5371 | $str = self::clean($str); |
||
5372 | } |
||
5373 | |||
5374 | /** |
||
5375 | * @psalm-suppress ImpureFunctionCall - we will reset the value in the next step |
||
5376 | */ |
||
5377 | 35 | $save = \mb_substitute_character(); |
|
5378 | /** @noinspection PhpUsageOfSilenceOperatorInspection - ignore "Unknown character" warnings, it's working anyway */ |
||
5379 | 35 | @\mb_substitute_character($replacement_char_helper); |
|
5380 | // the polyfill maybe return false, so cast to string |
||
5381 | 35 | $str = (string) \mb_convert_encoding($str, 'UTF-8', 'UTF-8'); |
|
5382 | 35 | \mb_substitute_character($save); |
|
5383 | } |
||
5384 | |||
5385 | 35 | return \str_replace( |
|
5386 | [ |
||
5387 | 35 | "\xEF\xBF\xBD", |
|
5388 | '�', |
||
5389 | ], |
||
5390 | [ |
||
5391 | 35 | $replacement_char, |
|
5392 | $replacement_char, |
||
5393 | ], |
||
5394 | $str |
||
5395 | ); |
||
13722 |