Visitor
in package
Utility for efficient AST traversal and modification.
visit() will walk through an AST using a depth first traversal, calling
the visitor's enter function at each node in the traversal, and calling the
leave function after visiting that node and all of its child nodes.
By returning different values from the enter and leave functions, the behavior of the visitor can be altered.
- no return (
void) or returnnull: no action -
Visitor::skipNode(): skips over the subtree at the current node of the AST -
Visitor::stop(): stop the Visitor completely -
Visitor::removeNode(): remove the current node - return any other value: replace this node with the returned value
When using visit() to edit an AST, the original AST will not be modified, and
a new version of the AST with the changes applied will be returned from the
visit function.
$editedAST = Visitor::visit($ast, [
'enter' => function (Node $node, $key, $parent, array $path, array $ancestors) {
// ...
},
'leave' => function (Node $node, $key, $parent, array $path, array $ancestors) {
// ...
}
]);
Alternatively to providing enter and leave functions, a visitor can
instead provide functions named the same as the kinds of AST nodes,
or enter/leave visitors at a named key, leading to four permutations of
visitor API:
-
Named visitors triggered when entering a node a specific kind.
Visitor::visit($ast, [ NodeKind::OBJECT_TYPE_DEFINITION => function (ObjectTypeDefinitionNode $node) { // enter the "ObjectTypeDefinition" node } ]); -
Named visitors that trigger upon entering and leaving a node of a specific kind.
Visitor::visit($ast, [ NodeKind::OBJECT_TYPE_DEFINITION => [ 'enter' => function (ObjectTypeDefinitionNode $node) { // enter the "ObjectTypeDefinition" node }, 'leave' => function (ObjectTypeDefinitionNode $node) { // leave the "ObjectTypeDefinition" node } ] ]); -
Generic visitors that trigger upon entering and leaving any node.
Visitor::visit($ast, [ 'enter' => function (Node $node) { // enter any node }, 'leave' => function (Node $node) { // leave any node } ]); -
Parallel visitors for entering and leaving nodes of a specific kind.
Visitor::visit($ast, [ 'enter' => [ NodeKind::OBJECT_TYPE_DEFINITION => function (ObjectTypeDefinitionNode $node) { // enter the "ObjectTypeDefinition" node } ], 'leave' => [ NodeKind::OBJECT_TYPE_DEFINITION => function (ObjectTypeDefinitionNode $node) { // leave the "ObjectTypeDefinition" node } ] ]);
Tags
Table of Contents
- VISITOR_KEYS = [AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::NAME => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::DOCUMENT => ['definitions'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::OPERATION_DEFINITION => ['name', 'variableDefinitions', 'directives', 'selectionSet'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::VARIABLE_DEFINITION => ['variable', 'type', 'defaultValue', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::VARIABLE => ['name'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::SELECTION_SET => ['selections'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::FIELD => ['alias', 'name', 'arguments', 'directives', 'selectionSet'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::ARGUMENT => ['name', 'value'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::FRAGMENT_SPREAD => ['name', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INLINE_FRAGMENT => ['typeCondition', 'directives', 'selectionSet'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::FRAGMENT_DEFINITION => [ 'name', // Note: fragment variable definitions are experimental and may be changed // or removed in the future. 'variableDefinitions', 'typeCondition', 'directives', 'selectionSet', ], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INT => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::FLOAT => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::STRING => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::BOOLEAN => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::NULL => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::ENUM => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::LST => ['values'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::OBJECT => ['fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::OBJECT_FIELD => ['name', 'value'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::DIRECTIVE => ['name', 'arguments'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::NAMED_TYPE => ['name'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::LIST_TYPE => ['type'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::NON_NULL_TYPE => ['type'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::SCHEMA_DEFINITION => ['description', 'directives', 'operationTypes'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::OPERATION_TYPE_DEFINITION => ['type'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::SCALAR_TYPE_DEFINITION => ['description', 'name', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::OBJECT_TYPE_DEFINITION => ['description', 'name', 'interfaces', 'directives', 'fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::FIELD_DEFINITION => ['description', 'name', 'arguments', 'type', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INPUT_VALUE_DEFINITION => ['description', 'name', 'type', 'defaultValue', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INTERFACE_TYPE_DEFINITION => ['description', 'name', 'interfaces', 'directives', 'fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::UNION_TYPE_DEFINITION => ['description', 'name', 'directives', 'types'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::ENUM_TYPE_DEFINITION => ['description', 'name', 'directives', 'values'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::ENUM_VALUE_DEFINITION => ['description', 'name', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INPUT_OBJECT_TYPE_DEFINITION => ['description', 'name', 'directives', 'fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::SCALAR_TYPE_EXTENSION => ['name', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::OBJECT_TYPE_EXTENSION => ['name', 'interfaces', 'directives', 'fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INTERFACE_TYPE_EXTENSION => ['name', 'interfaces', 'directives', 'fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::UNION_TYPE_EXTENSION => ['name', 'directives', 'types'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::ENUM_TYPE_EXTENSION => ['name', 'directives', 'values'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INPUT_OBJECT_TYPE_EXTENSION => ['name', 'directives', 'fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::DIRECTIVE_DEFINITION => ['description', 'name', 'arguments', 'locations'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::SCHEMA_EXTENSION => ['directives', 'operationTypes']]
- removeNode() : VisitorRemoveNode
- Returns marker for removing the current node.
- skipNode() : VisitorSkipNode
- Returns marker for skipping the subtree at the current node.
- stop() : VisitorStop
- Returns marker for stopping.
- visit() : mixed
- Visit the AST (see class description for details).
- visitInParallel() : VisitorArray
- Combines the given visitors to run in parallel.
- visitWithTypeInfo() : array<string|int, mixed>
- Creates a new visitor that updates TypeInfo and delegates to the given visitor.
- extractVisitFn() : callable|null
Constants
VISITOR_KEYS
public
mixed
VISITOR_KEYS
= [AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::NAME => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::DOCUMENT => ['definitions'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::OPERATION_DEFINITION => ['name', 'variableDefinitions', 'directives', 'selectionSet'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::VARIABLE_DEFINITION => ['variable', 'type', 'defaultValue', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::VARIABLE => ['name'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::SELECTION_SET => ['selections'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::FIELD => ['alias', 'name', 'arguments', 'directives', 'selectionSet'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::ARGUMENT => ['name', 'value'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::FRAGMENT_SPREAD => ['name', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INLINE_FRAGMENT => ['typeCondition', 'directives', 'selectionSet'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::FRAGMENT_DEFINITION => [
'name',
// Note: fragment variable definitions are experimental and may be changed
// or removed in the future.
'variableDefinitions',
'typeCondition',
'directives',
'selectionSet',
], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INT => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::FLOAT => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::STRING => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::BOOLEAN => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::NULL => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::ENUM => [], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::LST => ['values'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::OBJECT => ['fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::OBJECT_FIELD => ['name', 'value'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::DIRECTIVE => ['name', 'arguments'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::NAMED_TYPE => ['name'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::LIST_TYPE => ['type'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::NON_NULL_TYPE => ['type'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::SCHEMA_DEFINITION => ['description', 'directives', 'operationTypes'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::OPERATION_TYPE_DEFINITION => ['type'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::SCALAR_TYPE_DEFINITION => ['description', 'name', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::OBJECT_TYPE_DEFINITION => ['description', 'name', 'interfaces', 'directives', 'fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::FIELD_DEFINITION => ['description', 'name', 'arguments', 'type', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INPUT_VALUE_DEFINITION => ['description', 'name', 'type', 'defaultValue', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INTERFACE_TYPE_DEFINITION => ['description', 'name', 'interfaces', 'directives', 'fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::UNION_TYPE_DEFINITION => ['description', 'name', 'directives', 'types'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::ENUM_TYPE_DEFINITION => ['description', 'name', 'directives', 'values'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::ENUM_VALUE_DEFINITION => ['description', 'name', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INPUT_OBJECT_TYPE_DEFINITION => ['description', 'name', 'directives', 'fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::SCALAR_TYPE_EXTENSION => ['name', 'directives'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::OBJECT_TYPE_EXTENSION => ['name', 'interfaces', 'directives', 'fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INTERFACE_TYPE_EXTENSION => ['name', 'interfaces', 'directives', 'fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::UNION_TYPE_EXTENSION => ['name', 'directives', 'types'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::ENUM_TYPE_EXTENSION => ['name', 'directives', 'values'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::INPUT_OBJECT_TYPE_EXTENSION => ['name', 'directives', 'fields'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::DIRECTIVE_DEFINITION => ['description', 'name', 'arguments', 'locations'], AutomatticWooCommerceVendorGraphQLLanguageASTNodeKind::SCHEMA_EXTENSION => ['directives', 'operationTypes']]
Methods
removeNode()
Returns marker for removing the current node.
public
static removeNode() : VisitorRemoveNode
Return values
VisitorRemoveNode —skipNode()
Returns marker for skipping the subtree at the current node.
public
static skipNode() : VisitorSkipNode
Return values
VisitorSkipNode —stop()
Returns marker for stopping.
public
static stop() : VisitorStop
Return values
VisitorStop —visit()
Visit the AST (see class description for details).
public
static visit(NodeList<string|int, Node>|Node $root, VisitorArray $visitor[, array<string, mixed>|null $keyMap = null ]) : mixed
Parameters
- $root : NodeList<string|int, Node>|Node
- $visitor : VisitorArray
- $keyMap : array<string, mixed>|null = null
Tags
Return values
mixed —visitInParallel()
Combines the given visitors to run in parallel.
public
static visitInParallel(array<string|int, mixed> $visitors) : VisitorArray
Parameters
- $visitors : array<string|int, mixed>
Tags
Return values
VisitorArray —visitWithTypeInfo()
Creates a new visitor that updates TypeInfo and delegates to the given visitor.
public
static visitWithTypeInfo(TypeInfo $typeInfo, array<string|int, mixed> $visitor) : array<string|int, mixed>
Parameters
- $typeInfo : TypeInfo
- $visitor : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —extractVisitFn()
protected
static extractVisitFn(array<string|int, mixed> $visitor, string $kind, bool $isLeaving) : callable|null
Parameters
- $visitor : array<string|int, mixed>
- $kind : string
- $isLeaving : bool
