| 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'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.