Conditions | 8 |
Paths | 6 |
Total Lines | 47 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Tests | 23 |
CRAP Score | 8.0327 |
Changes | 0 |
1 | <?php |
||
8467 | 1 | public static function str_obfuscate( |
|
8468 | string $str, |
||
8469 | float $percent = 0.5, |
||
8470 | string $obfuscateChar = '*', |
||
8471 | array $keepChars = [] |
||
8472 | ): string { |
||
8473 | 1 | $obfuscateCharHelper = "\u{2603}"; |
|
8474 | 1 | $str = \str_replace($obfuscateChar, $obfuscateCharHelper, $str); |
|
8475 | |||
8476 | 1 | $chars = self::chars($str); |
|
8477 | 1 | $charsMax = \count($chars); |
|
8478 | 1 | $charsMaxChange = \round($charsMax * $percent); |
|
8479 | 1 | $charsCounter = 0; |
|
8480 | 1 | $charKeyDone = []; |
|
8481 | |||
8482 | 1 | while ($charsCounter < $charsMaxChange) { |
|
8483 | 1 | foreach ($chars as $charKey => $char) { |
|
8484 | 1 | if (isset($charKeyDone[$charKey])) { |
|
8485 | 1 | continue; |
|
8486 | } |
||
8487 | |||
8488 | 1 | if (\random_int(0, 100) > 50) { |
|
8489 | 1 | continue; |
|
8490 | } |
||
8491 | |||
8492 | 1 | if ($char === $obfuscateChar) { |
|
8493 | continue; |
||
8494 | } |
||
8495 | |||
8496 | 1 | ++$charsCounter; |
|
8497 | 1 | $charKeyDone[$charKey] = true; |
|
8498 | |||
8499 | 1 | if ($charsCounter > $charsMaxChange) { |
|
8500 | break; |
||
8501 | } |
||
8502 | |||
8503 | 1 | if (\in_array($char, $keepChars, true)) { |
|
8504 | 1 | continue; |
|
8505 | } |
||
8506 | |||
8507 | 1 | $chars[$charKey] = $obfuscateChar; |
|
8508 | } |
||
8509 | } |
||
8510 | |||
8511 | 1 | $str = \implode('', $chars); |
|
8512 | |||
8513 | 1 | return \str_replace($obfuscateCharHelper, $obfuscateChar, $str); |
|
8514 | } |
||
13722 |