AdvancedHtmlDom   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 65.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 121
ccs 23
cts 35
cp 0.6571
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A loadFile() 0 5 1
A __destruct() 0 5 1
A cache() 0 7 2
A text() 0 3 1
A title() 0 3 1
A setCache() 0 3 1
A __construct() 0 5 2
A load_file() 0 3 1
A load() 0 11 2
1
<?php
2
3
namespace Bavix\AdvancedHtmlDom;
4
5
use Bavix\AdvancedHtmlDom\CacheSystem\InterfaceCache;
6
7
class AdvancedHtmlDom extends AdvancedHtmlBase
8
{
9
10
    /**
11
     * @var
12
     */
13
    public $xpath;
14
15
    /**
16
     * @var
17
     */
18
    public $root;
19
20
    /**
21
     * @var InterfaceCache
22
     */
23
    protected $cache;
24
25
    /**
26
     * AdvancedHtmlDom constructor.
27
     *
28
     * @param null $html
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $html is correct as it would always require null to be passed?
Loading history...
29
     * @param bool $is_xml
30
     */
31 6
    public function __construct($html = null, $is_xml = false)
32
    {
33 6
        $this->doc = $this;
34 6
        if ($html) {
0 ignored issues
show
introduced by
$html is of type null, thus it always evaluated to false.
Loading history...
35 1
            $this->load($html, $is_xml);
36
        }
37 6
    }
38
39
    /**
40
     * @param      $html
41
     * @param bool $is_xml
42
     */
43 6
    public function load($html, $is_xml = false)
44
    {
45 6
        $this->dom = new \DOMDocument();
46 6
        if ($is_xml) {
47
            $html = \preg_replace('/xmlns=".*?"/ ', '', $html);
48
        }
49
50 6
        $this->dom->loadHTML($html, LIBXML_NOERROR);
51 6
        $this->xpath = new \DOMXPath($this->dom);
52
        //$this->root = new AHTMLNode($this->dom->documentElement, $this->doc);
53 6
        $this->root = $this->at('body');
0 ignored issues
show
Bug introduced by
The method at() does not exist on Bavix\AdvancedHtmlDom\AdvancedHtmlDom. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        /** @scrutinizer ignore-call */ 
54
        $this->root = $this->at('body');
Loading history...
54 6
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 1
    public function __destruct()
60
    {
61 1
        $this->xpath = $this->root = null;
62 1
        unset($this->xpath, $this->root);
63 1
        parent::__destruct();
64 1
    }
65
66
    /**
67
     * @param InterfaceCache $cache
68
     */
69
    public function setCache(InterfaceCache $cache)
70
    {
71
        $this->cache = $cache;
72
    }
73
74
    /**
75
     * @param      $file
76
     * @param bool $is_xml
77
     *
78
     * @deprecated loadFile
79
     */
80
    public function load_file($file, $is_xml = false)
81
    {
82
        $this->loadFile($file, $is_xml);
83
    }
84
85
    /**
86
     * @param string $file
87
     * @param bool $is_xml
88
     *
89
     * @return $this
90
     */
91 5
    public function loadFile($file, $is_xml = false)
92
    {
93 5
        $this->load($this->cache($file), $is_xml);
94
95 5
        return $this;
96
    }
97
98
    /**
99
     * @param string $url
100
     *
101
     * @return mixed
102
     */
103 5
    public function cache($url)
104
    {
105 5
        if (!$this->cache) {
106 5
            return \file_get_contents($url);
107
        }
108
109
        return $this->cache->get($url);
110
    }
111
112
    // special cases
113
114
    /**
115
     * @return mixed
116
     */
117
    public function text()
118
    {
119
        return $this->root->text;
120
    }
121
122
    /**
123
     * @return mixed
124
     */
125
    public function title()
126
    {
127
        return $this->at('title')->text();
0 ignored issues
show
Bug introduced by
The method text() does not exist on Bavix\AdvancedHtmlDom\AHTMLNodeList. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
        return $this->at('title')->/** @scrutinizer ignore-call */ text();
Loading history...
128
    }
129
130
}
131