ineersa / call-graph
PHPStan extension and DOT visualizer for application call graphs
Requires
- php: ^8.2
- phpstan/phpstan: ^2.1
Suggests
- phpstan/extension-installer: Automatically registers callgraph.neon in PHPStan
README
call-graph is a PHPStan extension plus CLI tools for extracting and visualizing call graphs.
It writes callgraph.json during PHPStan analysis. You can open that in an interactive HTML explorer (no Graphviz required) or render Graphviz DOT/SVG for static diagrams and docs.
Features
- Extracts method calls (
$obj->method()), static calls (Class::method()), and function calls (foo()). - Uses PHPStan type/reflection data to resolve declaring classes where possible.
- Emits structured JSON with metadata (
file,line,callType,unresolved). - Includes compatibility output (
data) for existing callmap-style tooling. - Interactive HTML explorer (Cytoscape.js) with URL-driven filters and node focus—no Graphviz install needed.
- Graphviz DOT/SVG output with namespace clustering and regex filtering.
- Excludes function-involved edges by default in visualization (use
--include-functionsto opt in). - Supports coupling-oriented views with namespace mode and edge-weight filtering.
Install
composer require --dev ineersa/call-graph
Requirements: PHP 8.2+ and PHPStan 2.1+.
If you use phpstan/extension-installer, callgraph.neon is auto-registered from package metadata.
Without extension-installer, include it manually in your phpstan.neon:
includes: - vendor/ineersa/call-graph/callgraph.neon
Generate call graph JSON
Run PHPStan with the extension config:
./vendor/bin/phpstan analyse -c vendor/ineersa/call-graph/callgraph.neon <path/to/src>
By default this writes callgraph.json in the current working directory.
Override output location in your own config:
includes: - vendor/ineersa/call-graph/callgraph.neon services: errorFormatter.callgraph: class: CallGraph\PHPStan\Formatter\CallGraphJsonFormatter arguments: outputFile: build/callgraph.json
Render visualization
Interactive HTML (recommended for exploration)
Loads the graph in the browser with pan/zoom, node focus, and shareable filter URLs. Does not require Graphviz.
./vendor/bin/callgraph-viz-html --input callgraph.json --html callgraph.html
Query parameters (changing them reloads the page):
mode=class|method|namespacenamespaceDepth=<n>minEdgeWeight=<n>maxNodes=<n>includeFunctions=1strictNamespaces=1(whennsis set, keep only relations where both sides match)ns=App\\Service,App\\Domain(comma-separated namespace prefixes)
Example:
file:///.../callgraph.html?mode=method&ns=App\Service,App\Domain&maxNodes=300
Graphviz (DOT and SVG)
For static diagrams, documentation, or pipelines that already use dot:
./vendor/bin/callgraph-viz --input callgraph.json --dot callgraph.dot ./vendor/bin/callgraph-viz --input callgraph.json --dot callgraph.dot --svg callgraph.svg
Useful filters:
./vendor/bin/callgraph-viz \ --mode method \ --include '/^App\\/' \ --exclude '/\\Tests\\/' \ --max-nodes 250
Large graph / coupling-style view:
./vendor/bin/callgraph-viz \
--mode namespace \
--namespace-depth 3 \
--min-edge-weight 2 \
--max-nodes 120 \
--include '/^App\\/' \
--dot coupling.dot
Include functions if needed:
./vendor/bin/callgraph-viz --include-functions --mode method
Output format
callgraph.json contains:
meta: generation metadataedges: full graph edges with enriched metadatadata: callmap-compatible shape (callingClass,callingMethod,calledClass,calledMethod)
Example edge:
{
"callerClass": "App\\Service\\UserService",
"callerMember": "getUser",
"callerKind": "method",
"calleeClass": "App\\Repository\\UserRepository",
"calleeMember": "find",
"calleeKind": "method",
"callType": "method",
"file": "src/Service/UserService.php",
"line": 41,
"unresolved": false
}
Acknowledgements
This project follows the PHPStan pattern for extracting structured data from analysis (collectors, a CollectedDataNode rule, and a custom error formatter), as described in Using PHPStan to Extract Data About Your Codebase.
The callmap-compatible data shape and the overall idea of emitting a JSON call map from PHPStan build on prior work in stella-maris/callmap.
