Completed
Push — master ( 3dc188...ddb6ef )
by Lars
01:45
created

SimpleXmlDomNode::findMulti()   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 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\helper;
6
7
/**
8
 * {@inheritdoc}
9
 */
10 View Code Duplication
class SimpleXmlDomNode extends AbstractSimpleXmlDomNode implements SimpleXmlDomNodeInterface
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...
11
{
12
    /**
13
     * Find list of nodes with a CSS selector.
14
     *
15
     * @param string   $selector
16
     * @param int|null $idx
17
     *
18
     * @return SimpleXmlDomNodeInterface<SimpleXmlDomInterface>|SimpleXmlDomNodeInterface[]|null
0 ignored issues
show
Documentation introduced by
The doc-type SimpleXmlDomNodeInterfac...DomNodeInterface[]|null could not be parsed: Expected "|" or "end of type", but got "<" at position 25. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
19
     */
20 1
    public function find(string $selector, $idx = null)
21
    {
22
        // init
23 1
        $elements = new static();
24
25 1
        foreach ($this as $node) {
26 1
            \assert($node instanceof SimpleXmlDomInterface);
27 1
            foreach ($node->find($selector) as $res) {
28 1
                $elements->append($res);
29
            }
30
        }
31
32
        // return all elements
33 1
        if ($idx === null) {
34 1
            if (\count($elements) === 0) {
35
                return new SimpleXmlDomNodeBlank();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \voku\helper\SimpleXmlDomNodeBlank(); (voku\helper\SimpleXmlDomNodeBlank) is incompatible with the return type declared by the interface voku\helper\SimpleXmlDomNodeInterface::find of type voku\helper\SimpleXmlDom...SimpleXmlDomNode[]|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...
36
            }
37
38 1
            return $elements;
39
        }
40
41
        // handle negative values
42
        if ($idx < 0) {
43
            $idx = \count($elements) + $idx;
44
        }
45
46
        // return one element
47
        return $elements[$idx] ?? null;
48
    }
49
50
    /**
51
     * Find nodes with a CSS selector.
52
     *
53
     * @param string $selector
54
     *
55
     * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>
0 ignored issues
show
Documentation introduced by
The doc-type SimpleXmlDomInterface[]|...<SimpleXmlDomInterface> could not be parsed: Expected "|" or "end of type", but got "<" at position 49. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
56
     */
57
    public function findMulti(string $selector): SimpleXmlDomNodeInterface
58
    {
59
        return $this->find($selector, null);
60
    }
61
62
    /**
63
     * Find nodes with a CSS selector.
64
     *
65
     * @param string $selector
66
     *
67
     * @return false|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>
0 ignored issues
show
Documentation introduced by
The doc-type false|SimpleXmlDomInterf...<SimpleXmlDomInterface> could not be parsed: Expected "|" or "end of type", but got "<" at position 55. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
68
     */
69
    public function findMultiOrFalse(string $selector)
70
    {
71
        $return = $this->find($selector, null);
72
73
        if ($return instanceof SimpleXmlDomNodeBlank) {
74
            return false;
75
        }
76
77
        return $return;
78
    }
79
80
    /**
81
     * Find one node with a CSS selector.
82
     *
83
     * @param string $selector
84
     *
85
     * @return SimpleXmlDomNodeInterface<SimpleXmlDomInterface>|null
0 ignored issues
show
Documentation introduced by
The doc-type SimpleXmlDomNodeInterfac...leXmlDomInterface>|null could not be parsed: Expected "|" or "end of type", but got "<" at position 25. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
86
     */
87
    public function findOne(string $selector)
88
    {
89
        return $this->find($selector, 0);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->find($selector, 0); of type voku\helper\SimpleXmlDom...SimpleXmlDomNode[]|null adds the type voku\helper\SimpleXmlDomNode[] to the return on line 89 which is incompatible with the return type declared by the interface voku\helper\SimpleXmlDomNodeInterface::findOne of type voku\helper\SimpleXmlDomNode|null.
Loading history...
90
    }
91
92
    /**
93
     * Find one node with a CSS selector or false, if no element is found.
94
     *
95
     * @param string $selector
96
     *
97
     * @return false|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>
0 ignored issues
show
Documentation introduced by
The doc-type false|SimpleXmlDomNodeIn...<SimpleXmlDomInterface> could not be parsed: Expected "|" or "end of type", but got "<" at position 31. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
98
     */
99
    public function findOneOrFalse(string $selector)
100
    {
101
        $return = $this->find($selector, 0);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->find($selector, 0); of type voku\helper\SimpleXmlDom...SimpleXmlDomNode[]|null adds the type voku\helper\SimpleXmlDomNode[] to the return on line 108 which is incompatible with the return type declared by the interface voku\helper\SimpleXmlDom...terface::findOneOrFalse of type false|voku\helper\SimpleXmlDomNode.
Loading history...
102
103
        /** @noinspection NullCoalescingOperatorCanBeUsedInspection */
104
        if ($return === null) {
105
            return false;
106
        }
107
108
        return $return;
109
    }
110
111
    /**
112
     * Get html of elements.
113
     *
114
     * @return string[]
115
     */
116
    public function innerHtml(): array
117
    {
118
        // init
119
        $html = [];
120
121
        foreach ($this as $node) {
122
            $html[] = $node->outertext;
123
        }
124
125
        return $html;
126
    }
127
128
    /**
129
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
130
     *
131
     * @return string[]
132
     */
133
    public function innertext()
134
    {
135
        return $this->innerHtml();
136
    }
137
138
    /**
139
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
140
     *
141
     * @return string[]
142
     */
143
    public function outertext()
144
    {
145
        return $this->innerHtml();
146
    }
147
148
    /**
149
     * Get plain text.
150
     *
151
     * @return string[]
152
     */
153
    public function text(): array
154
    {
155
        // init
156
        $text = [];
157
158
        foreach ($this as $node) {
159
            $text[] = $node->plaintext;
160
        }
161
162
        return $text;
163
    }
164
}
165