Obfuscater   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 59
rs 10
c 0
b 0
f 0
ccs 18
cts 18
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 13 3
A makeSafer() 0 19 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelHtml\Helpers;
6
7
/**
8
 * Class     Obfuscater
9
 *
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class Obfuscater
13
{
14
    /* -----------------------------------------------------------------
15
     |  Main Methods
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * Obfuscate a string to prevent spam-bots from sniffing it.
21
     *
22
     * @param  string  $value
23
     *
24
     * @return string
25
     */
26 12
    public static function make(string $value): string
27
    {
28 12
        $safe = '';
29
30 12
        foreach (str_split($value) as $letter) {
31 12
            if (ord($letter) > 128)
32 6
                return $letter;
33
34 6
            self::makeSafer($safe, $letter);
35
        }
36
37 6
        return $safe;
38
    }
39
40
    /* -----------------------------------------------------------------
41
     |  Other Methods
42
     | -----------------------------------------------------------------
43
     */
44
45
    /**
46
     * Make safer.
47
     *
48
     * @param  string  $letter
49
     * @param  string  $safe
50
     */
51 6
    private static function makeSafer(string &$safe, string $letter)
52
    {
53
        // To properly obfuscate the value, we will randomly convert each letter to
54
        // its entity or hexadecimal representation, keeping a bot from sniffing
55
        // the randomly obfuscated letters out of the string on the responses.
56 6
        switch (rand(1, 3)) {
57 6
            case 1:
58 6
                $safe .= '&#' . ord($letter).';';
59 6
                break;
60
61 6
            case 2:
62 6
                $safe .= '&#x' . dechex(ord($letter)).';';
63 6
                break;
64
65 6
            case 3:
66 6
                $safe .= $letter;
67
                // no break
68
        }
69 6
    }
70
}
71