Skip to main content

php-parser-py

PHP's parser, in py - Drive nikic/PHP-Parser from Python.

Parse, query, mutate, and regenerate PHP code in Python through a graph API — no local PHP required.

Install

# via uv
uv add php-parser-py

# or via pip
pip install php-parser-py

Requires Python >= 3.10.

Usage

from php_parser_py import parse_file, Modifier, PHPVersion

# Parse (the target PHP version is required)
ast = parse_file("src/User.php", target_php=PHPVersion(8, 3))

# Query
functions = ast.nodes(lambda n: n.node_type == "Stmt_Function")
for func in functions:
    params = [c for c in ast.succ(func) if c.node_type == "Param"]

# Modify structure (copy-on-write: source `ast` is untouched)
modifier = Modifier(ast)
modifier.add_node("new_1", "Stmt_Break")
modifier.add_edge("parent_id", "new_1", field="stmts", index=0)
modified = modifier.ast

API

Function / Class Signature Description
PHPVersion (major: int, minor: int) Target PHP version, 7.0 to 7.4 and 8.0 to 8.5
parse_code (code: str, *, target_php: PHPVersion) -> list[Node] Parse PHP string into statement nodes
parse_file (path: str, *, target_php: PHPVersion) -> AST Parse single file with project/file structure
parse_project (project_path: str, file_filter?, *, target_php: PHPVersion) -> AST Parse all PHP files in a directory
AST.target_php -> PHPVersion | None Version the graph was parsed under (read-only)
Modifier (ast: AST) Copy-on-write graph mutation; read result from modifier.ast
AST.nodes (predicate?) -> Generator[Node] Iterate/filter nodes
AST.succ (node) -> Generator[Node] Get children
AST.prev (node) -> Generator[Node] Get parents
AST.first_node (predicate) -> Node | None First matching node
Node.node_type -> str PHP-Parser node type (e.g. "Stmt_Function")
Node.start_line -> int Source line number

Full specifications in docs/design-parser.md and docs/design-graph.md.

Background

Python wrapper for nikic/PHP-Parser built on the cpg2py graph framework. Ships a static PHP binary in platform-specific wheels; falls back to a php on PATH when no bundled binary matches the host.

Documentation

  • Concepts — problem scope, data flow, and core terminology.
  • Design — architecture, class specifications, and mapping rules.
  • AST Reference — complete table of PHP-Parser node types.
  • cpg2py API — graph querying patterns.

For Agents

Agent-consumable documentation index at docs/llms.txt (llmstxt.org format).

Citation

If you use this project in academic work, please cite our paper. BibTeX (placeholder until the paper is released):

@misc{citation_placeholder,
  title  = {{TODO: paper title — not yet released}},
  author = {{TODO: authors}},
  year   = {{TODO}},
  note   = {Paper not yet released. Citation entry will be updated on publication.}
}

License

GPL-2.0 — see LICENSE.

Metadata

Release files for php-parser-py 2.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for php-parser-py 2.0.3
File Actions
php_parser_py-2.0.3-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for php_parser_py-2.0.3-py3-none-win_amd64.whl
php_parser_py-2.0.3-py3-none-manylinux_2_17_x86_64.whl Python 3 none Linux glibc 2.17+ x86-64 Details for php_parser_py-2.0.3-py3-none-manylinux_2_17_x86_64.whl
php_parser_py-2.0.3-py3-none-manylinux_2_17_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64 Details for php_parser_py-2.0.3-py3-none-manylinux_2_17_aarch64.whl
php_parser_py-2.0.3-py3-none-macosx_11_0_x86_64.whl Python 3 none macOS 11.0+ x86-64 Details for php_parser_py-2.0.3-py3-none-macosx_11_0_x86_64.whl
php_parser_py-2.0.3-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for php_parser_py-2.0.3-py3-none-macosx_11_0_arm64.whl

Total release size: 17.2 MB

Release files / php_parser_py-2.0.3-py3-none-win_amd64.whl

Download URL php_parser_py-2.0.3-py3-none-win_amd64.whl
Size 3.8 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
6c4256811711f39957f0339a1def5a3edeb656f1693e8b5ab46a3b23779e63c4
BLAKE2b-256 checksum
How to use checksums
0a33b39e925b1d73ba989e676c883926e68c722371cf95cd40a6e9a02497d983
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / php_parser_py-2.0.3-py3-none-manylinux_2_17_x86_64.whl

Download URL php_parser_py-2.0.3-py3-none-manylinux_2_17_x86_64.whl
Size 3.3 MB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
dcccb062cdb55b3c4228c32074b6d2cb29c1d1b527a16ca3364bf6c180eec4eb
BLAKE2b-256 checksum
How to use checksums
6ff5d29e992458d75ac6bfb45e57c06ed446d2bff120ee750816c5039746672f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / php_parser_py-2.0.3-py3-none-manylinux_2_17_aarch64.whl

Download URL php_parser_py-2.0.3-py3-none-manylinux_2_17_aarch64.whl
Size 3.3 MB
Tags Linux glibc 2.17+ ARM64 Python 3
SHA-256 checksum
How to use checksums
51d25233b18a6658059ed50881c98d476f18f2e01ffc0ea5666007eb4eb1444f
BLAKE2b-256 checksum
How to use checksums
57f6badbe310ada86340d40f8ce0d29d0088093e3dec5be0df66bdc518a127b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / php_parser_py-2.0.3-py3-none-macosx_11_0_x86_64.whl

Download URL php_parser_py-2.0.3-py3-none-macosx_11_0_x86_64.whl
Size 3.6 MB
Tags Python 3 macOS 11.0+ x86-64
SHA-256 checksum
How to use checksums
a7d4e80655d1ec3daab422ce532f04a087014183aa3e1c92457667310c58f43f
BLAKE2b-256 checksum
How to use checksums
2494c68b096d4d50f1188a3d2524f1bb20965f41c9fe8e4c4ebf83f60077f957
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / php_parser_py-2.0.3-py3-none-macosx_11_0_arm64.whl

Download URL php_parser_py-2.0.3-py3-none-macosx_11_0_arm64.whl
Size 3.4 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a4e84e3fffbf32911aa0d3a6cc5a3ca7435b3808a095211a732b06fb41424e40
BLAKE2b-256 checksum
How to use checksums
79864378af3108092d1ec7346ae64b88622c225477a5a4585490f3caece09548
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Security for php-parser-py 2.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page