Completed
Push — master ( ed92f4...60669a )
by Daniel
03:02
created

code/HTML5Value.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class SS_HTML5Value extends SS_HTMLValue
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...
4
{
5
    public function setContent($content)
6
    {
7
        require_once(HTML5LIB_PATH.'/HTML5/Parser.php');
8
9
        // Convert any errors to exceptions
10
        set_error_handler(
11
            function ($no, $str) {
12
                throw new Exception("HTML Parse Error: ".$str);
13
            },
14
            error_reporting()
15
        );
16
17
        // Use HTML5lib to parse the HTML fragment
18
        try {
19
            $document = HTML5_Parser::parse(
20
                '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>'.
21
                "<body>$content</body></html>"
22
            );
23
        } catch (Exception $e) {
24
            $document = false;
25
        }
26
27
        // Disable our error handler (restoring to previous value)
28
        restore_error_handler();
29
30
        // If we couldn't parse the HTML, set the error state
31
        if ($document) {
32
            $this->setDocument($document);
0 ignored issues
show
It seems like $document can also be of type object<DOMNodeList>; however, SS_HTMLValue::setDocument() does only seem to accept object<DOMDocument>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
33
        } else {
34
            $this->setInvalid();
35
        }
36
    }
37
}
38