public static function visit($root, $visitor, $keyMap = null)
    {
        $visitorKeys = $keyMap ?: self::$visitorKeys;

        $stack     = null;
        $inArray   = $root instanceof NodeList || is_array($root);
        $keys      = [$root];
        $index     = -1;
        $edits     = [];
        $parent    = null;
        $path      = [];
        $ancestors = [];
        $newRoot   = $root;

        $UNDEFINED = null;

        do {
            $index++;
            $isLeaving = $index === count($keys);
            $key       = null;
            $node      = null;
            $isEdited  = $isLeaving && count($edits) !== 0;

            if ($isLeaving) {
                $key    = ! $ancestors ? $UNDEFINED : $path[count($path) - 1];
                $node   = $parent;
                $parent = array_pop($ancestors);

                if ($isEdited) {
                    if ($inArray) {
                        // $node = $node; // arrays are value types in PHP
                        if ($node instanceof NodeList) {
                            $node = clone $node;
                        }
                    } else {
                        $node = clone $node;
                    }
                    $editOffset = 0;
                    for ($ii = 0; $ii < count($edits); $ii++) {
                        $editKey   = $edits[$ii][0];
                        $editValue = $edits[$ii][1];

                        if ($inArray) {
                            $editKey -= $editOffset;
                        }
                        if ($inArray && $editValue === null) {
                            if ($node instanceof NodeList) {
                                $node->splice($editKey, 1);
                            } else {
                                array_splice($node, $editKey, 1);
                            }
                            $editOffset++;
                        } else {
                            if ($node instanceof NodeList || is_array($node)) {
                                $node[$editKey] = $editValue;
                            } else {
                                $node->{$editKey} = $editValue;
                            }
                        }
                    }
                }
                $index   = $stack['index'];
                $keys    = $stack['keys'];
                $edits   = $stack['edits'];
                $inArray = $stack['inArray'];
                $stack   = $stack['prev'];
            } else {
                $key  = $parent !== null ? ($inArray ? $index : $keys[$index]) : $UNDEFINED;
                $node = $parent !== null ? ($parent instanceof NodeList || is_array($parent) ? $parent[$key] : $parent->{$key}) : $newRoot;
                if ($node === null || $node === $UNDEFINED) {
                    continue;
                }
                if ($parent !== null) {
                    $path[] = $key;
                }
            }

            $result = null;
            if (! $node instanceof NodeList && ! is_array($node)) {
                if (! ($node instanceof Node)) {
                    throw new Exception('Invalid AST Node: ' . json_encode($node));
                }

                $visitFn = self::getVisitFn($visitor, $node->kind, $isLeaving);

                if ($visitFn) {
                    $result    = call_user_func($visitFn, $node, $key, $parent, $path, $ancestors);
                    $editValue = null;

                    if ($result !== null) {
                        if ($result instanceof VisitorOperation) {
                            if ($result->doBreak) {
                                break;
                            }
                            if (! $isLeaving && $result->doContinue) {
                                array_pop($path);
                                continue;
                            }
                            if ($result->removeNode) {
                                $editValue = null;
                            }
                        } else {
                            $editValue = $result;
                        }

                        $edits[] = [$key, $editValue];
                        if (! $isLeaving) {
                            if (! ($editValue instanceof Node)) {
                                array_pop($path);
                                continue;
                            }

                            $node = $editValue;
                        }
                    }
                }
            }

            if ($result === null && $isEdited) {
                $edits[] = [$key, $node];
            }

            if ($isLeaving) {
                array_pop($path);
            } else {
                $stack   = [
                    'inArray' => $inArray,
                    'index'   => $index,
                    'keys'    => $keys,
                    'edits'   => $edits,
                    'prev'    => $stack,
                ];
                $inArray = $node instanceof NodeList || is_array($node);

                $keys  = ($inArray ? $node : $visitorKeys[$node->kind]) ?: [];
                $index = -1;
                $edits = [];
                if ($parent !== null) {
                    $ancestors[] = $parent;
                }
                $parent = $node;
            }
        } while ($stack);

        if (count($edits) !== 0) {
            $newRoot = $edits[0][1];
        }

        return $newRoot;
    }
