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.

execute()   F
last analyzed

Complexity

Conditions 53
Paths 5904

Size

Total Lines 185
Code Lines 114

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 53
eloc 114
c 1
b 0
f 0
nc 5904
nop 3
dl 0
loc 185
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Removes all unrecognized tags from the list of tokens.
5
 *
6
 * This strategy iterates through all the tokens and removes unrecognized
7
 * tokens. If a token is not recognized but a TagTransform is defined for
8
 * that element, the element will be transformed accordingly.
9
 */
10
11
class HTMLPurifier_Strategy_RemoveForeignElements extends HTMLPurifier_Strategy
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
14
    /**
15
     * @param HTMLPurifier_Token[] $tokens
16
     * @param HTMLPurifier_Config $config
17
     * @param HTMLPurifier_Context $context
18
     * @return array|HTMLPurifier_Token[]
19
     */
20
    public function execute($tokens, $config, $context)
21
    {
22
        $definition = $config->getHTMLDefinition();
23
        $generator = new HTMLPurifier_Generator($config, $context);
24
        $result = array();
25
26
        $escape_invalid_tags = $config->get('Core.EscapeInvalidTags');
27
        $remove_invalid_img = $config->get('Core.RemoveInvalidImg');
28
29
        // currently only used to determine if comments should be kept
30
        $trusted = $config->get('HTML.Trusted');
31
        $comment_lookup = $config->get('HTML.AllowedComments');
32
        $comment_regexp = $config->get('HTML.AllowedCommentsRegexp');
33
        $check_comments = $comment_lookup !== array() || $comment_regexp !== null;
34
35
        $remove_script_contents = $config->get('Core.RemoveScriptContents');
36
        $hidden_elements = $config->get('Core.HiddenElements');
37
38
        // remove script contents compatibility
39
        if ($remove_script_contents === true) {
40
            $hidden_elements['script'] = true;
41
        } elseif ($remove_script_contents === false && isset($hidden_elements['script'])) {
42
            unset($hidden_elements['script']);
43
        }
44
45
        $attr_validator = new HTMLPurifier_AttrValidator();
46
47
        // removes tokens until it reaches a closing tag with its value
48
        $remove_until = false;
49
50
        // converts comments into text tokens when this is equal to a tag name
51
        $textify_comments = false;
52
53
        $token = false;
54
        $context->register('CurrentToken', $token);
55
56
        $e = false;
57
        if ($config->get('Core.CollectErrors')) {
58
            $e =& $context->get('ErrorCollector');
59
        }
60
61
        foreach ($tokens as $token) {
62
            if ($remove_until) {
63
                if (empty($token->is_tag) || $token->name !== $remove_until) {
0 ignored issues
show
Documentation introduced by
The property is_tag does not exist on object<HTMLPurifier_Token>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property name does not exist on object<HTMLPurifier_Token>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
64
                    continue;
65
                }
66
            }
67
            if (!empty($token->is_tag)) {
0 ignored issues
show
Documentation introduced by
The property is_tag does not exist on object<HTMLPurifier_Token>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
                // DEFINITION CALL
69
70
                // before any processing, try to transform the element
71
                if (isset($definition->info_tag_transform[$token->name])) {
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<HTMLPurifier_Token>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
72
                    $original_name = $token->name;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist in HTMLPurifier_Token.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
73
                    // there is a transformation for this tag
74
                    // DEFINITION CALL
75
                    $token = $definition->
76
                        info_tag_transform[$token->name]->transform($token, $config, $context);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<HTMLPurifier_Token>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
77
                    if ($e) {
78
                        $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Tag transform', $original_name);
79
                    }
80
                }
81
82
                if (isset($definition->info[$token->name])) {
83
                    // mostly everything's good, but
84
                    // we need to make sure required attributes are in order
85
                    if (($token instanceof HTMLPurifier_Token_Start || $token instanceof HTMLPurifier_Token_Empty) &&
86
                        $definition->info[$token->name]->required_attr &&
0 ignored issues
show
Bug Best Practice introduced by
The expression $definition->info[$token->name]->required_attr of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
87
                        ($token->name != 'img' || $remove_invalid_img) // ensure config option still works
88
                    ) {
89
                        $attr_validator->validateToken($token, $config, $context);
90
                        $ok = true;
91
                        foreach ($definition->info[$token->name]->required_attr as $name) {
92
                            if (!isset($token->attr[$name])) {
93
                                $ok = false;
94
                                break;
95
                            }
96
                        }
97
                        if (!$ok) {
98
                            if ($e) {
99
                                $e->send(
100
                                    E_ERROR,
101
                                    'Strategy_RemoveForeignElements: Missing required attribute',
102
                                    $name
0 ignored issues
show
Bug introduced by
The variable $name seems to be defined by a foreach iteration on line 91. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
103
                                );
104
                            }
105
                            continue;
106
                        }
107
                        $token->armor['ValidateAttributes'] = true;
108
                    }
109
110
                    if (isset($hidden_elements[$token->name]) && $token instanceof HTMLPurifier_Token_Start) {
111
                        $textify_comments = $token->name;
112
                    } elseif ($token->name === $textify_comments && $token instanceof HTMLPurifier_Token_End) {
113
                        $textify_comments = false;
114
                    }
115
116
                } elseif ($escape_invalid_tags) {
117
                    // invalid tag, generate HTML representation and insert in
118
                    if ($e) {
119
                        $e->send(E_WARNING, 'Strategy_RemoveForeignElements: Foreign element to text');
120
                    }
121
                    $token = new HTMLPurifier_Token_Text(
122
                        $generator->generateFromToken($token)
123
                    );
124
                } else {
125
                    // check if we need to destroy all of the tag's children
126
                    // CAN BE GENERICIZED
127
                    if (isset($hidden_elements[$token->name])) {
128
                        if ($token instanceof HTMLPurifier_Token_Start) {
129
                            $remove_until = $token->name;
130
                        } elseif ($token instanceof HTMLPurifier_Token_Empty) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
131
                            // do nothing: we're still looking
132
                        } else {
133
                            $remove_until = false;
134
                        }
135
                        if ($e) {
136
                            $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign meta element removed');
137
                        }
138
                    } else {
139
                        if ($e) {
140
                            $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign element removed');
141
                        }
142
                    }
143
                    continue;
144
                }
145
            } elseif ($token instanceof HTMLPurifier_Token_Comment) {
146
                // textify comments in script tags when they are allowed
147
                if ($textify_comments !== false) {
148
                    $data = $token->data;
149
                    $token = new HTMLPurifier_Token_Text($data);
150
                } elseif ($trusted || $check_comments) {
151
                    // always cleanup comments
152
                    $trailing_hyphen = false;
153
                    if ($e) {
154
                        // perform check whether or not there's a trailing hyphen
155
                        if (substr($token->data, -1) == '-') {
156
                            $trailing_hyphen = true;
157
                        }
158
                    }
159
                    $token->data = rtrim($token->data, '-');
160
                    $found_double_hyphen = false;
161
                    while (strpos($token->data, '--') !== false) {
162
                        $found_double_hyphen = true;
163
                        $token->data = str_replace('--', '-', $token->data);
164
                    }
165
                    if ($trusted || !empty($comment_lookup[trim($token->data)]) ||
166
                        ($comment_regexp !== null && preg_match($comment_regexp, trim($token->data)))) {
167
                        // OK good
168
                        if ($e) {
169
                            if ($trailing_hyphen) {
170
                                $e->send(
171
                                    E_NOTICE,
172
                                    'Strategy_RemoveForeignElements: Trailing hyphen in comment removed'
173
                                );
174
                            }
175
                            if ($found_double_hyphen) {
176
                                $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Hyphens in comment collapsed');
177
                            }
178
                        }
179
                    } else {
180
                        if ($e) {
181
                            $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed');
182
                        }
183
                        continue;
184
                    }
185
                } else {
186
                    // strip comments
187
                    if ($e) {
188
                        $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed');
189
                    }
190
                    continue;
191
                }
192
            } elseif ($token instanceof HTMLPurifier_Token_Text) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
193
            } else {
194
                continue;
195
            }
196
            $result[] = $token;
197
        }
198
        if ($remove_until && $e) {
199
            // we removed tokens until the end, throw error
200
            $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Token removed to end', $remove_until);
201
        }
202
        $context->destroy('CurrentToken');
203
        return $result;
204
    }
205
}
206
207
// vim: et sw=4 sts=4
208