Passed
Push — master ( 60319d...b48e8d )
by Daniele
08:21
created

FluidHelper::domdocumentToHtml()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
rs 8.5806
cc 4
eloc 28
nc 6
nop 2
1
<?php
2
3
namespace FluidXml;
4
5
class FluidHelper
6
{
7
        public static function isAnXmlString($string)
8
        {
9
                // Removes any empty new line at the beginning,
10
                // otherwise the first character check may fail.
11
                $string = \ltrim($string);
12
13
                return $string[0] === '<';
14
        }
15
16
        public static function exportNode(\DOMDocument $dom, \DOMNode $node, $html = false)
17
        {
18
                // $delegate = $html ? 'saveHTML' : 'saveXML';
19
20
                // return $dom->$delegate($node);
21
22
                if ($html) {
23
                        return static::domnodeToHtml($node);
24
                }
25
26
                return $dom->saveXML($node);
27
        }
28
29
        public static function domdocumentToStringWithoutHeaders(\DOMDocument $dom, $html = false)
30
        {
31
                return static::exportNode($dom, $dom->documentElement, $html);
32
        }
33
34
        public static function domnodelistToString(\DOMNodeList $nodelist, $html = false)
35
        {
36
                $nodes = [];
37
38
                // Algorithm 1:
39
                foreach ($nodelist as $n) {
40
                        $nodes[] = $n;
41
                }
42
43
                // Algorithm 2:
44
                // $nodes = \iterator_to_array($nodelist);
45
46
                // Algorithm 1 is faster than Algorithm 2.
47
48
                return static::domnodesToString($nodes, $html);
49
        }
50
51
        public static function domnodesToString(array $nodes, $html = false)
52
        {
53
                $delegate = $html ? 'saveHTML' : 'saveXML';
0 ignored issues
show
Unused Code introduced by
$delegate is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
54
55
                $dom = $nodes[0]->ownerDocument;
56
                $xml = '';
57
58
                foreach ($nodes as $n) {
59
                        $xml .= static::exportNode($dom, $n, $html) . PHP_EOL;
60
                }
61
62
                return \rtrim($xml);
63
        }
64
65
        public static function simplexmlToStringWithoutHeaders(\SimpleXMLElement $element, $html = false)
66
        {
67
                $dom = \dom_import_simplexml($element);
68
69
                return static::exportNode($dom->ownerDocument, $dom, $html);
70
        }
71
72
        public static function domdocumentToHtml($dom, $clone = true)
73
        {
74
                if ($clone) {
75
                        $dom = $dom->cloneNode(true);
76
                }
77
78
                $voids = ['area',
79
                          'base',
80
                          'br',
81
                          'col',
82
                          'colgroup',
83
                          'command',
84
                          'embed',
85
                          'hr',
86
                          'img',
87
                          'input',
88
                          'keygen',
89
                          'link',
90
                          'meta',
91
                          'param',
92
                          'source',
93
                          'track',
94
                          'wbr'];
95
96
                // Every empty node. There is no reason to match nodes with content inside.
97
                $query = '//*[not(node())]';
98
                $nodes = (new \DOMXPath($dom))->query($query);
99
100
                foreach ($nodes as $n) {
101
                        if (! \in_array($n->nodeName, $voids)) {
102
                                // If it is not a void/empty tag,
103
                                // we need to leave the tag open.
104
                                $n->appendChild(new \DOMProcessingInstruction('X-NOT-VOID'));
105
                        }
106
                }
107
108
                $html = static::domdocumentToStringWithoutHeaders($dom);
109
110
                // Let's remove the placeholder.
111
                $html = \preg_replace('/\s*<\?X-NOT-VOID\?>\s*/', '', $html);
112
113
                return $html;
114
        }
115
116
        public static function domnodeToHtml(\DOMNode $node)
117
        {
118
                $dom = new \DOMDocument();
119
                $dom->formatOutput       = true;
120
                $dom->preserveWhiteSpace = false;
121
                $node = $dom->importNode($node, true);
122
                $dom->appendChild($node);
123
124
                return static::domdocumentToHtml($dom, false);
125
        }
126
}
127