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

AbstractSimpleHtmlDomNode::__get()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 27

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 7.0222

Importance

Changes 0
Metric Value
dl 27
loc 27
ccs 12
cts 13
cp 0.9231
rs 8.5546
c 0
b 0
f 0
cc 7
nc 6
nop 1
crap 7.0222
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