Completed
Push — master ( 8111da...2b9d4e )
by Lars
03:23
created

SimpleHtmlDomNode   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 65.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 115
ccs 28
cts 43
cp 0.6512
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 14 4
A outertext() 0 4 1
A innertext() 0 4 1
A __invoke() 0 4 1
A __toString() 0 4 1
B find() 0 19 6
A innerHtml() 0 9 2
A text() 0 9 2
1
<?php
2
3
namespace voku\helper;
4
5
/**
6
 * Class SimpleHtmlDomNode
7
 *
8
 * @package voku\helper
9
 *
10
 * @property-read string outertext Get dom node's outer html
11
 * @property-read string plaintext Get dom node's plain text
12
 */
13
class SimpleHtmlDomNode extends \ArrayObject implements SimpleHtmlDomNodeInterface
14
{
15
  /**
16
   * @param $name
17
   *
18
   * @return string
19
   */
20 2
  public function __get($name)
21
  {
22 2
    $name = strtolower($name);
23
24
    switch ($name) {
25 2
      case 'outertext':
26 2
      case 'innertext':
27 1
        return $this->innerHtml();
28 1
      case 'plaintext':
29 1
        return $this->text();
30
    }
31
32
    return null;
33
  }
34
35
  /**
36
   * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
37
   */
38
  public function outertext()
39
  {
40
    $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...
41
  }
42
43
  /**
44
   * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
45
   */
46
  public function innertext()
47
  {
48
    $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...
49
  }
50
51
  /**
52
   * @param string $selector
53
   * @param int    $idx
54
   *
55
   * @return SimpleHtmlDom|SimpleHtmlDomNode|null
56
   */
57
  public function __invoke($selector, $idx = null)
58
  {
59
    return $this->find($selector, $idx);
60
  }
61
62
  /**
63
   * @return mixed
64
   */
65
  public function __toString()
66
  {
67
    return $this->innerHtml();
68
  }
69
70
  /**
71
   * Find list of nodes with a CSS selector
72
   *
73
   * @param string $selector
74
   * @param int    $idx
75
   *
76
   * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|null
77
   */
78 11
  public function find($selector, $idx = null)
79
  {
80 11
    $elements = new self();
81 11
    foreach ($this as $node) {
82 11
      foreach ($node->find($selector) as $res) {
83 10
        $elements->append($res);
84 11
      }
85 11
    }
86
87 11
    if (null === $idx) {
88 11
      return $elements;
89
    } else {
90
      if ($idx < 0) {
91
        $idx = count($elements) + $idx;
92
      }
93
    }
94
95
    return (isset($elements[$idx]) ? $elements[$idx] : null);
96
  }
97
98
  /**
99
   * Get html of Elements
100
   *
101
   * @return string
102
   */
103 1
  public function innerHtml()
104
  {
105 1
    $text = '';
106 1
    foreach ($this as $node) {
107 1
      $text .= $node->outertext;
108 1
    }
109
110 1
    return $text;
111
  }
112
113
  /**
114
   * Get plain text
115
   *
116
   * @return string
117
   */
118 2
  public function text()
119
  {
120 2
    $text = '';
121 2
    foreach ($this as $node) {
122 2
      $text .= $node->plaintext;
123 2
    }
124
125 2
    return $text;
126
  }
127
}
128