Skip to content

Core\Ast\Node::nodes

$node->nodes(): array<Core\Ast\Node>

Every node inside this one, however deep it sits, in the order they appear in the source.

children goes one step down. nodes keeps going: the children, their children, and so on to the bottom of the tree. On the node Core\Ast::parse gives you, that is every node of the file at once, which is what you want when you are looking for something and do not care where it sits.

The node you call it on is not in the result. Both members answer what the node contains, and a node does not contain itself. A node with nothing inside it gives you an empty array.

Good to know: the array is built when you ask for it, not kept on the node. Asking every node of a large file for its own nodes walks the tree once per node, so keep the result if you need it twice.

The examples below show how much deeper this goes than children, how big each class in a file is, and which constructs a file uses.

Return value

Returns array<Core\Ast\Node>. All nested nodes in source order. The node itself is not in the list, the same as with children.

Examples

Everything below a node not just one step

<?nvs
// Shows the difference between `children` and `nodes`. `children` gives the
// parts one step down. `nodes` gives every part below, to the bottom of the
// tree.
Core\Ast\Node $file = Core\Ast::parse('<?nvs if (1) { echo 2; }');
// This prints 1: the file holds one statement.
echo 'one step down: ', Core\Arr::count($file->children()), "\n";
// This prints 5: the `if`, its condition, its block, the `echo` and the 2.
echo 'everything below: ', Core\Arr::count($file->nodes()), "\n";
foreach ($file->nodes() as Core\Ast\Node $node) {
echo $node->kind(), "\n";
}
Output
one step down: 1
everything below: 5
If
Int
Block
Echo
Int

How big each class in a file is

<?nvs
// Shows how to measure each class in a file. The number of nodes below a class
// says how much it does, so this finds the class worth splitting first. The
// tree carries no names, so the sizes come out in the order the classes appear.
string $source = <<<'NVS'
<?nvs
class Invoice {
public static function total(int $a, int $b): int {
return $a + $b;
}
}
class Receipt {
public static function line(): string {
return "one";
}
public static function footer(): string {
return "two";
}
}
NVS;
// This prints 5, then 6.
foreach (Core\Ast::parse($source)->children() as Core\Ast\Node $class) {
echo $class->kind(), ' ', Core\Arr::count($class->nodes()), "\n";
}
Output
ClassDecl 5
ClassDecl 6

Which constructs a file uses

<?nvs
// Shows which constructs a file uses. A tool that moves code from one language
// to another checks this first, to see whether the file needs anything it
// cannot handle yet.
string $source = <<<'NVS'
<?nvs
class Invoice {
public static function total(int $a, int $b): int {
return $a + $b;
}
}
NVS;
array<string> $kinds = [];
foreach (Core\Ast::parse($source)->nodes() as Core\Ast\Node $node) {
$kinds = Core\Arr::append($kinds, $node->kind());
}
// This prints Binary, ClassDecl, Method, Return and Variable, one per line.
foreach (Core\Arr::sort(Core\Arr::unique($kinds)) as string $kind) {
echo $kind, "\n";
}
Output
Binary
ClassDecl
Method
Return
Variable