Completed
Push — escape-helper ( da2c52 )
by Colin
04:02
created

Xml   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 19
ccs 6
cts 6
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A escape() 0 10 2
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Util;
16
17
/**
18
 * Utility class for handling/generating XML and HTML
19
 */
20
final class Xml
21
{
22
    /**
23
     * @param string $string
24
     * @param bool   $preserveEntities
25
     *
26
     * @return string
27
     */
28 1782
    public static function escape($string, $preserveEntities = false)
29
    {
30 1782
        if ($preserveEntities) {
31 399
            $string = preg_replace('/[&](?![#](x[a-f0-9]{1,8}|[0-9]{1,8});|[a-z][a-z0-9]{1,31};)/i', '&amp;', $string);
32 399
        } else {
33 1779
            $string = str_replace('&', '&amp;', $string);
34
        }
35
36 1782
        return str_replace(['<', '>', '"'], ['&lt;', '&gt;', '&quot;'], $string);
37
    }
38
}
39