Completed
Push — master ( d7231d...c3f5ad )
by Lars
01:56
created

SimpleHtmlDomNode::innertext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\helper;
6
7 View Code Duplication
class SimpleHtmlDomNode extends AbstractSimpleHtmlDomNode implements SimpleHtmlDomNodeInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 nodes with a CSS selector.
48
     *
49
     * @param string $selector
50
     *
51
     * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface
52
     */
53
    public function findMulti(string $selector): SimpleHtmlDomNodeInterface
54
    {
55
        return $this->find($selector, null);
56
    }
57
58
    /**
59
     * Find nodes with a CSS selector.
60
     *
61
     * @param string $selector
62
     *
63
     * @return false|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface
64
     */
65
    public function findMultiOrFalse(string $selector)
66
    {
67
        $return = $this->find($selector, null);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->find($selector, null); of type voku\helper\SimpleHtmlDo...DomNodeInterface[]|null adds the type voku\helper\SimpleHtmlDomNodeInterface[] to the return on line 73 which is incompatible with the return type declared by the interface voku\helper\SimpleHtmlDo...rface::findMultiOrFalse of type false|voku\helper\Simple...pleHtmlDomNodeInterface.
Loading history...
68
69
        if ($return instanceof SimpleHtmlDomNodeBlank) {
70
            return false;
71
        }
72
73
        return $return;
74
    }
75
76
    /**
77
     * Find one node with a CSS selector.
78
     *
79
     * @param string $selector
80
     *
81
     * @return SimpleHtmlDomNodeInterface|null
82
     */
83
    public function findOne(string $selector)
84
    {
85
        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 85 which is incompatible with the return type declared by the interface voku\helper\SimpleHtmlDomNodeInterface::findOne of type voku\helper\SimpleHtmlDomNode|null.
Loading history...
86
    }
87
88
    /**
89
     * Find one node with a CSS selector.
90
     *
91
     * @param string $selector
92
     *
93
     * @return false|SimpleHtmlDomNodeInterface
94
     */
95
    public function findOneOrFalse(string $selector)
96
    {
97
        $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 103 which is incompatible with the return type declared by the interface voku\helper\SimpleHtmlDo...terface::findOneOrFalse of type false|voku\helper\SimpleHtmlDomNode.
Loading history...
98
99
        if ($return === null) {
100
            return false;
101
        }
102
103
        return $return;
104
    }
105
106
    /**
107
     * Get html of elements.
108
     *
109
     * @return string[]
110
     */
111 1
    public function innerHtml(): array
112
    {
113
        // init
114 1
        $html = [];
115
116 1
        foreach ($this as $node) {
117 1
            $html[] = $node->outertext;
118
        }
119
120 1
        return $html;
121
    }
122
123
    /**
124
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
125
     */
126
    public function innertext()
127
    {
128
        return $this->innerHtml();
129
    }
130
131
    /**
132
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
133
     */
134
    public function outertext()
135
    {
136
        return $this->innerHtml();
137
    }
138
139
    /**
140
     * Get plain text.
141
     *
142
     * @return string[]
143
     */
144 2
    public function text(): array
145
    {
146
        // init
147 2
        $text = [];
148
149 2
        foreach ($this as $node) {
150 2
            $text[] = $node->plaintext;
151
        }
152
153 2
        return $text;
154
    }
155
}
156