Completed
Push — master ( 236b50...7a7214 )
by Lars
02:07
created

SimpleXmlDomBlank::hasAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 3
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 3
loc 3
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
/**
8
 * @noinspection PhpHierarchyChecksInspection
9
 *
10
 * {@inheritDoc}
11
 */
12 View Code Duplication
class SimpleXmlDomBlank extends AbstractSimpleXmlDom implements \IteratorAggregate, SimpleXmlDomInterface
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...
13
{
14
    /**
15
     * @param string $name
16
     * @param array  $arguments
17
     *
18
     * @throws \BadMethodCallException
19
     *
20
     * @return SimpleXmlDomInterface|string|null
21
     */
22
    public function __call($name, $arguments)
23
    {
24
        $name = \strtolower($name);
25
26
        if (isset(self::$functionAliases[$name])) {
27
            return \call_user_func_array([$this, self::$functionAliases[$name]], $arguments);
28
        }
29
30
        throw new \BadMethodCallException('Method does not exist');
31
    }
32
33
    /**
34
     * Find list of nodes with a CSS selector.
35
     *
36
     * @param string   $selector
37
     * @param int|null $idx
38
     *
39
     * @return SimpleXmlDomNodeInterface
40
     */
41
    public function find(string $selector, $idx = null)
42
    {
43
        return new SimpleXmlDomNodeBlank();
44
    }
45
46
    /**
47
     * Returns an array of attributes.
48
     *
49
     * @return null
50
     */
51
    public function getAllAttributes()
52
    {
53
        return null;
54
    }
55
56
    /**
57
     * @return bool
58
     */
59
    public function hasAttributes(): bool {
60
        return false;
61
    }
62
63
    /**
64
     * Return attribute value.
65
     *
66
     * @param string $name
67
     *
68
     * @return string
69
     */
70
    public function getAttribute(string $name): string
71
    {
72
        return '';
73
    }
74
75
    /**
76
     * Determine if an attribute exists on the element.
77
     *
78
     * @param string $name
79
     *
80
     * @return bool
81
     */
82
    public function hasAttribute(string $name): bool
83
    {
84
        return false;
85
    }
86
87
    /**
88
     * Get dom node's inner xml.
89
     *
90
     * @param bool $multiDecodeNewHtmlEntity
91
     *
92
     * @return string
93
     */
94
    public function innerXml(bool $multiDecodeNewHtmlEntity = false): string
95
    {
96
        return '';
97
    }
98
99
    /**
100
     * Remove attribute.
101
     *
102
     * @param string $name <p>The name of the html-attribute.</p>
103
     *
104
     * @return SimpleXmlDomInterface
105
     */
106
    public function removeAttribute(string $name): SimpleXmlDomInterface
107
    {
108
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (voku\helper\SimpleXmlDomBlank) is incompatible with the return type declared by the interface voku\helper\SimpleXmlDomInterface::removeAttribute of type self.

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...
109
    }
110
111
    protected function replaceChildWithString(string $string): SimpleXmlDomInterface
112
    {
113
        return new static();
114
    }
115
116
    protected function replaceNodeWithString(string $string): SimpleXmlDomInterface
117
    {
118
        return new static();
119
    }
120
121
    protected function replaceTextWithString($string): SimpleXmlDomInterface
122
    {
123
        return new static();
124
    }
125
126
    /**
127
     * Set attribute value.
128
     *
129
     * @param string      $name       <p>The name of the html-attribute.</p>
130
     * @param string|null $value      <p>Set to NULL or empty string, to remove the attribute.</p>
131
     * @param bool        $strict     </p>
132
     *                                $value must be NULL, to remove the attribute,
133
     *                                so that you can set an empty string as attribute-value e.g. autofocus=""
134
     *                                </p>
135
     *
136
     * @return SimpleXmlDomInterface
137
     */
138
    public function setAttribute(string $name, $value = null, bool $strict = false): SimpleXmlDomInterface
139
    {
140
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (voku\helper\SimpleXmlDomBlank) is incompatible with the return type declared by the interface voku\helper\SimpleXmlDomInterface::setAttribute of type self.

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...
141
    }
142
143
    /**
144
     * Get dom node's plain text.
145
     *
146
     * @return string
147
     */
148
    public function text(): string
149
    {
150
        return '';
151
    }
152
153
    /**
154
     * Get dom node's outer html.
155
     *
156
     * @param bool $multiDecodeNewHtmlEntity
157
     *
158
     * @return string
159
     */
160
    public function xml(bool $multiDecodeNewHtmlEntity = false): string
161
    {
162
        return '';
163
    }
164
165
    /**
166
     * Returns children of node.
167
     *
168
     * @param int $idx
169
     *
170
     * @return null
171
     */
172
    public function childNodes(int $idx = -1)
173
    {
174
        return null;
175
    }
176
177
    /**
178
     * Find nodes with a CSS selector.
179
     *
180
     * @param string $selector
181
     *
182
     * @return SimpleXmlDomNodeInterface
183
     */
184
    public function findMulti(string $selector): SimpleXmlDomNodeInterface
185
    {
186
        return new SimpleXmlDomNodeBlank();
187
    }
188
189
    /**
190
     * Find nodes with a CSS selector or false, if no element is found.
191
     *
192
     * @param string $selector
193
     *
194
     * @return false
195
     */
196
    public function findMultiOrFalse(string $selector)
197
    {
198
        return false;
199
    }
200
201
    /**
202
     * Find one node with a CSS selector.
203
     *
204
     * @param string $selector
205
     *
206
     * @return SimpleXmlDomInterface
207
     */
208
    public function findOne(string $selector): SimpleXmlDomInterface
209
    {
210
        return new static();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new static(); (voku\helper\SimpleXmlDomBlank) is incompatible with the return type declared by the interface voku\helper\SimpleXmlDomInterface::findOne of type self.

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...
211
    }
212
213
    /**
214
     * Find one node with a CSS selector or false, if no element is found.
215
     *
216
     * @param string $selector
217
     *
218
     * @return false
219
     */
220
    public function findOneOrFalse(string $selector)
221
    {
222
        return false;
223
    }
224
225
    /**
226
     * Returns the first child of node.
227
     *
228
     * @return null
229
     */
230
    public function firstChild()
231
    {
232
        return null;
233
    }
234
235
    /**
236
     * Return elements by ".class".
237
     *
238
     * @param string $class
239
     *
240
     * @return SimpleXmlDomNodeInterface
241
     */
242
    public function getElementByClass(string $class): SimpleXmlDomNodeInterface
243
    {
244
        return new SimpleXmlDomNodeBlank();
245
    }
246
247
    /**
248
     * Return element by #id.
249
     *
250
     * @param string $id
251
     *
252
     * @return SimpleXmlDomInterface
253
     */
254
    public function getElementById(string $id): SimpleXmlDomInterface
255
    {
256
        return new static();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new static(); (voku\helper\SimpleXmlDomBlank) is incompatible with the return type declared by the interface voku\helper\SimpleXmlDomInterface::getElementById of type self.

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...
257
    }
258
259
    /**
260
     * Return element by tag name.
261
     *
262
     * @param string $name
263
     *
264
     * @return SimpleXmlDomInterface
265
     */
266
    public function getElementByTagName(string $name): SimpleXmlDomInterface
267
    {
268
        return new static();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new static(); (voku\helper\SimpleXmlDomBlank) is incompatible with the return type declared by the interface voku\helper\SimpleXmlDom...ce::getElementByTagName of type self.

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...
269
    }
270
271
    /**
272
     * Returns elements by "#id".
273
     *
274
     * @param string   $id
275
     * @param int|null $idx
276
     *
277
     * @return SimpleXmlDomNodeInterface
278
     */
279
    public function getElementsById(string $id, $idx = null)
280
    {
281
        return new SimpleXmlDomNodeBlank();
282
    }
283
284
    /**
285
     * Returns elements by tag name.
286
     *
287
     * @param string   $name
288
     * @param int|null $idx
289
     *
290
     * @return SimpleXmlDomNodeInterface
291
     */
292
    public function getElementsByTagName(string $name, $idx = null)
293
    {
294
        return new SimpleXmlDomNodeBlank();
295
    }
296
297
    /**
298
     * @return \DOMNode
299
     */
300
    public function getNode(): \DOMNode
301
    {
302
        return new \DOMNode();
303
    }
304
305
    /**
306
     * Create a new "XmlDomParser"-object from the current context.
307
     *
308
     * @return XmlDomParser
309
     */
310
    public function getXmlDomParser(): XmlDomParser
311
    {
312
        return new XmlDomParser($this);
313
    }
314
315
    /**
316
     * Get dom node's inner html.
317
     *
318
     * @param bool $multiDecodeNewHtmlEntity
319
     *
320
     * @return string
321
     */
322
    public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string
323
    {
324
        return '';
325
    }
326
327
    /**
328
     * Nodes can get partially destroyed in which they're still an
329
     * actual DOM node (such as \DOMElement) but almost their entire
330
     * body is gone, including the `nodeType` attribute.
331
     *
332
     * @return bool true if node has been destroyed
333
     */
334
    public function isRemoved(): bool
335
    {
336
        return true;
337
    }
338
339
    /**
340
     * Returns the last child of node.
341
     *
342
     * @return null
343
     */
344
    public function lastChild()
345
    {
346
        return null;
347
    }
348
349
    /**
350
     * Returns the next sibling of node.
351
     *
352
     * @return null
353
     */
354
    public function nextSibling()
355
    {
356
        return null;
357
    }
358
359
    /**
360
     * Returns the parent of node.
361
     *
362
     * @return SimpleXmlDomInterface
363
     */
364
    public function parentNode(): SimpleXmlDomInterface
365
    {
366
        return new static();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new static(); (voku\helper\SimpleXmlDomBlank) is incompatible with the return type declared by the interface voku\helper\SimpleXmlDomInterface::parentNode of type self.

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...
367
    }
368
369
    /**
370
     * Returns the previous sibling of node.
371
     *
372
     * @return null
373
     */
374
    public function previousSibling()
375
    {
376
        return null;
377
    }
378
379
    /**
380
     * @param string|string[]|null $value <p>
381
     *                                    null === get the current input value
382
     *                                    text === set a new input value
383
     *                                    </p>
384
     *
385
     * @return string|string[]|null
386
     */
387
    public function val($value = null)
388
    {
389
        return null;
390
    }
391
392
    /**
393
     * Retrieve an external iterator.
394
     *
395
     * @see  http://php.net/manual/en/iteratoraggregate.getiterator.php
396
     *
397
     * @return SimpleXmlDomNodeInterface
398
     *                           <p>
399
     *                              An instance of an object implementing <b>Iterator</b> or
400
     *                              <b>Traversable</b>
401
     *                           </p>
402
     */
403
    public function getIterator(): SimpleXmlDomNodeInterface
404
    {
405
        return new SimpleXmlDomNodeBlank();
406
    }
407
}
408