Completed
Push — master ( 9cc464...a12f42 )
by Lars
12:54 queued 08:14
created

SimpleHtmlDomNode   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 113
Duplicated Lines 16.81 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 64.29%

Importance

Changes 18
Bugs 4 Features 1
Metric Value
wmc 18
c 18
b 4
f 1
lcom 0
cbo 0
dl 19
loc 113
ccs 27
cts 42
cp 0.6429
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 12 12 4
A outertext() 0 4 1
A innertext() 0 4 1
A __invoke() 0 4 1
A __toString() 0 4 1
B find() 7 19 6
A innerHtml() 0 9 2
A text() 0 9 2

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