Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
12 / 13
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DOMPostOrder
92.31% covered (success)
92.31%
12 / 13
50.00% covered (danger)
50.00%
1 / 2
7.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 traverse
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Utils;
5
6use Wikimedia\Parsoid\DOM\Node;
7
8/**
9 * Non-recursive post-order traversal of a DOM tree.
10 */
11class DOMPostOrder {
12    /**
13     * @suppress PhanEmptyPrivateMethod
14     */
15    private function __construct() {
16        /* Not meant to be instantiated. */
17    }
18
19    /**
20     * Non-recursive post-order traversal of a DOM tree.
21     * @param Node $root
22     * @param callable $visitFunc Called in post-order on each node.
23     */
24    public static function traverse( Node $root, callable $visitFunc ): void {
25        $node = $root;
26        while ( true ) {
27            // Find leftmost (grand)child, and visit that first.
28            while ( $node->firstChild ) {
29                $node = $node->firstChild;
30            }
31            while ( true ) {
32                $visitFunc( $node );
33                if ( $node === $root ) {
34                    return; // Visiting the root is the last thing we do.
35                }
36                /* Look for right sibling to continue traversal. */
37                if ( $node->nextSibling ) {
38                    $node = $node->nextSibling;
39                    /* Loop back and visit its leftmost (grand)child first. */
40                    break;
41                }
42                /* Visit parent only after we've run out of right siblings. */
43                $node = $node->parentNode;
44            }
45        }
46    }
47}