| Conditions | 19 |
| Paths | 84 |
| Total Lines | 74 |
| Code Lines | 29 |
| 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 |
||
| 161 | private static function registerSubtreePrefixes(DOMXPath $xp, DOMNode $node, array $prefixToUri): void |
||
| 162 | { |
||
| 163 | $root = $node instanceof DOMDocument |
||
| 164 | ? $node->documentElement |
||
| 165 | : ($node instanceof DOMElement ? $node : $node->parentNode); |
||
| 166 | |||
| 167 | if (!$root instanceof DOMElement) { |
||
| 168 | return; |
||
| 169 | } |
||
| 170 | |||
| 171 | // $visited = 0; |
||
| 172 | |||
| 173 | /** @var array<array{0:\DOMElement,1:int}> $queue */ |
||
| 174 | $queue = [[$root, 0]]; |
||
| 175 | |||
| 176 | while ($queue) { |
||
| 177 | /** @var \DOMElement $el */ |
||
| 178 | /** @var int $depth */ |
||
| 179 | [$el, $depth] = array_shift($queue); |
||
| 180 | |||
| 181 | // Depth guard: cap traversal at UNBOUNDED_LIMIT (root = depth 0). |
||
| 182 | // Breaking here halts further descent to avoid pathological depth and excessive work, |
||
| 183 | // which is safer in production than risking runaway traversal or hard failures. |
||
| 184 | // Trade-off: deeper descendant-only prefixes may remain unregistered, so some |
||
| 185 | // prefixed XPath queries might fail; overall processing continues gracefully. |
||
| 186 | if ($depth >= C_XML::UNBOUNDED_LIMIT) { |
||
| 187 | break; |
||
| 188 | } |
||
| 189 | |||
| 190 | // if (++$visited > C_XML::UNBOUNDED_LIMIT) { |
||
| 191 | // // Safety valve: stop further traversal to avoid unbounded work and noisy exceptions. |
||
| 192 | // // Returning here halts namespace registration for this subtree, which is safer in |
||
| 193 | // // production than risking pathological O(n) behavior or a hard failure (e.g. throwing |
||
| 194 | // // \RuntimeException(__METHOD__ . ': exceeded subtree traversal limit')). |
||
| 195 | // // Trade-off: some descendant-only prefixes may remain unregistered, so related XPath |
||
| 196 | // // queries might fail, but overall processing continues gracefully. |
||
| 197 | // break; |
||
| 198 | // } |
||
| 199 | |||
| 200 | // Element prefix |
||
| 201 | if ($el->prefix && !isset($prefixToUri[$el->prefix])) { |
||
| 202 | $uri = $el->namespaceURI; |
||
| 203 | if (is_string($uri) && $uri !== '') { |
||
| 204 | $xp->registerNamespace($el->prefix, $uri); |
||
| 205 | $prefixToUri[$el->prefix] = $uri; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | // Attribute prefixes (excluding xmlns) |
||
| 210 | if ($el->hasAttributes()) { |
||
| 211 | foreach ($el->attributes as $attr) { |
||
| 212 | if ( |
||
| 213 | $attr->prefix && |
||
| 214 | $attr->prefix !== 'xmlns' && |
||
| 215 | !isset($prefixToUri[$attr->prefix]) |
||
| 216 | ) { |
||
| 217 | $uri = $attr->namespaceURI; |
||
| 218 | if (is_string($uri) && $uri !== '') { |
||
| 219 | $xp->registerNamespace($attr->prefix, $uri); |
||
| 220 | $prefixToUri[$attr->prefix] = $uri; |
||
| 221 | } |
||
| 222 | } else { |
||
| 223 | // Optional: collision detection (same prefix, different URI) |
||
| 224 | // if ($prefixToUri[$pfx] !== $attr->namespaceURI) { |
||
| 225 | // // Default: skip rebind; could log a debug message here. |
||
| 226 | // } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | // Enqueue children (only DOMElement to keep types precise) |
||
| 232 | foreach ($el->childNodes as $child) { |
||
| 233 | if ($child instanceof DOMElement) { |
||
| 234 | $queue[] = [$child, $depth + 1]; |
||
| 235 | } |
||
| 263 |