Skip to content

fix(parsers): capture PHP imports instead of trait uses - #1563

Merged
Shashankss1205 merged 1 commit into
mainfrom
fix/parser-php-imports
Aug 2, 2026
Merged

Shashankss1205 merged 1 commit into
mainfrom
fix/parser-php-imports

Conversation

@Shashankss1205

Copy link
Copy Markdown
Collaborator

Fixes #1522. Full suite: 1078 passed, 7 skipped, no failures.

The bug

The imports query captured use_declaration — which in tree-sitter-php is the trait use inside a class body, the same node type php.py already relies on at line ~309 for trait handling. A top-level use App\Models\User; is a namespace_use_declaration.

This ran in both directions:

  • Real imports were dropped entirely. PHP files produced no Module nodes and no IMPORTS edges, so resolution/calls.py had an empty local_imports map for PHP and no call to a class imported from another file could resolve.

  • Trait uses were recorded as imports. The repo's own fixture has a conflict-resolution block inside a class:

    class ConflictResolver {
        use Alpha, Beta {
            Beta::execute insteadof Alpha;
            ...
        }

    That became a Module node named Alpha whose full_import_name was the entire block, newlines and all.

The fix

Capture namespace_use_clause rather than the declaration. That is the level that exists once per imported symbol, so the braced group form expands for free. The handler now reads the AST instead of regexing the declaration text:

source name full_import_name alias
use App\Models\User; User App\Models\User —
use App\Models\Post as BlogPost; BlogPost App\Models\Post BlogPost
use Foo\Bar\{A, B as C}; A, C Foo\Bar\A, Foo\Bar\B —, C
use function Baz\helper; helper Baz\helper —

Two secondary improvements that fell out of it:

Trait handling is untouched and still works: a use Greets; inside a class yields imports == [] with Greets correctly in traits.

Golden

sample_project_php regenerated. Verified semantically rather than by line count:

REMOVED (bogus, trait-derived):
   'Alpha'  -> 'use Alpha, Beta {\n  Beta::execute insteadof Alpha; ... }'
   'Logger' -> 'use Logger;'
ADDED (real imports):
   'ClassLoader'  -> 'Composer\Autoload\ClassLoader'
   'Repository'   -> 'App\Models\Repository'
   'User'         -> 'App\Models\User'
   'UserService'  -> 'App\Services\UserService'
   'VersionParser'-> 'Composer\Semver\VersionParser'

New test file tests/unit/parsers/test_php_parser.py covers all five import forms, the trait-use case, and ordering.

🤖 Generated with Claude Code

The imports query captured `use_declaration`, which in tree-sitter-php is
the *trait* use inside a class body — the same node type php.py already
relies on for trait handling. A top-level `use App\Models\User;` is a
`namespace_use_declaration`.

The effect ran in both directions: real imports were dropped entirely, so
PHP files produced no Module nodes and no IMPORTS edges, while trait uses
were recorded as imports. In the repo's own fixture, `use Alpha, Beta {
... }` — a trait conflict-resolution block inside a class — became a
Module named "Alpha" whose full_import_name was the entire block.

Capture `namespace_use_clause` instead. That is the level that exists once
per imported symbol, so the braced group form expands for free, and the
handler now reads the AST rather than regexing the declaration text:

    use App\Models\User;              -> User      (App\Models\User)
    use App\Models\Post as BlogPost;  -> BlogPost  (App\Models\Post)
    use Foo\Bar\{A, B as C};          -> A         (Foo\Bar\A)
                                         C         (Foo\Bar\B)
    use function Baz\helper;          -> helper    (Baz\helper)

`full_import_name` is now the clean fully-qualified path rather than the
raw source text, and rows are sorted by line so output is deterministic.

Golden for sample_project_php regenerated: 5 real imports added, the 2
bogus trait-derived Modules removed.

Co-Authored-By: Claude Fable 5 <[email protected]>
@vercel

vercel Bot commented Aug 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
code-graph-context-pack Ready Ready Preview Aug 2, 2026 5:59pm

@github-project-automation github-project-automation Bot moved this to Backlog tasks in CGC Progress Board Aug 2, 2026
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Hi! 👋 Join our CodeGraphContext Discord channel to collaborate: https://discord.gg/dR4QY32uYQ

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

🔍 PR Code Graph Analysis

fix(parsers): capture PHP imports instead of trait uses (#1563)

📊 Interactive Visualization

View the blast radius graph: PR Reviewer Dashboard

📦 Artifacts

The graph JSON has been uploaded as a build artifact: pr-code-graph-1563


Generated by CodeGraphContext using FalkorDB Lite

@Shashankss1205
Shashankss1205 merged commit af8a4c7 into main Aug 2, 2026
19 checks passed
@github-project-automation github-project-automation Bot moved this from Backlog tasks to Done in CGC Progress Board Aug 2, 2026
@Shashankss1205
Shashankss1205 deleted the fix/parser-php-imports branch August 2, 2026 18:07
Shashankss1205 added a commit that referenced this pull request Aug 2, 2026
Bug-fix release. Since 0.5.4 on PyPI:

- fix(indexer): record non-code files on the SCIP path so `cgc index`
  stops reporting a partial index forever (#1565, #1566)
- fix(parsers): capture PHP imports rather than trait uses (#1522, #1563)
- fix(parsers): recover Python annotated parameters — 40.3% of parameters
  were being dropped — and Go grouped imports (#1520, #1521, #1561)
- fix(graph): always sanitize bundle filenames (#1517, #1541)
- fix(cli/mcp): read import aliases from the IMPORTS relationship (#1515, #1540)
- fix(cli/mcp): fall back to JSON when GCF encoding fails (#1518, #1539)

Co-authored-by: Claude Fable 5 <[email protected]>

This branch was successfully deployed

1 active deployment
Preview — 4b707390 Deployed Aug 2, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug(indexer): PHP imports query matches use_declaration (trait use) instead of namespace_use_declaration, so PHP files produce no imports at all

1 participant