Sanitizer   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
dl 0
loc 31
rs 10
c 1
b 0
f 1
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sanitizeStyles() 0 11 1
A sanitizeElementName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\StyleImporter\CSS\Util;
6
7
final class Sanitizer
8
{
9
    /**
10
     * @param string $styles
11
     *
12
     * @return string
13
     *
14
     * @see https://www.regextester.com/94246 Adapted from
15
     */
16
    public static function sanitizeStyles(string $styles): string
17
    {
18
        $replacements = [
19
            '/{/' => ' {',
20
            '/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/' => '',
21
        ];
22
23
        return preg_replace(
24
            array_keys($replacements),
25
            array_values($replacements),
26
            sprintf("\n%s", $styles)
27
        );
28
    }
29
30
    /**
31
     * @param string $elementName
32
     *
33
     * @return string
34
     */
35
    public static function sanitizeElementName(string $elementName): string
36
    {
37
        return str_replace('.', '\.', str_replace('#', '\#', $elementName));
38
    }
39
}
40