Skip to content

PHP: resolve instance-method calls edges from typed receivers (upstream Graphify-Labs#1682) #1

Closed

Description

@filipechagas

Upstream issue: Graphify-Labs#1682 (confirmed present at v8 HEAD, 0.9.33, via a live repro build).

Problem Statement

When a user points an AI coding assistant at a graphify graph of a PHP codebase and asks "what depends on this service / what breaks if I change it", the answer is silently wrong. calls edges exist only for static calls (ClassName::method()). Instance-method calls — constructor-injected dependencies ($this->service->method()), typed locals, and inline instantiation ((new Service())->method()) — produce no calls edge at all, even when the callee class is statically knowable. Constructor injection is the dominant call pattern in modern Laravel code, so for most service classes the inbound-calls answer is empty. Worse, the graph looks complete (rich inherits/method/contains edges), so the user has no signal that the most useful edge type for impact analysis is missing.

Solution

The PHP extractor learns to read the receiver of an instance call, and a new PHP language resolver turns receiver + declared type into a calls edge — but only when the answer is unambiguous. The resolver follows the existing Java member-call resolver's philosophy: resolve when the receiver has exactly one known concrete type, that type has exactly one definition in the corpus, and that definition has exactly one matching method; otherwise refuse and emit nothing. Type-inferred edges carry the INFERRED confidence label; inline-new edges, where the source names the exact class, carry EXTRACTED. Static-call behavior is unchanged. Remaining limitations are stated in the changelog so the edge set no longer looks complete when it is not.

User Stories

  1. As an AI assistant user, I want get_neighbors("LeadHunterService") to return the controllers that call it through constructor-injected properties, so that impact analysis matches reality.
  2. As an AI assistant user, I want (new \App\Services\MixedPaymentService())->resolve() call sites to appear as inbound calls edges on MixedPaymentService, so that inline-instantiated services are not invisible.
  3. As an AI assistant user, I want calls through typed locals ($svc = new LeadHunterService(); $svc->search()) to produce calls edges, so that local-variable dispatch is tracked.
  4. As an AI assistant user, I want calls through natively typed properties (private Mailer $mailer) to produce calls edges, so that property-injected dependencies are tracked.
  5. As an AI assistant user, I want calls through typed method parameters (function handle(LeadHunterService $svc)) to produce calls edges, so that method-injected dependencies are tracked.
  6. As an AI assistant user, I want nullable-typed receivers (?Mailer) to resolve to the underlying class, so that the common nullable-dependency idiom is not a blind spot.
  7. As an AI assistant user, I want nullsafe calls ($this->mailer?->send()) to resolve the same way as regular member calls, so that modern PHP 8 syntax is covered.
  8. As an AI assistant user, I want static Class::method() edges to keep working exactly as today, so that the fix does not regress the one pattern that already works.
  9. As an AI assistant user, I want same-file $this->method() calls to keep producing edges as today, so that existing intra-class call graphs are preserved.
  10. As a graph consumer, I want no calls edge when the receiver's type is an interface, so that the graph never guesses which implementation is bound.
  11. As a graph consumer, I want no calls edge when the receiver is untyped, union-typed, or intersection-typed, so that common method names (log(), find(), execute()) never produce false cross-class edges.
  12. As a graph consumer, I want no calls edge when a local variable is reassigned or rebound (including by a closure parameter or a foreach target that shadows it), so that scope confusion never produces a wrong edge.
  13. As a graph consumer, I want no calls edge when two classes in the corpus share the receiver's type name, so that ambiguity is refused rather than resolved by luck.
  14. As a graph consumer, I want no calls edge when the resolved class has no matching method, so that magic-method dispatch (Eloquent) is never guessed.
  15. As a graph consumer, I want chained receivers ($a->b()->c()) left unresolved, so that return-type inference is never faked.
  16. As a graph consumer, I want type-inferred edges labelled INFERRED and inline-new edges labelled EXTRACTED, so that I can weigh edges by how directly the source states them.
  17. As a graph consumer, I want an interface sharing a short name with an unrelated class to block resolution, so that Laravel's Contracts naming convention does not mint edges into the wrong class.
  18. As a user of mixed-language corpora, I want PHP receiver data to never leak into the Swift, Python, or TypeScript member-call resolvers, so that a PHP variable name cannot mint an edge into another language's class.
  19. As a user of graphify update (watch/incremental), I want the new resolution to run on incremental rebuilds too, so that the graph does not drift after edits.
  20. As an upgrading user, I want stale cached extraction data invalidated on release, so that pre-fix caches cannot produce receiver-less results.
  21. As a maintainer, I want the changelog to state exactly which member calls now resolve and which are deliberately refused, so that users can calibrate trust in the edge set.
  22. As a maintainer, I want the new resolver to follow the existing Java resolver's structure and guard set, so that the codebase keeps one recognizable pattern for typed-receiver resolution.

Implementation Decisions

  • Receiver capture (extraction): the shared tree-sitter engine's PHP member-call walk records a receiver key alongside the method name. The mapping, verified by AST probes against the pinned tree-sitter-php grammar (prototype output):

    Source form Receiver key Extra
    $this->m() this
    $this->prop->m() this.prop
    $var->m() var
    (new X())->m(), (new \NS\X())->m() (new) class name, plus the qualified text when the source gives one
    anything else (chains, array access, $$var, anonymous classes) none — behavior unchanged
  • Nullsafe member calls are added to the PHP call node types; they share the regular member-call shape.

  • Type tables: a per-class map from property name to declared type is built from typed property declarations and constructor-promoted parameters — the two sites the engine already parses for references edges. A per-method layer adds natively typed parameters and locals assigned with new. Binding conflicts poison the name (Java convention), and poisoning explicitly covers: reassignment to a non-new value, conflicting new types, closure and arrow-function parameters, foreach targets, and list-destructuring elements. Anonymous-class subtrees are skipped when walking method bodies.

  • Concrete-type policy: a type contributes to the table only when it is a single named class type. Nullable unwraps to its inner type. Union, intersection, primitive, self, static, and parent types are refused — including when they appear as the class of a new expression. Docblock types are ignored.

  • Raw-call stamping: the resolved receiver type and a PHP language tag are stamped on the raw call record (the C#/Java pattern). The existing in-file bare-name match is skipped only when a receiver type was actually stamped — untyped receivers keep today's same-file edges.

  • Resolution pass: a new PHP member-call resolver, registered through the language-resolver registry (so it runs in both full and incremental builds automatically), structured as a clone of the Java member-call resolver. Receiver this resolves against the caller's enclosing class. Otherwise the pass requires a stamped receiver type, exactly one definition of that type in the corpus, and exactly one matching method on it — any failed condition means no edge, with no bare-name fallback. Name matching is case-insensitive (PHP semantics, consistent with the shared pass).

  • Interface refusal: PHP interface declarations do not mint class definition nodes today, which would let a lone same-short-named class silently satisfy the single-definition guard. The extractor therefore pre-scans interface names (following the existing C# interface pre-scan pattern), and the resolver refuses any receiver type whose name matches a known interface.

  • Confidence: type-inferred edges are INFERRED (score 0.8). Inline-new edges are EXTRACTED (score 1.0) only when the source's qualified name corroborates the resolved node; otherwise they are downgraded to INFERRED.

  • Cross-resolver hardening: the Swift, Python, and TypeScript member-call resolvers gain a language-tag skip so that language-tagged raw calls (PHP, and incidentally the pre-existing ObjC leak) can no longer flow through them in mixed corpora. The Ruby resolver needs no change — it already filters by file suffix.

  • Cache and release: the package version is bumped so the version-namespaced AST cache invalidates raw calls that predate the new receiver fields.

  • Transparency: the changelog entry names both what now resolves and what is deliberately refused; the how-it-works doc gains a note in its confidence section.

Testing Decisions

  • Seam: every test goes through the public extract() pipeline entry point — fixture files in a temp directory in, {nodes, edges} out. Tests assert only external behavior: the presence or absence of specific calls edges and their confidence labels. No internal helper (receiver-table builder, concrete-type helper) is tested directly; their behavior is fully observable at the seam.
  • Good-test shape: every positive case includes a decoy class defining an identically named method, and asserts the decoy received no edge — a resolution test that cannot catch mis-binding proves nothing.
  • Prior art: test_java_member_calls.py (structure, decoy corpus, extract() usage), test_csharp_member_calls.py (confidence and negative-assertion idioms), test_php_type_resolution.py (PHP fixture style).
  • New test module covering, as extract()-level cases: the three upstream repro scenarios (constructor-promoted property → INFERRED; inline new → EXTRACTED; static control unchanged); typed local, typed property, typed parameter, nullable, and nullsafe positives; the same-file $this->method() preservation case; and negatives for every refusal guard — untyped, union, reassigned local (both non-new rebind and conflicting new), closure/foreach shadowing, duplicate class names, interface-typed receiver, interface/class short-name collision, unknown method with no fallback edge, and chained receivers.
  • One mixed-corpus test (.php + .py in a single extract() call) proving PHP receiver data cannot mint an edge through the Python resolver.
  • Regression suites to run unchanged: the existing PHP language tests, PHP type-resolution tests, and the C#, Java, TypeScript, and ObjC member-call suites (the ObjC suite guards the incidental leak closure).

Out of Scope

  • Resolving interface-typed receivers to implementations, including via use-import maps or Laravel service-container bound_to edges. This is the documented upgrade path, not this change.
  • Methods provided by traits or inherited from parent classes (the method index does not walk inheritance; Java parity).
  • Docblock-only types (@var), chained-call receivers, and array-element receivers.
  • Bare new X() statements without an immediate method call (unchanged from today).
  • The residual wrong-edge exposure where a use alias points outside the corpus while one unrelated in-corpus class shares the short name — accepted and documented; the Java resolver has the identical exposure today.

Further Notes

  • One known behavior delta: a same-file call through a receiver that is now concretely typed moves from the bare-name EXTRACTED edge to the resolver's INFERRED edge (same endpoints, different label). The existing PHP language test still passes; the PR description must state the delta.
  • Pre-merge checks that remain open from design verification: run the full test suite (the design was verified against source line-by-line, but tests were not executed in the review environment); probe the older tree-sitter-php versions the project accepts, or raise the version floor; one-line AST probes for anonymous classes and self/static in type position; one manual incremental-rebuild test across the version-bump cache boundary.
  • Provenance: this spec was produced from an 11-agent investigation of the upstream issue — 7 investigators (including a live repro at HEAD and tree-sitter AST probes), 1 designer, and 3 adversarial verifiers whose corrections (interface short-name bypass, closure/foreach scope poisoning, defer-only-when-typed, inline-new FQN corroboration, Ruby hardening refuted) are folded into the decisions above.

Activity

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

    @filipechagas
    CollaboratorAuthor

    Submitted upstream as Graphify-Labs#2492 (head lawnstarter:feat/php-member-calls-1682, db7c8f8, 0.9.34). All child tickets #2-#7 closed, plus review-phase fixes #8/#11/#12/#13/#14. Known-open residuals tracked: #9, #10, #15, #16. Leaving this spec open until the upstream PR merges.

  3. added a commit that references this issue on Aug 5, 2026
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-agent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions