Parser   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 60
ccs 0
cts 30
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 13 2
A createDOMDocument() 0 20 3
A parseNode() 0 8 2
1
<?php
2
3
namespace tomzx\HtmlParser;
4
5
class Parser
6
{
7
    protected $suppressErrors = false;
8
9
    /**
10
     * @param string $code
11
     * @return \tomzx\HtmlParser\Node[]
12
     */
13
    public function parse($code)
14
    {
15
        $document = $this->createDOMDocument($code);
16
17
        if (!($root = $document->getElementsByTagName('html')->item(0))) {
18
            throw new \InvalidArgumentException('Invalid HTML was provided');
19
        }
20
21
        $rootNode = new Node($root);
22
        //$statements = $this->parseNode($rootNode);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
23
24
        return [$rootNode]; //$statements;
25
    }
26
27
    /**
28
     * @param string $code
29
     * @return \DOMDocument
30
     */
31
    protected function createDOMDocument($code)
32
    {
33
        $document = new \DOMDocument();
34
35
        // TODO: Make suppress error configurable
36
        if ($this->suppressErrors) {
37
            // Suppress conversion errors (from http://bit.ly/pCCRSX)
38
            libxml_use_internal_errors(true);
39
        }
40
41
        // Hack to load utf-8 HTML (from http://bit.ly/pVDyCt)
42
        $document->loadHTML('<?xml encoding="UTF-8">' . $code);
43
        $document->encoding = 'UTF-8';
44
45
        if ($this->suppressErrors) {
46
            libxml_clear_errors();
47
        }
48
49
        return $document;
50
    }
51
52
    /**
53
     * @param \tomzx\HtmlParser\Node $node
54
     * @return array
55
     */
56
    protected function parseNode(Node $node)
57
    {
58
        $statements = $node->getChildren();
59
        foreach ($node->getChildren() as $child) {
60
            $statements[] = $this->parseNode($child);
0 ignored issues
show
Compatibility introduced by
$child of type object<tomzx\AbstractParser\NodeInterface> is not a sub-type of object<tomzx\HtmlParser\Node>. It seems like you assume a concrete implementation of the interface tomzx\AbstractParser\NodeInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
61
        }
62
        return $statements;
63
    }
64
}
65