public static function visit($root, $visitor, $keyMap = null)
    {
        $visitorKeys = $keyMap ?: self::$visitorKeys;

        $stack     = null;
        $inArray   = $root instanceof NodeList || is_array($root);
        $keys      = [$root];
        $index     = -1;
        $edits     = [];
        $parent    = null;
        $path      = [];
        $ancestors = [];
        $newRoot   = $root;

        $UNDEFINED = null;

        do {
            $index++;
            $isLeaving = $index === count($keys);
            $key       = null;
            $node      = null;
            $isEdited  = $isLeaving && count($edits) !== 0;

            if ($isLeaving) {
                $key    = ! $ancestors ? $UNDEFINED : $path[count($path) - 1];
                $node   = $parent;
                $parent = array_pop($ancestors);

                if ($isEdited) {
                    if ($inArray) {
                        
                        if ($node instanceof NodeList) {
                            $node = clone $node;
                        }
                    } else {
                        $node = clone $node;
                    }
                    $editOffset = 0;
                    for ($ii = 0; $ii < count($edits); $ii++) {
                        $editKey   = $edits[$ii][0];
                        $editValue = $edits[$ii][1];

                        if ($inArray) {
                            $editKey -= $editOffset;
                        }
                        if ($inArray && $editValue === null) {
                            if ($node instanceof NodeList) {
                                $node->splice($editKey, 1);
                            } else {
                                array_splice($node, $editKey, 1);
                            }
                            $editOffset++;
                        } else {
                            if ($node instanceof NodeList || is_array($node)) {
                                $node[$editKey] = $editValue;
                            } else {
                                $node->{$editKey} = $editValue;
                            }
                        }
                    }
                }
                $index   = $stack['index'];
                $keys    = $stack['keys'];
                $edits   = $stack['edits'];
                $inArray = $stack['inArray'];
                $stack   = $stack['prev'];
            } else {
                $key  = $parent !== null ? ($inArray ? $index : $keys[$index]) : $UNDEFINED;
                $node = $parent !== null ? ($parent instanceof NodeList || is_array($parent) ? $parent[$key] : $parent->{$key}) : $newRoot;
                if ($node === null || $node === $UNDEFINED) {
                    continue;
                }
                if ($parent !== null) {
                    $path[] = $key;
                }
            }

            $result = null;
            if (! $node instanceof NodeList && ! is_array($node)) {
                if (! ($node instanceof Node)) {
                    throw new Exception('Invalid AST Node: ' . json_encode($node));
                }

                $visitFn = self::getVisitFn($visitor, $node->kind, $isLeaving);

                if ($visitFn) {
                    $result    = call_user_func($visitFn, $node, $key, $parent, $path, $ancestors);
                    $editValue = null;

                    if ($result !== null) {
                        if ($result instanceof VisitorOperation) {
                            if ($result->doBreak) {
                                break;
                            }
                            if (! $isLeaving && $result->doContinue) {
                                array_pop($path);
                                continue;
                            }
                            if ($result->removeNode) {
                                $editValue = null;
                            }
                        } else {
                            $editValue = $result;
                        }

                        $edits[] = [$key, $editValue];
                        if (! $isLeaving) {
                            if (! ($editValue instanceof Node)) {
                                array_pop($path);
                                continue;
                            }

                            $node = $editValue;
                        }
                    }
                }
            }

            if ($result === null && $isEdited) {
                $edits[] = [$key, $node];
            }

            if ($isLeaving) {
                array_pop($path);
            } else {
                $stack   = [
                    'inArray' => $inArray,
                    'index'   => $index,
                    'keys'    => $keys,
                    'edits'   => $edits,
                    'prev'    => $stack,
                ];
                $inArray = $node instanceof NodeList || is_array($node);

                $keys  = ($inArray ? $node : $visitorKeys[$node->kind]) ?: [];
                $index = -1;
                $edits = [];
                if ($parent !== null) {
                    $ancestors[] = $parent;
                }
                $parent = $node;
            }
        } while ($stack);

        if (count($edits) !== 0) {
            $newRoot = $edits[0][1];
        }

        return $newRoot;
    }
