Completed
Push — master ( 05cc52...a48403 )
by Lars
01:56 queued 11s
created

SimpleHtmlDomNode   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 63.33%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 113
ccs 19
cts 30
cp 0.6333
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B find() 0 28 6
A findOne() 0 4 1
A findMulti() 0 4 1
A innerHtml() 0 11 2
A innertext() 0 4 1
A outertext() 0 4 1
A text() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\helper;
6
7
class SimpleHtmlDomNode extends AbstractSimpleHtmlDomNode implements SimpleHtmlDomNodeInterface
8
{
9
    /**
10
     * Find list of nodes with a CSS selector.
11
     *
12
     * @param string   $selector
13
     * @param int|null $idx
14
     *
15
     * @return SimpleHtmlDomNodeInterface|SimpleHtmlDomNodeInterface[]|null
16
     */
17 11
    public function find(string $selector, $idx = null)
18
    {
19
        // init
20 11
        $elements = new static();
21
22 11
        foreach ($this as $node) {
23 11
            foreach ($node->find($selector) as $res) {
24 10
                $elements->append($res);
25
            }
26
        }
27
28
        // return all elements
29 11
        if ($idx === null) {
30 11
            if (\count($elements) === 0) {
31 1
                return new SimpleHtmlDomNodeBlank();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \voku\helper\SimpleHtmlDomNodeBlank(); (voku\helper\SimpleHtmlDomNodeBlank) is incompatible with the return type declared by the interface voku\helper\SimpleHtmlDomNodeInterface::find of type voku\helper\SimpleHtmlDo...impleHtmlDomNode[]|null.

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...
32
            }
33
34 10
            return $elements;
35
        }
36
37
        // handle negative values
38
        if ($idx < 0) {
39
            $idx = \count($elements) + $idx;
40
        }
41
42
        // return one element
43
        return $elements[$idx] ?? null;
44
    }
45
46
    /**
47
     * Find one node with a CSS selector.
48
     *
49
     * @param string $selector
50
     *
51
     * @return SimpleHtmlDomNodeInterface|null
52
     */
53
    public function findOne(string $selector)
54
    {
55
        return $this->find($selector, 0);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->find($selector, 0); of type voku\helper\SimpleHtmlDo...DomNodeInterface[]|null adds the type voku\helper\SimpleHtmlDomNodeInterface[] to the return on line 55 which is incompatible with the return type declared by the interface voku\helper\SimpleHtmlDomNodeInterface::findOne of type voku\helper\SimpleHtmlDomNode|null.
Loading history...
56
    }
57
58
    /**
59
     * Find nodes with a CSS selector.
60
     *
61
     * @param string $selector
62
     *
63
     * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface
64
     */
65
    public function findMulti(string $selector): SimpleHtmlDomNodeInterface
66
    {
67
        return $this->find($selector, null);
68
    }
69
70
    /**
71
     * Get html of elements.
72
     *
73
     * @return string[]
74
     */
75 1
    public function innerHtml(): array
76
    {
77
        // init
78 1
        $html = [];
79
80 1
        foreach ($this as $node) {
81 1
            $html[] = $node->outertext;
82
        }
83
84 1
        return $html;
85
    }
86
87
    /**
88
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
89
     */
90
    public function innertext()
91
    {
92
        return $this->innerHtml();
93
    }
94
95
    /**
96
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
97
     */
98
    public function outertext()
99
    {
100
        return $this->innerHtml();
101
    }
102
103
    /**
104
     * Get plain text.
105
     *
106
     * @return string[]
107
     */
108 2
    public function text(): array
109
    {
110
        // init
111 2
        $text = [];
112
113 2
        foreach ($this as $node) {
114 2
            $text[] = $node->plaintext;
115
        }
116
117 2
        return $text;
118
    }
119
}
120