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

AbstractSimpleHtmlDomNode::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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