Completed
Push — master ( a046ab...1a416e )
by Бабичев
02:17
created

AdvancedHtmlDom   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 65.71%

Importance

Changes 0
Metric Value
dl 0
loc 124
ccs 23
cts 35
cp 0.6571
rs 10
c 0
b 0
f 0
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A loadFile() 0 5 1
A __destruct() 0 5 1
A cache() 0 8 2
A text() 0 3 1
A title() 0 3 1
A setCache() 0 3 1
A load() 0 12 2
A load_file() 0 3 1
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 4
    public function __construct($html = null, $is_xml = false)
32
    {
33 4
        $this->doc = $this;
34 4
        if ($html)
35
        {
36 1
            $this->load($html, $is_xml);
37
        }
38 4
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 1
    public function __destruct()
44
    {
45 1
        $this->xpath = $this->root = null;
46 1
        unset($this->xpath, $this->root);
47 1
        parent::__destruct();
48 1
    }
49
50
    /**
51
     * @param InterfaceCache $cache
52
     */
53
    public function setCache(InterfaceCache $cache)
54
    {
55
        $this->cache = $cache;
56
    }
57
58
    /**
59
     * @param string $url
60
     *
61
     * @return mixed
62
     */
63 3
    public function cache($url)
64
    {
65 3
        if (!$this->cache)
66
        {
67 3
            return \file_get_contents($url);
68
        }
69
70
        return $this->cache->get($url);
71
    }
72
73
    /**
74
     * @param      $html
75
     * @param bool $is_xml
76
     */
77 4
    public function load($html, $is_xml = false)
78
    {
79 4
        $this->dom = new \DOMDocument();
80 4
        if ($is_xml)
81
        {
82
            $html = \preg_replace('/xmlns=".*?"/ ', '', $html);
83
        }
84
85 4
        $this->dom->loadHTML($html, LIBXML_NOERROR);
86 4
        $this->xpath = new \DOMXPath($this->dom);
87
        //$this->root = new AHTMLNode($this->dom->documentElement, $this->doc);
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
88 4
        $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

88
        /** @scrutinizer ignore-call */ 
89
        $this->root = $this->at('body');
Loading history...
89 4
    }
90
91
    /**
92
     * @param      $file
93
     * @param bool $is_xml
94
     *
95
     * @deprecated loadFile
96
     */
97
    public function load_file($file, $is_xml = false)
98
    {
99
        $this->loadFile($file, $is_xml);
100
    }
101
102
    /**
103
     * @param string $file
104
     * @param bool   $is_xml
105
     *
106
     * @return $this
107
     */
108 3
    public function loadFile($file, $is_xml = false)
109
    {
110 3
        $this->load($this->cache($file), $is_xml);
111
112 3
        return $this;
113
    }
114
115
    // special cases
116
117
    /**
118
     * @return mixed
119
     */
120
    public function text()
121
    {
122
        return $this->root->text;
123
    }
124
125
    /**
126
     * @return mixed
127
     */
128
    public function title()
129
    {
130
        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

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