Sanitizer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 0
cbo 0
dl 0
loc 50
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sanitize() 0 6 1
A domDocSanitize() 0 21 3
A removeBloat() 0 18 2
1
<?php namespace Taskforcedev\LaravelForum\Helpers;
2
3
use \DOMDocument;
4
5
class Sanitizer
6
{
7
    public function sanitize($html = '')
8
    {
9
        $html = $this->domDocSanitize($html);
10
        $html = $this->removeBloat($html);
11
        return trim($html);
12
    }
13
14
    public function domDocSanitize($html)
15
    {
16
        $dom = new DOMDocument();
17
18
        $dom->loadHTML($html);
19
20
        $script = $dom->getElementsByTagName('script');
21
22
        $remove = [];
23
        foreach ($script as $item) {
24
            $remove[] = $item;
25
        }
26
27
        foreach ($remove as $item) {
28
            $item->parentNode->removeChild($item);
29
        }
30
31
        $html = $dom->saveHTML();
32
33
        return $html;
34
    }
35
36
    public function removeBloat($html)
37
    {
38
        // @codingStandardsIgnoreStart
39
        $removals = [
40
            '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">',
41
            '<html>',
42
            '<body>',
43
            '</body>',
44
            '</html>'
45
        ];
46
        // @codingStandardsIgnoreEnd
47
48
        foreach ($removals as $rem) {
49
            $html = str_replace($rem, '', $html);
50
        }
51
52
        return $html;
53
    }
54
}
55