| Conditions | 10 |
| Paths | 45 |
| Total Lines | 41 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 125 | public static function normalizeDocument(DOMDocument $doc): DOMDocument |
||
| 126 | { |
||
| 127 | // Get the root element |
||
| 128 | $root = $doc->documentElement; |
||
| 129 | |||
| 130 | // Collect all xmlns attributes from the document |
||
| 131 | $xpath = new DOMXPath($doc); |
||
| 132 | $xmlnsAttributes = []; |
||
| 133 | |||
| 134 | // Register all namespaces to ensure XPath can handle them |
||
| 135 | foreach ($xpath->query('//namespace::*') as $node) { |
||
| 136 | $name = $node->nodeName === 'xmlns' ? 'xmlns' : $node->nodeName; |
||
| 137 | $xmlnsAttributes[$name] = $node->nodeValue; |
||
| 138 | } |
||
| 139 | |||
| 140 | // If no xmlns attributes found, return early with debug info |
||
| 141 | if (empty($xmlnsAttributes)) { |
||
| 142 | return $doc->saveXML(); |
||
|
|
|||
| 143 | } |
||
| 144 | |||
| 145 | // Remove xmlns attributes from all elements |
||
| 146 | $nodes = $xpath->query('//*[namespace::*]'); |
||
| 147 | foreach ($nodes as $node) { |
||
| 148 | $attributesToRemove = []; |
||
| 149 | foreach ($node->attributes as $attr) { |
||
| 150 | if (strpos($attr->nodeName, 'xmlns') === 0 || $attr->nodeName === 'xmlns') { |
||
| 151 | $attributesToRemove[] = $attr->nodeName; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | foreach ($attributesToRemove as $attrName) { |
||
| 155 | $node->removeAttribute($attrName); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | // Add all collected xmlns attributes to the root element |
||
| 160 | foreach ($xmlnsAttributes as $name => $value) { |
||
| 161 | $root->setAttribute($name, $value); |
||
| 162 | } |
||
| 163 | |||
| 164 | // Return the normalized XML |
||
| 165 | return static::fromString($root->ownerDocument->saveXML()); |
||
| 166 | } |
||
| 168 |