Passed
Push — master ( 088ea9...e4761e )
by Бабичев
02:41
created

AHTMLNode::attributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\AdvancedHtmlDom;
4
5
class AHTMLNode extends AdvancedHtmlBase implements \ArrayAccess
6
{
7
8
    /**
9
     * @var
10
     */
11
    private $_path;
12
13
    /**
14
     * AHTMLNode constructor.
15
     *
16
     * @param $node
17
     * @param $doc
18
     */
19 2
    public function __construct($node, $doc)
20
    {
21 2
        $this->node    = $node;
22 2
        $this->_path   = $node->getNodePath();
23 2
        $this->doc     = $doc;
24 2
        $this->is_text = $node->nodeName === '#text';
25 2
    }
26
27
    /**
28
     * @param $html
29
     *
30
     * @return mixed
31
     */
32
    private function get_fragment($html)
33
    {
34
        $dom = $this->doc->dom;
35
        $fragment = $dom->createDocumentFragment() or die('nope');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
36
        $fragment->appendXML($html);
37
38
        return $fragment;
39
    }
40
41
    /**
42
     * @param $html
43
     *
44
     * @return AHTMLNode|null
45
     */
46
    public function replace($html)
47
    {
48
        $node = empty($html) ? null : $this->before($html);
49
        $this->remove();
50
51
        return $node;
52
    }
53
54
    /**
55
     * @param $html
56
     *
57
     * @return AHTMLNode
58
     */
59
    public function before($html)
60
    {
61
        $fragment = $this->get_fragment($html);
62
        $this->node->parentNode->insertBefore($fragment, $this->node);
63
64
        return new AHTMLNode($this->node->previousSibling, $this->doc);
65
    }
66
67
    /**
68
     * @param $html
69
     */
70
    public function after($html)
71
    {
72
        $fragment = $this->get_fragment($html);
73
        if ($ref_node = $this->node->nextSibling)
74
        {
75
            $this->node->parentNode->insertBefore($fragment, $ref_node);
76
        }
77
        else
78
        {
79
            $this->node->parentNode->appendChild($fragment);
80
        }
81
    }
82
83
    /**
84
     * @param $str
85
     *
86
     * @return mixed
87
     */
88
    public function decamelize($str)
89
    {
90
        $str = \preg_replace_callback(
91
            '/(^|[a-z])([A-Z])/',
92
            function($matches) {
93
                return
94
                    \strtolower(
95
                        \strlen($matches[1])
96
                            ? $matches[1] . '_' . $matches[2] : $matches[2]
97
                    );
98
            },
99
            $str
100
        );
101
102
        return \preg_replace('/ /', '_', \strtolower($str));
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function attributes()
109
    {
110
        $ret = array();
111
        foreach ($this->node->attributes as $attr)
112
        {
113
            $ret[$attr->nodeName] = $attr->nodeValue;
114
        }
115
116
        return $ret;
117
    }
118
119
    /**
120
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
121
     * @param int  $level
122
     *
123
     * @return array
124
     */
125
    public function flatten($key = null, $level = 1)
126
    {
127
128
        $children = $this->children;
0 ignored issues
show
Bug Best Practice introduced by
The property children does not exist on Bavix\AdvancedHtmlDom\AHTMLNode. Since you implemented __get, consider adding a @property annotation.
Loading history...
129
        $ret      = array();
130
        $tag      = $this->tag;
0 ignored issues
show
Bug Best Practice introduced by
The property tag does not exist on Bavix\AdvancedHtmlDom\AHTMLNode. Since you implemented __get, consider adding a @property annotation.
Loading history...
131
132
        if ($this->at('./preceding-sibling::' . $this->tag) || $this->at('./following-sibling::' . $this->tag) || ($key = $this->tag . 's'))
0 ignored issues
show
Bug introduced by
Are you sure $this->tag of type null|Bavix\AdvancedHtmlD...edHtmlDom\AHTMLNodeList can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

132
        if ($this->at('./preceding-sibling::' . /** @scrutinizer ignore-type */ $this->tag) || $this->at('./following-sibling::' . $this->tag) || ($key = $this->tag . 's'))
Loading history...
Bug introduced by
The method at() does not exist on Bavix\AdvancedHtmlDom\AHTMLNode. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
        if ($this->/** @scrutinizer ignore-call */ at('./preceding-sibling::' . $this->tag) || $this->at('./following-sibling::' . $this->tag) || ($key = $this->tag . 's'))
Loading history...
133
        {
134
            $count = $this->search('./preceding-sibling::' . $this->tag)->length + 1;
0 ignored issues
show
Bug introduced by
The method search() does not exist on Bavix\AdvancedHtmlDom\AHTMLNode. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

134
            $count = $this->/** @scrutinizer ignore-call */ search('./preceding-sibling::' . $this->tag)->length + 1;
Loading history...
Bug Best Practice introduced by
The property length does not exist on Bavix\AdvancedHtmlDom\AHTMLNodeList. Since you implemented __get, consider adding a @property annotation.
Loading history...
135
            $tag .= '_' . $count;
136
        }
137
138
        if ($children->length == 0)
139
        {
140
            $ret[$this->decamelize(\implode(' ', \array_filter(array($key, $tag))))] = $this->text;
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist on Bavix\AdvancedHtmlDom\AHTMLNode. Since you implemented __get, consider adding a @property annotation.
Loading history...
141
        }
142
        else
143
        {
144
            $flatten = [];
145
            foreach ($children as $child)
146
            {
147
                $flatten[] = $child->flatten(\implode(' ', \array_filter(array($key, $level <= 0 ? $tag : null))), $level - 1);
148
//                $ret = array_merge($ret, $child->flatten(implode(' ', array_filter(array($key, $level <= 0 ? $tag : null))), $level - 1));
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
149
            }
150
151
            $ret = \array_merge($ret, ...$flatten);
0 ignored issues
show
Bug introduced by
$flatten is expanded, but the parameter $array2 of array_merge() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

151
            $ret = \array_merge($ret, /** @scrutinizer ignore-type */ ...$flatten);
Loading history...
152
        }
153
154
        return $ret;
155
    }
156
157
    /**
158
     * @param $name
159
     * @param $value
160
     */
161
    public function __set($name, $value)
162
    {
163
        switch ($name)
164
        {
165
            case 'text':
166
            case 'innertext':
167
            case 'innerText':
168
            case 'plaintext':
169
                $this->node->nodeValue = $value;
170
171
                return;
172
            case 'outertext':
173
                $this->replace($value);
174
175
                return;
176
            case 'tag':
177
                $el = $this->replace('<' . $value . '>' . $this->innerhtml . '</' . $value . '>');
0 ignored issues
show
Bug Best Practice introduced by
The property innerhtml does not exist on Bavix\AdvancedHtmlDom\AHTMLNode. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
Are you sure $this->innerhtml of type null|Bavix\AdvancedHtmlD...edHtmlDom\AHTMLNodeList can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

177
                $el = $this->replace('<' . $value . '>' . /** @scrutinizer ignore-type */ $this->innerhtml . '</' . $value . '>');
Loading history...
178
                foreach ($this->node->attributes as $_name => $att)
179
                {
180
                    $el->$_name = $att->nodeValue;
181
                }
182
                $this->node = $el->node;
183
184
                return;
185
186
        }
187
188
        $this->offsetSet($name, $value);
189
    }
190
191
    /**
192
     * @param mixed $offset
193
     *
194
     * @return bool
195
     */
196
    public function offsetExists($offset)
197
    {
198
        return true;
199
    }
200
201
    /**
202
     * @param mixed $offset
203
     *
204
     * @return mixed
205
     */
206
    public function offsetGet($offset)
207
    {
208
        return $this->node->getAttribute($offset);
209
    }
210
211
    /**
212
     * @param mixed $key
213
     * @param mixed $value
214
     */
215
    public function offsetSet($key, $value)
216
    {
217
        if ($value)
218
        {
219
            $this->node->setAttribute($key, $value);
220
221
            return;
222
        }
223
224
        $this->node->removeAttribute($key);
225
        //trigger_error('offsetSet not implemented', E_USER_WARNING);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
226
    }
227
228
    /**
229
     * @param mixed $offset
230
     */
231
    public function offsetUnset($offset)
232
    {
233
        \trigger_error('offsetUnset not implemented', E_USER_WARNING);
234
    }
235
236
    /**
237
     * @return mixed
238
     */
239
    public function title()
240
    {
241
        return $this->node->getAttribute('title');
242
    }
243
}
244