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

SimpleHtmlDomNode::seek()   F

Complexity

Conditions 37
Paths 1695

Size

Total Lines 101
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 132.601

Importance

Changes 9
Bugs 3 Features 1
Metric Value
c 9
b 3
f 1
dl 0
loc 101
ccs 40
cts 68
cp 0.5881
rs 2
cc 37
eloc 52
nc 1695
nop 2
crap 132.601

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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