Completed
Push — master ( d36330...3c9b9d )
by Lars
01:55
created

AbstractSimpleXmlDomNode::__toString()   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
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\helper;
6
7 View Code Duplication
abstract class AbstractSimpleXmlDomNode extends \ArrayObject
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
    /** @noinspection MagicMethodsValidityInspection */
10
11
    /**
12
     * @param string $name
13
     *
14
     * @return array|null
15
     */
16
    public function __get($name)
17
    {
18
        // init
19
        $name = \strtolower($name);
20
21
        if ($this->count() > 0) {
22
            $return = [];
23
24
            foreach ($this as $node) {
25
                if ($node instanceof SimpleXmlDomInterface) {
26
                    $return[] = $node->{$name};
27
                }
28
            }
29
30
            return $return;
31
        }
32
33
        if ($name === 'plaintext' || $name === 'outertext') {
34
            return [];
35
        }
36
37
        return null;
38
    }
39
40
    /**
41
     * @param string   $selector
42
     * @param int|null $idx
43
     *
44
     * @return SimpleXmlDomNodeInterface|SimpleXmlDomNodeInterface[]|null
45
     */
46
    public function __invoke($selector, $idx = null)
47
    {
48
        return $this->find($selector, $idx);
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function __toString()
55
    {
56
        // init
57
        $html = '';
58
59
        foreach ($this as $node) {
60
            $html .= $node->outertext;
61
        }
62
63
        return $html;
64
    }
65
66
    abstract public function find(string $selector, $idx = null);
67
}
68