public static function visit ( $ root , $ visitor , $ keyMap = null ) { $ visitorKeys = $ keyMap ? : self :: $ visitorKeys ; $ stack = null ; $ inArray = $ root instanceof NodeList || is_array ( $ root ) ; $ keys = [ $ root ] ; $ index = - 1 ; $ edits = [ ] ; $ parent = null ; $ path = [ ] ; $ ancestors = [ ] ; $ newRoot = $ root ; $ UNDEFINED = null ; do { $ index ++ ; $ isLeaving = $ index === count ( $ keys ) ; $ key = null ; $ node = null ; $ isEdited = $ isLeaving && count ( $ edits ) !== 0 ; if ( $ isLeaving ) { $ key = ! $ ancestors ? $ UNDEFINED : $ path [ count ( $ path ) - 1 ] ; $ node = $ parent ; $ parent = array_pop ( $ ancestors ) ; if ( $ isEdited ) { if ( $ inArray ) { // $node = $node; // arrays are value types in PHP if ( $ node instanceof NodeList ) { $ node = clone $ node ; } } else { $ node = clone $ node ; } $ editOffset = 0 ; for ( $ ii = 0 ; $ ii < count ( $ edits ) ; $ ii ++ ) { $ editKey = $ edits [ $ ii ] [ 0 ] ; $ editValue = $ edits [ $ ii ] [ 1 ] ; if ( $ inArray ) { $ editKey -= $ editOffset ; } if ( $ inArray && $ editValue === null ) { if ( $ node instanceof NodeList ) { $ node -> splice ( $ editKey , 1 ) ; } else { array_splice ( $ node , $ editKey , 1 ) ; } $ editOffset ++ ; } else { if ( $ node instanceof NodeList || is_array ( $ node ) ) { $ node [ $ editKey ] = $ editValue ; } else { $ node -> { $ editKey } = $ editValue ; } } } } $ index = $ stack [ 'index' ] ; $ keys = $ stack [ 'keys' ] ; $ edits = $ stack [ 'edits' ] ; $ inArray = $ stack [ 'inArray' ] ; $ stack = $ stack [ 'prev' ] ; } else { $ key = $ parent !== null ? ( $ inArray ? $ index : $ keys [ $ index ] ) : $ UNDEFINED ; $ node = $ parent !== null ? ( $ parent instanceof NodeList || is_array ( $ parent ) ? $ parent [ $ key ] : $ parent -> { $ key } ) : $ newRoot ; if ( $ node === null || $ node === $ UNDEFINED ) { continue ; } if ( $ parent !== null ) { $ path [ ] = $ key ; } } $ result = null ; if ( ! $ node instanceof NodeList && ! is_array ( $ node ) ) { if ( ! ( $ node instanceof Node ) ) { throw new Exception ( 'Invalid AST Node: ' . json_encode ( $ node ) ) ; } $ visitFn = self :: getVisitFn ( $ visitor , $ node -> kind , $ isLeaving ) ; if ( $ visitFn ) { $ result = call_user_func ( $ visitFn , $ node , $ key , $ parent , $ path , $ ancestors ) ; $ editValue = null ; if ( $ result !== null ) { if ( $ result instanceof VisitorOperation ) { if ( $ result -> doBreak ) { break ; } if ( ! $ isLeaving && $ result -> doContinue ) { array_pop ( $ path ) ; continue ; } if ( $ result -> removeNode ) { $ editValue = null ; } } else { $ editValue = $ result ; } $ edits [ ] = [ $ key , $ editValue ] ; if ( ! $ isLeaving ) { if ( ! ( $ editValue instanceof Node ) ) { array_pop ( $ path ) ; continue ; } $ node = $ editValue ; } } } } if ( $ result === null && $ isEdited ) { $ edits [ ] = [ $ key , $ node ] ; } if ( $ isLeaving ) { array_pop ( $ path ) ; } else { $ stack = [ 'inArray' => $ inArray , 'index' => $ index , 'keys' => $ keys , 'edits' => $ edits , 'prev' => $ stack , ] ; $ inArray = $ node instanceof NodeList || is_array ( $ node ) ; $ keys = ( $ inArray ? $ node : $ visitorKeys [ $ node -> kind ] ) ? : [ ] ; $ index = - 1 ; $ edits = [ ] ; if ( $ parent !== null ) { $ ancestors [ ] = $ parent ; } $ parent = $ node ; } } while ( $ stack ) ; if ( count ( $ edits ) !== 0 ) { $ newRoot = $ edits [ 0 ] [ 1 ] ; } return $ newRoot ; }
Visit the AST (see class description for details)

@param Node|ArrayObject|stdClass $root
@param callable[]                $visitor
@param mixed[]|null              $keyMap

@return Node|mixed

@throws Exception

@api
Visit the AST ( see class description for details )