adachsoft / code-graph-contract
Typed graph models and serialization contracts for representing source-code structure and dependencies.
Requires
- php: ^8.3
- adachsoft/collection: ^3.0
Requires (Dev)
- adachsoft/php-code-style: ^0.7.1
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
- rector/rector: ^2.8
- symplify/phpstan-rules: ^14.19.3
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is not auto-updated.
Last update: 2026-11-03 09:56:24 UTC
README
Code Graph Contract is a PHP library for representing source-code structure and dependencies as a typed, format-neutral graph. It provides graph models and import/export contracts; it does not parse source files.
Features
- Model modules, files, class-like elements, methods, and functions as graph nodes.
- Represent directed relationships such as calls, usage, inheritance, and type references.
- Attach optional source locations and metadata to nodes and relationships.
- Export graphs as deterministic canonical JSON or Mermaid module and class diagrams.
- Register and look up exporters through a typed registry.
- Import graphs from the canonical JSON representation.
Requirements
- PHP 8.3 or newer.
Installation
composer require adachsoft/code-graph-contract
Quick start
<?php
declare(strict_types=1);
use AdachSoft\CodeGraphContract\Enum\NodeKindEnum;
use AdachSoft\CodeGraphContract\Enum\RelationTypeEnum;
use AdachSoft\CodeGraphContract\Factory\CodeGraphFactory;
use AdachSoft\CodeGraphContract\Factory\GraphExporterFactory;
use AdachSoft\CodeGraphContract\ValueObject\EdgeVo;
use AdachSoft\CodeGraphContract\ValueObject\NodeVo;
require __DIR__ . '/vendor/autoload.php';
$graph = CodeGraphFactory::createEmpty();
$graph->addNode(new NodeVo(
id: 'class:billing-service',
name: 'BillingService',
kind: NodeKindEnum::CLASS_LIKE,
module: 'App\\Billing',
));
$graph->addNode(new NodeVo(
id: 'class:invoice-repository',
name: 'InvoiceRepository',
kind: NodeKindEnum::CLASS_LIKE,
module: 'App\\Persistence',
));
$graph->addEdge(new EdgeVo(
sourceId: 'class:billing-service',
targetId: 'class:invoice-repository',
relationType: RelationTypeEnum::USES,
));
$exporters = GraphExporterFactory::createDefaultRegistry();
$json = $exporters->get('json')->export($graph);
$mermaid = $exporters->get('mermaid-classes')->export($graph);
The default exporter registry supports json, mermaid-modules, and mermaid-classes. See the graph construction example and export examples for more details.
Scope
This package defines graph data structures and serialization boundaries for code-analysis tools. Source discovery and parsing are responsibilities of the integrating application.