StringSanitizerTrait   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 1
eloc 11
c 1
b 1
f 0
dl 0
loc 27
ccs 4
cts 4
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A encodeHtmlEntities() 0 6 1
1
<?php
2
/**
3
 * (c) Steve Nebes <[email protected]>.
4
 *
5
 *  For the full copyright and license information, please view the LICENSE
6
 *  file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace SN\HtmlSanitizer\Sanitizer;
12
13
/**
14
 * @internal
15
 */
16
trait StringSanitizerTrait
17
{
18
    /**
19
     * @var array<string, string>
20
     */
21
    private static $replacements = [
22
        // Some DB engines will transform UTF8 full-width characters their classical version
23
        // if the data is saved in a non-UTF8 field
24
        '<' => '&#xFF1C;',
25
        '>' => '&#xFF1E;',
26
        '+' => '&#xFF0B;',
27
        '=' => '&#xFF1D;',
28
        '@' => '&#xFF20;',
29
        '`' => '&#xFF40;',
30
    ];
31
32
    /**
33
     * @param string $string
34
     *
35
     * @return string
36
     */
37 10
    public function encodeHtmlEntities(string $string): string
38
    {
39 10
        $string = \htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
40 10
        $string = \str_replace(\array_keys(self::$replacements), \array_values(self::$replacements), $string);
41
42 10
        return $string;
43
    }
44
}
45