PHPHtmlDom::importHTML()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
namespace PHPTools\PHPHtmlDom;
3
4
/**
5
* Esta es la clas eque el usuario debe inicializar para convertir un texto html en un objeto DOM.
6
*/
7
class PHPHtmlDom
8
{
9
    /**
10
     * Objeto que permite convertir un selector css en un path.
11
     * @var \Symfony\Component\CssSelector\CssSelector
12
     */
13
    private $selector;
14
15
    /**
16
     * Objeto que permite escribir logs.
17
     * @var \PHPTools\PHPHtmlDom\Core\PHPHtmlDomLog
18
     */
19
    private $logger;
20
21
    /**
22
     * Objeto nativo de php que permite crear un documento DOM.
23
     * @var \DOMDocument
24
     */
25
    private $dom;
26
27
    /**
28
     * Objeto nativo de php que permite buscar un xpath dentro de un documento DOM.
29
     * @var \DOMXPath
30
     */
31
    private $xpath;
32
33
    /**
34
     * Objeto que permite importar el contennido html.
35
     * @var \PHPTools\PHPHtmlDom\Core\PHPHtmlDomImportHtml
36
     */
37
    private $importer;
38
39
    /**
40
     * Contenido crudo html.
41
     * @var string
42
     */
43
    private $html_content; 
44
45
    public function __construct()
46
    {
47
        $this->selector = new \Symfony\Component\CssSelector\CssSelectorConverter;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Symfony\Component\C...\CssSelectorConverter() of type object<Symfony\Component...r\CssSelectorConverter> is incompatible with the declared type object<Symfony\Component\CssSelector\CssSelector> of property $selector.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
        $this->logger = new \PHPTools\PHPHtmlDom\Core\PHPHtmlDomLog;
49
        $this->dom = new \DOMDocument;
50
        $this->importer = new \PHPTools\PHPHtmlDom\Core\PHPHtmlDomImportHtml;
51
        $this->html_content = NULL;
52
53
        $this->dom->preserveWhiteSpace = false;
54
        $this->dom->validateOnParse = true;
55
    }
56
57
    /**
58
     * Metodo que importa y convierte el contenido html en un objeto DOM.
59
     * @param  string $text_html Cadena de texto con la url, path, texto html.
60
     * @return boolean
61
     */
62
    final public function importHTML($text_html)
63
    {
64
        $content = $this->importer->import($text_html);
65
66
        if(!!is_null($content))
67
        {
68
            $this->logger->logError('E001', array($text_html));
69
        }
70
        else if(!$content)
71
        {
72
            $this->logger->logWarn('W001', array($text_html));
73
        }
74
        else
75
        {
76
            $this->html_content = $content;
0 ignored issues
show
Documentation Bug introduced by
It seems like $content can also be of type boolean. However, the property $html_content is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
77
        }
78
79
        return !!$this->domImport();
80
    }
81
82
    /**
83
     * Metodo que permite buscar elementos hijos a partir de un selector css.
84
     * @param  string $css_selector Cadena de texto con el selector css.
85
     * @return PHPTools\PHPHtmlDom\Core\PHPHtmlDomList
86
     */
87
    final public function e($css_selector)
88
    {
89
        $xpath = $this->toXPath($css_selector);
90
91
        if(!!$xpath)
0 ignored issues
show
Bug Best Practice introduced by
The expression $xpath of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
92
        {
93
            return new \PHPTools\PHPHtmlDom\Core\PHPHtmlDomList($this->xpath->query($xpath));
94
        }
95
    }
96
97
    /**
98
     * Permite importar el contenido dom del texto html.
99
     * @return boolean
100
     */
101
    private function domImport()
102
    {
103
        $dom_import = @$this->dom->loadHTML($this->html_content);
104
105
        if(!!$dom_import)
106
        {
107
            $this->xpath = new \DOMXPath($this->dom);
108
        }
109
        else
110
        {
111
            $this->logger->logError('E002', array($this->html_content));
112
        }
113
114
        return $dom_import;
115
    }
116
117
    /**
118
     * Metodo que convierte un selector css en un xpath
119
     * @param  string $css_selector Cadena de texto con el selector css.
120
     * @return string|NULL          Devuelve una cadena de texto con formato xpath si la converción e sposible o NULL en caso contrario.
121
     */
122
    private function toXPath($css_selector)
123
    {
124
        $xpath = Null;
125
126
        try
127
        {
128
            $xpath = $this->selector->toXPath($css_selector);
129
        }
130
        catch (\Exception $e)
131
        {
132
            $this->logger->logError('E003', array($css_selector));
133
            $this->logger->logError('E000', array($e->getMessage()));
134
        }
135
136
        return $xpath;
137
    }
138
}
139