Completed
Push — master ( 05ef16...3f1b18 )
by Lars
02:53
created

SimpleHtmlDomNode   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 96
Duplicated Lines 7.29 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 74.29%

Importance

Changes 17
Bugs 4 Features 1
Metric Value
wmc 15
c 17
b 4
f 1
lcom 0
cbo 0
dl 7
loc 96
ccs 26
cts 35
cp 0.7429
rs 10

6 Methods

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