Conditions | 8 |
Paths | 6 |
Total Lines | 47 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Tests | 23 |
CRAP Score | 8.0327 |
Changes | 0 |
1 | <?php |
||
9022 | 1 | public static function str_obfuscate( |
|
9023 | string $str, |
||
9024 | float $percent = 0.5, |
||
9025 | string $obfuscateChar = '*', |
||
9026 | array $keepChars = [] |
||
9027 | ): string { |
||
9028 | 1 | $obfuscateCharHelper = "\u{2603}"; |
|
9029 | 1 | $str = \str_replace($obfuscateChar, $obfuscateCharHelper, $str); |
|
9030 | |||
9031 | 1 | $chars = self::chars($str); |
|
9032 | 1 | $charsMax = \count($chars); |
|
9033 | 1 | $charsMaxChange = \round($charsMax * $percent); |
|
9034 | 1 | $charsCounter = 0; |
|
9035 | 1 | $charKeyDone = []; |
|
9036 | |||
9037 | 1 | while ($charsCounter < $charsMaxChange) { |
|
9038 | 1 | foreach ($chars as $charKey => $char) { |
|
9039 | 1 | if (isset($charKeyDone[$charKey])) { |
|
9040 | 1 | continue; |
|
9041 | } |
||
9042 | |||
9043 | 1 | if (\random_int(0, 100) > 50) { |
|
9044 | 1 | continue; |
|
9045 | } |
||
9046 | |||
9047 | 1 | if ($char === $obfuscateChar) { |
|
9048 | continue; |
||
9049 | } |
||
9050 | |||
9051 | 1 | ++$charsCounter; |
|
9052 | 1 | $charKeyDone[$charKey] = true; |
|
9053 | |||
9054 | 1 | if ($charsCounter > $charsMaxChange) { |
|
9055 | break; |
||
9056 | } |
||
9057 | |||
9058 | 1 | if (\in_array($char, $keepChars, true)) { |
|
9059 | 1 | continue; |
|
9060 | } |
||
9061 | |||
9062 | 1 | $chars[$charKey] = $obfuscateChar; |
|
9063 | } |
||
9064 | } |
||
9065 | |||
9066 | 1 | $str = \implode('', $chars); |
|
9067 | |||
9068 | 1 | return \str_replace($obfuscateCharHelper, $obfuscateChar, $str); |
|
9069 | } |
||
14812 |