Skip to content

Commit b4d8191

Browse files
committed
docs
1 parent 22f0586 commit b4d8191

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

doc/component/FAQ.markdown

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,17 @@ obtained through `$node->getAttribute('next')`.
5151

5252
`ParentConnectingVisitor` and `NodeConnectingVisitor` should not be used at the same time. The latter
5353
includes the functionality of the former.
54+
55+
56+
How can I limit the impact of cyclic references in the AST?
57+
-----
58+
59+
NodeConnectingVisitor adds a parent reference, which introduces a cycle. This means that the AST can now only be collected by cycle garbage collector.
60+
This in turn can lead to performance and/or memory issues.
61+
62+
To break the cyclic references between AST nodes `NodeConnectingVisitor` supports a boolean `$weakReferences` constructor parameter.
63+
When set to `true`, all attributes added by `NodeConnectingVisitor` will be wrapped into a `WeakReference` object.
64+
65+
After enabling this parameter, the parent node can be obtained through `$node->getAttribute('weak_parent')`,
66+
the previous node can be obtained through `$node->getAttribute('weak_previous')`, and the next node can be
67+
obtained through `$node->getAttribute('weak_next')`.

lib/PhpParser/NodeVisitor/NodeConnectingVisitor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
* Visitor that connects a child node to its parent node
1010
* as well as its sibling nodes.
1111
*
12-
* On the child node, the parent node can be accessed through
12+
* With <code>$weakReferences=false</code> on the child node, the parent node can be accessed through
1313
* <code>$node->getAttribute('parent')</code>, the previous
1414
* node can be accessed through <code>$node->getAttribute('previous')</code>,
1515
* and the next node can be accessed through <code>$node->getAttribute('next')</code>.
16+
*
17+
* With <code>$weakReferences=true</code> attribute names are prefixed by "weak_", e.g. "weak_parent".
1618
*/
1719
final class NodeConnectingVisitor extends NodeVisitorAbstract {
1820
/**

lib/PhpParser/NodeVisitor/ParentConnectingVisitor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
/**
1212
* Visitor that connects a child node to its parent node.
1313
*
14-
* On the child node, the parent node can be accessed through
14+
* With <code>$weakReferences=false</code> on the child node, the parent node can be accessed through
1515
* <code>$node->getAttribute('parent')</code>.
16+
*
17+
* With <code>$weakReferences=true</code> the attribute name is "weak_parent" instead.
1618
*/
1719
final class ParentConnectingVisitor extends NodeVisitorAbstract {
1820
/**

0 commit comments

Comments
 (0)