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

AbstractSimpleHtmlDomNode::find()

Size

Total Lines 1

Duplication

Lines 1
Ratio 100 %

Importance

Changes 0
Metric Value
dl 1
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
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