Completed
Push — develop ( 9ed2dd...34c48b )
by Peter
18:31 queued 06:00
created

src/WebinoDraw/Dom/Document.php (1 issue)

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
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
6
 * @copyright   Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\Dom;
12
13
use DOMXpath;
14
use WebinoDraw\Exception;
15
16
/**
17
 * Extended DOMDocument
18
 */
19
class Document extends \DOMDocument
20
{
21
    /**
22
     * @var DOMXpath
23
     */
24
    protected $xpath;
25
26
    /**
27
     * @param string $version
28
     * @param string $encoding
29
     */
30
    public function __construct($version = null, $encoding = null)
31
    {
32
        parent::__construct($version, $encoding);
33
        $this->registerNodeClass('DOMElement', Element::class);
34
        $this->registerNodeClass('DOMText', Text::class);
35
        $this->registerNodeClass('DOMAttr', Attr::class);
36
    }
37
38
    /**
39
     * @return DOMXpath
40
     */
41
    public function getXpath()
42
    {
43
        if (null == $this->xpath) {
44
            $this->setXpath(new DOMXPath($this));
45
        }
46
        return $this->xpath;
47
    }
48
49
    /**
50
     * @param DOMXpath $xpath
51
     * @return $this
52
     */
53
    public function setXpath(DOMXpath $xpath)
54
    {
55
        $this->xpath = $xpath;
56
        return $this;
57
    }
58
59
    /**
60
     * @return Element
61
     */
62
    public function getDocumentElement()
63
    {
64
        return $this->documentElement;
65
    }
66
67
    /**
68
     * @param string $source
69
     * @param array|null $options
70
     * @return bool
71
     */
72
    public function loadHTML($source, $options = null)
73
    {
74
        // hack HTML5
75
        $errors = libxml_use_internal_errors();
76
        libxml_use_internal_errors(true);
77
        $markup = mb_convert_encoding($source, 'HTML-ENTITIES', 'UTF-8');
78
        $result = parent::loadHTML($markup, LIBXML_COMPACT | $options);
79
        libxml_use_internal_errors($errors);
80
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (DOMDocument) is incompatible with the return type of the parent method DOMDocument::loadHTML of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
81
    }
82
}
83