| Conditions | 12 |
| Paths | 85 |
| Total Lines | 45 |
| Code Lines | 21 |
| 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 |
||
| 127 | public static function normalizeDocument(DOMDocument $doc): DOMDocument |
||
| 128 | { |
||
| 129 | // Get the root element |
||
| 130 | $root = $doc->documentElement; |
||
| 131 | |||
| 132 | // Collect all xmlns attributes from the document |
||
| 133 | $xpath = XPath::getXPath($doc); |
||
| 134 | $xmlnsAttributes = []; |
||
| 135 | |||
| 136 | // Register all namespaces to ensure XPath can handle them |
||
| 137 | foreach ($xpath->query('//namespace::*') as $node) { |
||
| 138 | $name = $node->nodeName === 'xmlns' ? 'xmlns' : $node->nodeName; |
||
| 139 | if ($name !== 'xmlns:xml') { |
||
| 140 | $xmlnsAttributes[$name] = $node->nodeValue; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | // If no xmlns attributes found, return early with debug info |
||
| 145 | if (empty($xmlnsAttributes)) { |
||
| 146 | return $root->ownerDocument; |
||
|
|
|||
| 147 | } |
||
| 148 | |||
| 149 | // Remove xmlns attributes from all elements |
||
| 150 | $nodes = $xpath->query('//*[namespace::*]'); |
||
| 151 | foreach ($nodes as $node) { |
||
| 152 | if ($node instanceof DOMElement) { |
||
| 153 | $attributesToRemove = []; |
||
| 154 | foreach ($node->attributes as $attr) { |
||
| 155 | if (strpos($attr->nodeName, 'xmlns') === 0 || $attr->nodeName === 'xmlns') { |
||
| 156 | $attributesToRemove[] = $attr->nodeName; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | foreach ($attributesToRemove as $attrName) { |
||
| 160 | $node->removeAttribute($attrName); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | // Add all collected xmlns attributes to the root element |
||
| 166 | foreach ($xmlnsAttributes as $name => $value) { |
||
| 167 | $root->setAttribute($name, $value); |
||
| 168 | } |
||
| 169 | |||
| 170 | // Return the normalized XML |
||
| 171 | return static::fromString($root->ownerDocument->saveXML()); |
||
| 172 | } |
||
| 201 |