codegraph icon indicating copy to clipboard operation
codegraph copied to clipboard

fix(extraction): map PHP include/require to file→file dependency edges (#660)

Open maxmilian opened this issue 5 months ago • 0 comments

Fixes #660.

Procedural / script-style PHP wires files together with include/require, but codegraph's PHP extractor only captured namespace_use_declaration as an import — so include/require(_once) produced zero file→file dependency edges. callers(lib.php) returned 0 for require_once("lib.php"); impact/trace missed the whole file-include graph.

What changed

  • src/extraction/languages/php.ts (+41): capture include / include_once / require / require_once expressions and extract the static string-literal path (single/double quotes, with/without parens). Dynamic forms (include $var, require __DIR__ . '/x', interpolated strings) resolve to null and are skipped.
  • src/resolution/import-resolver.ts (+71): resolve a PHP include path to a file→file imports edge relative to the including file, mirroring the existing C/C++ #include path. Added isPhpIncludePathRef to distinguish include paths (contain / or .) from namespace use symbols (FQN / bare class — which never do).
  • src/resolution/index.ts (+14): a path-shaped include that doesn't resolve to a known project file is left unresolved and does not fall back to the symbol name-matcher (which would otherwise mis-connect inc/db.php to an unrelated db.php elsewhere).

Before / After

Before After
require_once("lib.php") no edge; callers(lib.php) = 0 file→file imports edge; callers(lib.php) includes the caller
require "inc/db.php" (subdir) no edge resolves to inc/db.php
include $var / require __DIR__.'/x' no edge unchanged — skipped (no static path)
namespace use App\Foo\Bar resolved as before unchanged
unresolvable require "inc/db.php" — no edge (never mis-connects to a same-named file elsewhere)

Validation

npm test on the changed suites (Node 24.14.0):

  • npx vitest run __tests__/resolution.test.ts — 56/56 passing (+4 new: include→file e2e, subdirectory resolution, no-mis-connect regression, isPhpIncludePathRef unit test)
  • npx vitest run __tests__/extraction.test.ts -t "#660" — 3/3 new extraction tests passing (four include/require forms, dynamic-skip, include + namespace-use coexistence)

Note: a full extraction.test.ts run hits a pre-existing V8 Fatal process out of memory: Zone crash (~test 205) during turboshaft JIT compilation of the tree-sitter WASM grammars on Node 24.x — reproduced identically on unmodified main, so it is unrelated to this change. The repo's engines are >=18 <25.

Scope

PHP-only — other languages' extractors and resolvers are untouched (the new resolution branch is gated on language === 'php'). Static literal paths only, matching the issue scope; php.ini include_path is not modeled.

maxmilian avatar Jun 03 '26 07:06 maxmilian