Completed
Push — master ( e4761e...e4321c )
by Бабичев
03:02
created

AdvancedHtmlDom   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
dl 0
loc 116
ccs 18
cts 30
cp 0.6
rs 10
c 0
b 0
f 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A loadFile() 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 14 2
A load_file() 0 3 1
A __construct() 0 6 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 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
     * @param InterfaceCache $cache
42
     */
43
    public function setCache(InterfaceCache $cache)
44
    {
45
        $this->cache = $cache;
46
    }
47
48
    /**
49
     * @param string $url
50
     *
51
     * @return mixed
52
     */
53 3
    public function cache($url)
54
    {
55 3
        if (!$this->cache)
56
        {
57 3
            return \file_get_contents($url);
58
        }
59
60
        return $this->cache->get($url);
61
    }
62
63
    /**
64
     * @param      $html
65
     * @param bool $is_xml
66
     */
67 4
    public function load($html, $is_xml = false)
68
    {
69 4
        $this->dom = new \DOMDocument();
0 ignored issues
show
Bug introduced by
The call to DOMDocument::__construct() has too few arguments starting with version. ( Ignorable by Annotation )

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

69
        $this->dom = /** @scrutinizer ignore-call */ new \DOMDocument();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
70 4
        if ($is_xml)
71
        {
72
            $this->dom->loadXML(\preg_replace('/xmlns=".*?"/ ', '', $html));
73
        }
74
        else
75
        {
76 4
            $this->dom->loadHTML($html);
77
        }
78 4
        $this->xpath = new \DOMXPath($this->dom);
79
        //$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...
80 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

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

122
        return $this->at('title')->/** @scrutinizer ignore-call */ text();
Loading history...
123
    }
124
125
}
126