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

SimpleXmlDomNode::findOneOrFalse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 0
cts 5
cp 0
rs 9.9
c 0
b 0
f 0
nc 2
cc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\helper;
6
7 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...
8
{
9
    /**
10
     * Find list of nodes with a CSS selector.
11
     *
12
     * @param string   $selector
13
     * @param int|null $idx
14
     *
15
     * @return SimpleXmlDomNodeInterface|SimpleXmlDomNodeInterface[]|null
16
     */
17
    public function find(string $selector, $idx = null)
18
    {
19
        // init
20
        $elements = new static();
21
22
        foreach ($this as $node) {
23
            foreach ($node->find($selector) as $res) {
24
                $elements->append($res);
25
            }
26
        }
27
28
        // return all elements
29
        if ($idx === null) {
30
            if (\count($elements) === 0) {
31
                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...
32
            }
33
34
            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 SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface
52
     */
53
    public function findMulti(string $selector): SimpleXmlDomNodeInterface
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|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface
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\SimpleXmlDom...DomNodeInterface[]|null adds the type voku\helper\SimpleXmlDomNodeInterface[] to the return on line 73 which is incompatible with the return type declared by the interface voku\helper\SimpleXmlDom...rface::findMultiOrFalse of type false|voku\helper\Simple...mpleXmlDomNodeInterface.
Loading history...
68
69
        if ($return instanceof SimpleXmlDomNodeBlank) {
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 SimpleXmlDomNodeInterface|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\SimpleXmlDom...DomNodeInterface[]|null adds the type voku\helper\SimpleXmlDomNodeInterface[] to the return on line 85 which is incompatible with the return type declared by the interface voku\helper\SimpleXmlDomNodeInterface::findOne of type voku\helper\SimpleXmlDomNode|null.
Loading history...
86
    }
87
88
    /**
89
     * Find one node with a CSS selector or false, if no element is found.
90
     *
91
     * @param string $selector
92
     *
93
     * @return false|SimpleXmlDomNodeInterface
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\SimpleXmlDom...DomNodeInterface[]|null adds the type voku\helper\SimpleXmlDomNodeInterface[] to the return on line 104 which is incompatible with the return type declared by the interface voku\helper\SimpleXmlDom...terface::findOneOrFalse of type false|voku\helper\SimpleXmlDomNode.
Loading history...
98
99
        /** @noinspection NullCoalescingOperatorCanBeUsedInspection */
100
        if ($return === null) {
101
            return false;
102
        }
103
104
        return $return;
105
    }
106
107
    /**
108
     * Get html of elements.
109
     *
110
     * @return string[]
111
     */
112
    public function innerHtml(): array
113
    {
114
        // init
115
        $html = [];
116
117
        foreach ($this as $node) {
118
            $html[] = $node->outertext;
119
        }
120
121
        return $html;
122
    }
123
124
    /**
125
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
126
     */
127
    public function innertext()
128
    {
129
        return $this->innerHtml();
130
    }
131
132
    /**
133
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
134
     */
135
    public function outertext()
136
    {
137
        return $this->innerHtml();
138
    }
139
140
    /**
141
     * Get plain text.
142
     *
143
     * @return string[]
144
     */
145
    public function text(): array
146
    {
147
        // init
148
        $text = [];
149
150
        foreach ($this as $node) {
151
            $text[] = $node->plaintext;
152
        }
153
154
        return $text;
155
    }
156
}
157