tree-sitter-php

0.24.1 · active · verified Thu Apr 09

tree-sitter-php provides the PHP grammar for the tree-sitter parsing system. It enables incremental parsing to build concrete syntax trees for PHP source code, facilitating advanced code analysis, linting, and editor features. The library is actively maintained, with version 0.24.1 being the latest, and receives frequent updates to support new PHP language features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load the PHP grammar, initialize a parser, and parse a sample PHP code snippet. It then shows how to access the root node of the generated syntax tree and perform a simple query to extract class and method names.

from tree_sitter import Language, Parser
import tree_sitter_php

# Load the PHP grammar
PHP_LANGUAGE = Language(tree_sitter_php.language())
parser = Parser(PHP_LANGUAGE)

php_code = b'''
<?php

class MyClass {
    public function __construct(private string $name) {}

    public function greet(): string {
        return "Hello, " . $this->name . "!";
    }
}

$obj = new MyClass("World");
echo $obj->greet();

?>
'''

# Parse the code
tree = parser.parse(php_code)

# Get the root node of the syntax tree
root_node = tree.root_node

print(f"Root node type: {root_node.type}")
print(f"Number of children: {len(root_node.children)}")
# Example: Find a class_declaration node
for child in root_node.children:
    if child.type == 'declaration_list': # often wrappers around class/function
        for decl in child.children:
            if decl.type == 'class_declaration':
                print(f"Found class: {decl.child_by_field_name('name').text.decode('utf8')}")
                break

# You can also use queries for more specific pattern matching
query = PHP_LANGUAGE.query("""
(class_declaration name: (name) @class-name)
(method_declaration name: (name) @method-name)
""")

captures = query.captures(root_node)
for node, name in captures:
    print(f"Captured: {name.replace('-', '_')}: {node.text.decode('utf8')}")

view raw JSON →