Completed
Push — master ( 30ef4b...f3717e )
by Joseph
04:45
created

Node::hasClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Jclyons52\PHPQuery;
4
5
class Node
6
{
7
    public $node;
8
9 39
    public function __construct(\DOMNode $node)
10
    {
11 39
        $this->node = $node;
12 39
    }
13
14
    /**
15
     * @return string
16
     */
17 6
    public function toString()
18
    {
19 6
        return $this->node->ownerDocument->saveHTML($this->node);
20
    }
21
22
    /**
23
     * @param string $name
24
     * @param string|null $value
25
     * @return string
26
     * @throws \Exception
27
     */
28 15
    public function attr($name, $value = null)
29
    {
30 15
        if (!($this->node instanceof \DOMElement)) {
31 3
            throw new \Exception('dom node is not of type element');
32
        }
33
34 12
        if ($value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
35 6
            $this->node->setAttribute($name, $value);
36 4
        }
37
        
38 12
        return $this->node->getAttribute($name);
39
    }
40
41
    /**
42
     * @return string
43
     */
44 6
    public function text()
45
    {
46 6
        return $this->node->textContent;
47
    }
48
49
50
    /**
51
     *
52
     */
53 3
    public function remove()
54
    {
55 3
        $this->node->parentNode->removeChild($this->node);
56 3
    }
57
58
    /**
59
     * @param string $class
60
     * @return bool
61
     * @throws \Exception
62
     */
63 3
    public function hasClass($class)
64
    {
65 3
        $classes = explode(' ', $this->attr('class'));
66
67 3
        return in_array($class, $classes);
68
    }
69
}
70