Conditions | 5 |
Paths | 6 |
Total Lines | 43 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Tests | 17 |
CRAP Score | 5.0042 |
Changes | 9 | ||
Bugs | 3 | Features | 1 |
1 | <?php |
||
5306 | 35 | public static function replace_diamond_question_mark( |
|
5307 | string $str, |
||
5308 | string $replacement_char = '', |
||
5309 | bool $process_invalid_utf8_chars = true |
||
5310 | ): string { |
||
5311 | 35 | if ($str === '') { |
|
5312 | 9 | return ''; |
|
5313 | } |
||
5314 | |||
5315 | 35 | if ($process_invalid_utf8_chars) { |
|
5316 | 35 | if ($replacement_char === '') { |
|
5317 | 35 | $replacement_char_helper = 'none'; |
|
5318 | } else { |
||
5319 | 2 | $replacement_char_helper = \ord($replacement_char); |
|
5320 | } |
||
5321 | |||
5322 | 35 | if (self::$SUPPORT['mbstring'] === false) { |
|
5323 | // if there is no native support for "mbstring", |
||
5324 | // then we need to clean the string before ... |
||
5325 | $str = self::clean($str); |
||
5326 | } |
||
5327 | |||
5328 | /** |
||
5329 | * @psalm-suppress ImpureFunctionCall - we will reset the value in the next step |
||
5330 | */ |
||
5331 | 35 | $save = \mb_substitute_character(); |
|
5332 | /** @noinspection PhpUsageOfSilenceOperatorInspection - ignore "Unknown character" warnings, it's working anyway */ |
||
5333 | 35 | @\mb_substitute_character($replacement_char_helper); |
|
5334 | // the polyfill maybe return false, so cast to string |
||
5335 | 35 | $str = (string) \mb_convert_encoding($str, 'UTF-8', 'UTF-8'); |
|
5336 | 35 | \mb_substitute_character($save); |
|
5337 | } |
||
5338 | |||
5339 | 35 | return \str_replace( |
|
5340 | [ |
||
5341 | 35 | "\xEF\xBF\xBD", |
|
5342 | '�', |
||
5343 | ], |
||
5344 | [ |
||
5345 | 35 | $replacement_char, |
|
5346 | 35 | $replacement_char, |
|
5347 | ], |
||
5348 | 35 | $str |
|
5349 | ); |
||
13694 |