Completed
Push — master ( d45b97...b40fc2 )
by Lars
01:55
created

AbstractSimpleHtmlDom   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 187
Duplicated Lines 23.53 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
wmc 36
lcom 0
cbo 0
dl 44
loc 187
ccs 51
cts 63
cp 0.8095
rs 9.52
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 4 1
A __toString() 0 4 1
A __unset() 0 5 1
A __call() 0 10 2
find() 0 1 ?
getAllAttributes() 0 1 ?
getAttribute() 0 1 ?
hasAttribute() 0 1 ?
html() 0 1 ?
innerHtml() 0 1 ?
removeAttribute() 0 1 ?
replaceChildWithString() 0 1 ?
replaceNodeWithString() 0 1 ?
replaceTextWithString() 0 1 ?
setAttribute() 0 1 ?
text() 0 1 ?
C __get() 0 28 13
B __isset() 22 22 10
B __set() 22 22 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\helper;
6
7
abstract class AbstractSimpleHtmlDom
8
{
9
    /**
10
     * @var array
11
     */
12
    protected static $functionAliases = [
13
        'children'     => 'childNodes',
14
        'first_child'  => 'firstChild',
15
        'last_child'   => 'lastChild',
16
        'next_sibling' => 'nextSibling',
17
        'prev_sibling' => 'previousSibling',
18
        'parent'       => 'parentNode',
19
        'outertext'    => 'html',
20
        'outerhtml'    => 'html',
21
        'innertext'    => 'innerHtml',
22
        'innerhtml'    => 'innerHtml',
23
    ];
24
25
    /**
26
     * @var \DOMElement|\DOMNode|null
27
     */
28
    protected $node;
29
30
    /**
31
     * @param string $name
32
     * @param array  $arguments
33
     *
34
     * @throws \BadMethodCallException
35
     *
36
     * @return SimpleHtmlDomInterface|string|null
37
     */
38
    public function __call($name, $arguments)
39
    {
40
        $name = \strtolower($name);
41
42
        if (isset(self::$functionAliases[$name])) {
43
            return \call_user_func_array([$this, self::$functionAliases[$name]], $arguments);
44
        }
45
46
        throw new \BadMethodCallException('Method does not exist');
47
    }
48
49
    /**
50
     * @param string $name
51
     *
52
     * @return array|string|null
53
     */
54 54
    public function __get($name)
55
    {
56 54
        $nameOrig = $name;
57 54
        $name = \strtolower($name);
58
59 54
        switch ($name) {
60 54
            case 'outerhtml':
61 49
            case 'outertext':
62 39
            case 'html':
63 24
                return $this->html();
64 39
            case 'innerhtml':
65 33
            case 'innertext':
66 11
                return $this->innerHtml();
67 30
            case 'text':
68 25
            case 'plaintext':
69 20
                return $this->text();
70 14
            case 'tag':
71 5
                return $this->node ? $this->node->nodeName : '';
72 12
            case 'attr':
73
                return $this->getAllAttributes();
74
            default:
75 12
                if ($this->node && \property_exists($this->node, $nameOrig)) {
76 1
                    return $this->node->{$nameOrig};
77
                }
78
79 12
                return $this->getAttribute($name);
80
        }
81
    }
82
83
    /**
84
     * @param string $selector
85
     * @param int    $idx
86
     *
87
     * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface
88
     */
89 12
    public function __invoke($selector, $idx = null)
90
    {
91 12
        return $this->find($selector, $idx);
92
    }
93
94
    /**
95
     * @param string $name
96
     *
97
     * @return bool
98
     */
99 1 View Code Duplication
    public function __isset($name)
0 ignored issues
show
Duplication introduced by
This method 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...
100
    {
101 1
        $nameOrig = $name;
102 1
        $name = \strtolower($name);
103
104 1
        switch ($name) {
105 1
            case 'outertext':
106 1
            case 'outerhtml':
107 1
            case 'innertext':
108 1
            case 'innerhtml':
109 1
            case 'plaintext':
110 1
            case 'text':
111 1
            case 'tag':
112
                return true;
113
            default:
114 1
                if ($this->node && \property_exists($this->node, $nameOrig)) {
115
                    return isset($this->node->{$nameOrig});
116
                }
117
118 1
                return $this->hasAttribute($name);
119
        }
120
    }
121
122
    /**
123
     * @param string $name
124
     * @param mixed  $value
125
     *
126
     * @return SimpleHtmlDomInterface|null
127
     */
128 17 View Code Duplication
    public function __set($name, $value)
0 ignored issues
show
Duplication introduced by
This method 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...
129
    {
130 17
        $nameOrig = $name;
131 17
        $name = \strtolower($name);
132
133 17
        switch ($name) {
134 17
            case 'outerhtml':
135 15
            case 'outertext':
136 4
                return $this->replaceNodeWithString($value);
137 13
            case 'innertext':
138 11
            case 'innerhtml':
139 7
                return $this->replaceChildWithString($value);
140 10
            case 'plaintext':
141 1
                return $this->replaceTextWithString($value);
142
            default:
143 9
                if ($this->node && \property_exists($this->node, $nameOrig)) {
144
                    return $this->node->{$nameOrig} = $value;
145
                }
146
147 9
                return $this->setAttribute($name, $value);
148
        }
149
    }
150
151
    /**
152
     * @return string
153
     */
154 2
    public function __toString()
155
    {
156 2
        return $this->html();
157
    }
158
159
    /**
160
     * @param string $name
161
     *
162
     * @return void
163
     */
164
    public function __unset($name)
165
    {
166
        /** @noinspection UnusedFunctionResultInspection */
167
        $this->removeAttribute($name);
168
    }
169
170
    abstract public function find(string $selector, $idx = null);
171
172
    abstract public function getAllAttributes();
173
174
    abstract public function getAttribute(string $name): string;
175
176
    abstract public function hasAttribute(string $name): bool;
177
178
    abstract public function html(bool $multiDecodeNewHtmlEntity = false): string;
179
180
    abstract public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string;
181
182
    abstract public function removeAttribute(string $name): SimpleHtmlDomInterface;
183
184
    abstract protected function replaceChildWithString(string $string): SimpleHtmlDomInterface;
185
186
    abstract protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface;
187
188
    abstract protected function replaceTextWithString($string): SimpleHtmlDomInterface;
189
190
    abstract public function setAttribute(string $name, $value = null, bool $strict = false): SimpleHtmlDomInterface;
191
192
    abstract public function text(): string;
193
}
194