Completed
Push — master ( 01d92b...e4ee10 )
by Lars
05:00
created

SimpleHtmlDomNode   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 68.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 132
ccs 30
cts 44
cp 0.6818
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 9 2
A innerHtml() 0 9 2
A text() 0 9 2
A __get() 0 18 4
A outertext() 0 4 1
A innertext() 0 4 1
A __invoke() 0 4 1
B find() 0 26 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\helper;
6
7
/**
8
 * Class SimpleHtmlDomNode
9
 *
10
 * @package voku\helper
11
 *
12
 * @property-read string outertext <p>Get dom node's outer html.</p>
13
 * @property-read string plaintext <p>Get dom node's plain text.</p>
14
 */
15
class SimpleHtmlDomNode extends \ArrayObject implements SimpleHtmlDomNodeInterface
16
{
17
  /** @noinspection MagicMethodsValidityInspection */
18
  /**
19
   * @param string $name
20
   *
21
   * @return array|null
22
   */
23 3
  public function __get($name)
24
  {
25 3
    $name = strtolower($name);
26
27 3
    if ($this->count() > 0) {
28 3
      $return = array();
29
30 3
      foreach ($this as $node) {
31 3
        if ($node instanceof SimpleHtmlDom) {
32 3
          $return[] = $node->{$name};
33
        }
34
      }
35
36 3
      return $return;
37
    }
38
39
    return null;
40
  }
41
42
  /**
43
   * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
44
   */
45
  public function outertext()
46
  {
47
    $this->innerHtml();
0 ignored issues
show
Unused Code introduced by
The call to the method voku\helper\SimpleHtmlDomNode::innerHtml() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
48
  }
49
50
  /**
51
   * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
52
   */
53
  public function innertext()
54
  {
55
    $this->innerHtml();
0 ignored issues
show
Unused Code introduced by
The call to the method voku\helper\SimpleHtmlDomNode::innerHtml() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
56
  }
57
58
  /**
59
   * @param string $selector
60
   * @param int    $idx
61
   *
62
   * @return SimpleHtmlDomNode[]|SimpleHtmlDomNode|null
63
   */
64
  public function __invoke($selector, $idx = null)
65
  {
66
    return $this->find($selector, $idx);
67
  }
68
69
  /**
70
   * @return string
71
   */
72 1
  public function __toString()
73
  {
74 1
    $html = '';
75 1
    foreach ($this as $node) {
76 1
      $html .= $node->outertext;
77
    }
78
79 1
    return $html;
80
  }
81
82
  /**
83
   * Find list of nodes with a CSS selector.
84
   *
85
   * @param string $selector
86
   * @param int    $idx
87
   *
88
   * @return SimpleHtmlDomNode[]|SimpleHtmlDomNode|null
89
   */
90 11
  public function find(string $selector, $idx = null)
91
  {
92 11
    $elements = new self();
93 11
    foreach ($this as $node) {
94 11
      foreach ($node->find($selector) as $res) {
95 11
        $elements->append($res);
96
      }
97
    }
98
99
    // return all elements
100 11
    if (null === $idx) {
101 11
      return $elements;
102
    }
103
104
    // handle negative values
105
    if ($idx < 0) {
106
      $idx = \count($elements) + $idx;
107
    }
108
109
    // return one element
110
    if (isset($elements[$idx])) {
111
      return $elements[$idx];
112
    }
113
114
    return null;
115
  }
116
117
  /**
118
   * Get html of elements.
119
   *
120
   * @return array
121
   */
122 1
  public function innerHtml(): array
123
  {
124 1
    $html = array();
125 1
    foreach ($this as $node) {
126 1
      $html[] = $node->outertext;
127
    }
128
129 1
    return $html;
130
  }
131
132
  /**
133
   * Get plain text.
134
   *
135
   * @return array
136
   */
137 1
  public function text(): array
138
  {
139 1
    $text = array();
140 1
    foreach ($this as $node) {
141 1
      $text[] = $node->plaintext;
142
    }
143
144 1
    return $text;
145
  }
146
}
147