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

code/HTML5Value.php (3 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
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
4
5
	public function setContent($content) {
6
		require_once(HTML5LIB_PATH.'/HTML5/Parser.php');
7
8
		// Convert any errors to exceptions
9
		set_error_handler(
10
			function($no, $str){
11
				throw new Exception("HTML Parse Error: ".$str);
12
			},
13
			error_reporting()
14
		);
15
16
		// Use HTML5lib to parse the HTML fragment
17
		try {
18
			$document = HTML5_Parser::parse(
19
				'<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>'.
20
				"<body>$content</body></html>"
21
			);
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) $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...
32
		else $this->setInvalid();
33
	}
34
35
}
0 ignored issues
show
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...