GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b02eb6...eeb8ae )
by Akpé Aurelle Emmanuel Moïse
17s queued 11s
created

HtmlStrip::replaceOrRemove()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 2
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace EZAMA;
4
5
class HtmlStrip extends HtmlStripHelper
6
{
7
    protected $type;
8
9
    public function __construct($html, $type = 'replace', $notAllowedTags = ['', false], $notAllowedAttributes = ['', false])
10
    {
11
        $callback = function ($v) {
12
            return trim(strtolower($v));
13
        };
14
        if (!$this->handleTags($notAllowedTags, $callback, function ($v) {
15
            return isset(self::$tags[$v]);
16
        })) {
17
            $this->allowedTags = self::$tags;
18
        }
19
        if (!$this->handleAttributes($notAllowedAttributes, $callback, function ($v) {
20
            return isset(self::$attributes[$v]);
21
        })) {
22
            $this->allowedAttributes = self::$attributes;
23
        }
24
        $this->type = !is_string($type) || !in_array($tmp = strtolower(trim($type)), ['remove', 'replace']) ? 'remove' : $tmp;
25
26
        $bs = new BeforeStrip($html);
27
        $this->is_html = $bs->isHtml();
28
        $this->is_php = $bs->isPHP();
29
        $bs = new PrepareStrip($bs);
30
        $this->loadHTML($bs->getPrepared());
31
    }
32
33
    public function go($type = self::TAGS)
34
    {
35
        switch ($type) {
36
            case self::TAGS_AND_ATTRIBUTES:
37
                return self::strip($this->html, $this->type, $this->allowedTags, $this->allowedAttributes, $this->is_php);
38
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
39
            case self::TAGS_WITH_ATTRIBUTES:
40
                return self::stripTagsWithAttributes($this->html, $this->allowedTags, $this->allowedAttributes, $this->is_php);
41
            break;
42
            case self::TAGS:
43
                return self::strip($this->html, $this->type, $this->allowedTags, self::$attributes, $this->is_php);
44
            break;
45
            case self::ATTRIBUTES:
46
                return self::strip($this->html, $this->type, self::$tags, $this->allowedAttributes, $this->is_php);
47
            break;
48
            default:
49
                throw new invalidArgumentException('unexpected strip type');
0 ignored issues
show
Bug introduced by
The type EZAMA\invalidArgumentException was not found. Did you mean invalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
50
            break;
51
        }
52
    }
53
54
    protected static function strip($domDoc, $type, $allowed_tags, $allowed_attrs, $is_php)
55
    {
56
        $firstDiv = 0;
57
        if (count(self::$tags) > count($allowed_tags) || count(self::$attributes) > count($allowed_attrs)) {
58
            foreach (new DOMNodeRecursiveIterator($domDoc->getElementsByTagName('*'))as $tag) {
59
                if (!isset($allowed_tags['<' . $tag->tagName . '>'])) {
60
                    if (self::skipUsefull($tag, $firstDiv)) {
61
                        continue;
62
                    }
63
                    self::replaceOrRemove($tag, $type);
64
                } else {
65
                    self::stripAttributes($tag, $allowed_attrs);
66
                }
67
            }
68
69
            self::handleComments($domDoc, $allowed_tags);
70
        }
71
72
        return self::handlePhp($is_php, $domDoc, $allowed_tags);
73
    }
74
75
    protected static function skipUsefull($tag, &$firstDiv)
76
    {
77
        if ($tag->tagName === 'div' && !$firstDiv) {
78
            $firstDiv++;
79
80
            return true;
81
        }
82
        if ($tag->tagName === 'doctypetag') {
83
            return true;
84
        }
85
    }
86
87
    private static function replaceOrRemove($tag, $type)
88
    {
89
        if ($type === 'remove') {
90
            $tag->parentNode->removeChild($tag);
91
        } else {
92
            if (!in_array(strtolower($tag->tagName), ['php', 'style', 'script'])) {
93
                $elem = $tag->parentNode;
94
                $childNodes = $tag->childNodes;
95
                while ($childNodes->length > 0) {
96
                    $elem->insertBefore($childNodes->item(0), $tag);
97
                }
98
                $elem->removeChild($tag);
99
            } else {
100
                $tag->parentNode->removeChild($tag);
101
            }
102
        }
103
    }
104
105
    protected static function stripTagsWithAttributes($domDoc, &$allowed_tags, &$allowed_attrs, $is_php)
106
    {
107
        if (count(self::$attributes) > count($allowed_attrs)) {
108
            foreach (new DOMNodeRecursiveIterator($domDoc->getElementsByTagName('*'))as $tag) {
109
                self::stripAttributes($tag, $allowed_attrs, 2);
110
            }
111
        }
112
113
        return self::handlePhp($is_php, $domDoc, $allowed_tags);
114
    }
115
116
    public static function getEventsAttributes()
117
    {
118
        return array_keys(self::$events_attributes);
119
    }
120
121
    public static function getAttributes()
122
    {
123
        return array_keys(self::$attributes);
124
    }
125
126
    public static function getTags()
127
    {
128
        return array_keys(self::$tags);
129
    }
130
}
131