Search by

adachsoft / code-graph-contract

Arkadiusz Adach

Typed graph models and serialization contracts for representing source-code structure and dependencies.

Package info

gitlab.com/a.adach/code-graph-contract

Issues

pkg:composer/adachsoft/code-graph-contract

Statistics

Installs: 12

Dependents: 2

Suggesters: 0

Stars: 0

v0.1.0 2026-11-02 12:12 UTC

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.