Passed
Push — master ( 44266f...d1b9e8 )
by ReliQ
04:53
created

Sanitizer::sanitizeStyles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 11
rs 10
c 1
b 0
f 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