Skip to content

Spec: PHP use-import-map awareness for member-call resolution (upgrade path for #16) #18

Closed

Description

@filipechagas

Problem

Measured during 0.9.34 release verification (probe scenario "10 use-alias outside corpus (Risk 6)") and filed as #16: a PHP property typed via a use alias whose target is outside the corpus, combined with exactly one unrelated in-corpus class of the same short name, mints a wrong INFERRED 0.8 calls edge.

Minimal repro (two files; the false edge is App\Http\I::go -> App\Local\Client::send INFERRED 0.8):

// app/Local/Client.php
<?php
namespace App\Local;
class Client { public function send(): int { return 1; } }
// app/Http/I.php
<?php
namespace App\Http;
use Vendor\Sdk\Client;
class I {
    private Client $c;
    public function go(): int { return $this->c->send(); }
}

Root cause: _resolve_php_member_calls binds a short receiver-type name through a corpus-wide short-name index (type_def_nids, extract.py:3166-3173) whose only refusal rule is "more than one candidate" (extract.py:3247-3260). The use alias makes Client a claimed name that should be decisive, but the resolver never sees use statements. Compounding: _php_name_text (engine.py:671-675) flattens every written type to its short name, so even a fully-qualified \Vendor\Sdk\Client annotation is indistinguishable from a bare Client.

This was accepted as out of scope by spec #1 and documented in the 0.9.34 changelog; this spec is the documented upgrade path: use-import-map awareness following the C# CsharpNameResolver pattern.

Prior art: the C# mechanism (v8 @ 102ae79)

  • Capture — _import_csharp (extract.py:577-605): every using directive becomes an imports edge that preserves target_fqn, alias, using_kind, and lexical scope (scope_kind/scope_id).
  • Storage — CsharpNameResolver.__init__ (graphify/extractors/csharp.py:174-222) rebuilds per-file using/alias maps from those edges (no re-parse) plus a (namespace, simple_name) -> node_id index from node metadata.namespace.
  • Consult — resolve_type_name (csharp.py:269-296) returns a (node_id, decisive) verdict. The trick is decisive-on-miss: an alias to an out-of-corpus type returns (None, True), and the caller (extract.py:2870-2885) then refuses instead of falling back to the corpus-wide bare-name index. That refusal is exactly what PHP is missing.

Existing PHP infrastructure to reuse

  • A correct use-parser already exists: _resolve_php_type_references (graphify/extractors/resolution.py:2417-2648) builds uses_by_file via _record_use_clause (resolution.py:2476-2495) and the namespace_use_declaration walk (:2504-2517). It already handles group use use A\{B, C};, aliases use X as Y;, aliased group use, and rejects use function / use const. But its maps are function-local and it only repoints inherits/implements/mixes_in/imports/references — nothing survives for the member-call pass.
  • The lossy counterpart is _import_php (extract.py:664-681): raw.split("\\")[-1], emits imports edges with no metadata at all.
  • Namespace facts exist twice: _php_pre_scan_class_namespaces (engine.py:183-237, per-file short -> FQN, feeds inline-new corroboration only) and the re-parse in resolution.py. Two different multi-namespace-file policies (per-declaration vs bail-entirely).
  • Incremental-rebuild carry-over precedent (commits 8d051c6, 85bb8df): persisted underscore markers on the file node (engine.py:5262-5290), preserved in graph.json (extract.py:6171-6176), read back in watch.py:1205-1225 / cli.py:3140-3160, re-injected via _php_context_interface_entry (extract.py:3106-3136, wired at :6019-6034).

Design decisions (resolved; flagged for maintainer review)

  1. Reuse the resolution.py use-parser — do not write a second one. Extract _record_use_clause + the namespace_use_declaration walk into a shared helper consumed by both _resolve_php_type_references and the new capture path. A fresh bare-loop parser in _import_php would mishandle group use and function/const imports (the current code mishandles groups entirely).
  2. Stay on the per-file-payload + persisted-marker channel; do not add metadata.namespace to PHP nodes. Matches the PHP interface refusal does not survive incremental rebuilds, minting a wrong edge (user stories 10 + 19) #11/PHP: enum- and trait-typed receivers bypass the interface refusal and bind an unrelated same-short-named class #12/PHP: EXTRACTED corroboration matches the file path only, ignoring the declared namespace #14 precedent already wired through watch/cli/context-injection, and avoids changing the node shape every PHP pass sees. Flagged alternative: true C# parity via metadata.namespace would benefit every future PHP pass but has a much wider blast radius — revisit if a third consumer of PHP namespace facts appears.
  3. Incremental-parity contract: refusal may land alone; positive binding must ship with the carry-over marker. The use map belongs to the calling file (always freshly dispatched), so the decisive refusal (PHP use-import map 3/5: PhpNameResolver decisive refusal (closes #16) #21) is correct on both full and incremental runs without new markers. The positive alias binding (PHP use-import map 4/5: positive alias binding via namespace index #22) needs the declared FQNs of unchanged defining files, so PHP use-import map 5/5: incremental-rebuild parity marker for declared FQNs #23 (the marker) must land in the same release as PHP use-import map 4/5: positive alias binding via namespace index #22 — otherwise incremental rebuilds silently lose the edges PHP use-import map 4/5: positive alias binding via namespace index #22 adds, the exact PHP interface refusal does not survive incremental rebuilds, minting a wrong edge (user stories 10 + 19) #11-parity failure mode.

Convention note: keep the resolution.py casefold convention for names; PHP namespace segments are case-insensitive.

Tickets

  1. Capture — enrich PHP imports edges with target_fqn/alias/use_kind metadata via the shared parser (mirror _import_csharp). No behavior change; metadata-shape tests only. Independently landable.
  2. Qualified receiver types — thread the written qualified name alongside the short name for property/param/promoted-param/local type positions (engine.py:671-675, :717-740, :761-885; today only inline-new keeps receiver_qualified, engine.py:4903). On its own this lets a fully-qualified annotation resolve or refuse correctly. Widest-blast-radius ticket: it changes the receiver-type table's value shape.
  3. PhpNameResolver decisive refusal — closes PHP: use-alias pointing outside the corpus + one same-short-named in-corpus class mints a wrong INFERRED edge (design risk 6, measured) #16. New resolver mirroring csharp.py:174-296 returning (nid, decisive), consulted in _resolve_php_member_calls (extract.py:3247-3260) in front of the type_def_nids fallback, exactly like the C# call site (extract.py:2870-2885). Strictly subtractive: an alias-claimed short name that doesn't resolve in-corpus refuses instead of falling back. Depends on 1 (+2 for FQ-annotation coverage).
  4. Positive alias binding — use the alias to bind use App\Services\X; to the right X among several same-short-named in-corpus classes, via a (namespace, name) index sourced from the php_class_fqns payload (engine.py:5285-5290). Additive-only recall win. Depends on 3.
  5. Incremental parity — persist the declared-FQN marker for unchanged defining files and wire the four established carry-over sites; mirrored incremental tests per the test_php_member_calls.py:1544-1878 pattern (_full_then_incremental). Must ship with 4.

Risk profiles: 3 can only delete edges; 4 can only add or re-point them. Review them separately.

Risks / traps (from triage)

  • Group use and aliased group use are already right in resolution.py:2508-2517 — reuse beats rewrite.
  • use function / use const must not enter the class-name map (resolution.py:2479-2480).
  • Trait use inside a class body is a different AST node (use_declaration); _PHP_CONFIG.import_types is frozenset({"namespace_use_clause"}) (extract.py:926) — do not widen to namespace_use_declaration or trait mixins become imports.
  • Multi-namespace files: two existing policies disagree (resolution.py:2541-2542 bails; engine.py:183-237 is per-declaration). Pick per-declaration and note it.
  • _resolve_php_type_references runs at extract.py:5594-5606, member-call resolvers at :6030 — shared state must be valid at both points or built twice.
  • No test in tests/test_php_member_calls.py covers this scenario today (grep Vendor returns nothing); the only repro lives in /tmp — the fixture above is the canonical copy.

References

Activity

  1. added
    ready-for-human
    on Aug 5, 2026
  2. filipechagas commented on Aug 6, 2026

    @filipechagas
    CollaboratorAuthor

    All five tickets have landed on v8, closing the spec:

    1. PHP use-import map 1/5: capture use FQN/alias metadata on imports edges #19 capture — use FQN/alias/kind metadata on imports edges (PR feat(php): capture use FQN/alias/kind metadata on imports edges (#19) #29)
    2. PHP use-import map 2/5: keep written qualified receiver types (stop flattening FQNs) #20 qualified receiver types — written qualified names kept (PR feat(extract): keep written qualified PHP receiver types (#20) #31)
    3. PHP use-import map 3/5: PhpNameResolver decisive refusal (closes #16) #21 decisive refusal — PhpNameResolver, closed PHP: use-alias pointing outside the corpus + one same-short-named in-corpus class mints a wrong INFERRED edge (design risk 6, measured) #16 (PR fix(php): refuse a member call whose receiver type the file claims (#21, closes #16) #32)
    4. PHP use-import map 4/5: positive alias binding via namespace index #22 positive alias binding — declared-FQN index selects among namesakes and follows renaming aliases (PR feat(php): positive alias binding via declared FQNs + incremental parity marker (#22, #23) #35)
    5. PHP use-import map 5/5: incremental-rebuild parity marker for declared FQNs #23 incremental parity — persisted _php_class_fqns marker through the four carry-over sites (PR feat(php): positive alias binding via declared FQNs + incremental parity marker (#22, #23) #35)

    #22 and #23 shipped together in one release per design decision 3. Verification for the final pair (PR #35): full suite 4124 green on merged v8; differential extract vs v8@cec9cdd over a 25-file shape corpus — 6 edges added (all App\Alpha\X::send, INFERRED 0.8), 0 deleted, 0 re-pointed; end-to-end graphify extract → touch callers → graphify update reproduces all edges with the defining files undispatched, and a pre-marker graph fails closed.

    Upstreaming status: Graphify-Labs#2492 (base feature) and Graphify-Labs#2502 (#19) are open upstream; #20–#23 stack on them and go upstream once those merge.

  3. filipechagas commented on Aug 6, 2026

    @filipechagas
    CollaboratorAuthor

    Spec delivered in full on v8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ready-for-human

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions