Repository navigation
feat(php): type member-call receivers from return types, docblocks, properties and callers - #5697
livingforelvis wants to merge 11 commits into
Conversation
…d call chains Co-authored-by: Prachi Dhir <[email protected]>
…r table Co-authored-by: Prachi Dhir <[email protected]>
…er undeclared returns Co-authored-by: Prachi Dhir <[email protected]>
Co-authored-by: Prachi Dhir <[email protected]>
Co-authored-by: Prachi Dhir <[email protected]>
Co-authored-by: Prachi Dhir <[email protected]>
Co-authored-by: Prachi Dhir <[email protected]>
…method on each Co-authored-by: Prachi Dhir <[email protected]>
…lass its nearest value gives Co-authored-by: Prachi Dhir <[email protected]>
Co-authored-by: Prachi Dhir <[email protected]>
|
Thanks for the pull request, @livingforelvis. A maintainer will review it soon. Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions. A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic. |
There was a problem hiding this comment.
Graphify review 
2 bugs · High impact
The PHP extractor now tracks receiver types through returns, locals, properties, and parameters.
Same-line PHP methods reuse the first method’s receiver data ·
engine.py:1414
Qualified PHP docblock types can bind to the wrong class ·
engine.py:1192
What this review covered
- One review pass used all of its turns for reading code, so it reported from what it had already read.
- A changed file's diff was too long to show in full, so the review may not have seen every changed line in it; the in-depth pass still reviewed every changed file.
- 8 of 8 changed files inspected. This is scope, not a correctness guarantee.
Code graph · 6634 functions depend on this change
- 7 functions became more coupled. Largest:
_extract_generic()(23 callers),origin_type()(3 callers),_resolve_php_member_calls()(2 callers). - Gate, test list and every flagged function: in the Graphify check.
Reviewed 3bc7ea8 · 2 earlier findings not re-checked
…aches no subclass method Co-authored-by: Prachi Dhir <[email protected]>
| for entries in binds.values(): | ||
| entries.sort(key=lambda b: (b[0], b[1])) | ||
| if params or binds or props or returns or news or calls or any(order): | ||
| scopes.setdefault(str(n.start_point[0] + 1), { |
There was a problem hiding this comment.
Same-line PHP methods reuse the first method’s receiver data
When multiple methods begin on the same line, later methods can be resolved using the first method’s parameters and assignments, producing incorrect or missing call edges.
Key PHP scopes by function rather than starting line in _php_function_scopes.
Evidence · checked against the code
Expected: The later method’s receiver type is inferred from that method’s scope.
Actually: The line-keyed scope map retains the first populated scope, which the resolver then uses for the later method’s receiver inference.
- Independent source review: Checked the PHP member-call tests: they cover receiver resolution and method calls, but their methods are on separate lines and they do not exercise same-line scope collisions. The resolver’s scope lookup is keyed by the caller’s line; no per-method key or other same-line disambiguation was found in the inspected path.
- This affects methods whose recorded starting lines are identical; the added tests use methods on separate lines.
Prompt to fix with AI
In graphify/extractors/engine.py, _php_function_scopes (lines 1411-1414):
Problem: Same-line PHP methods reuse the first method’s receiver data
Expected: The later method’s receiver type is inferred from that method’s scope.
Actually: The line-keyed scope map retains the first populated scope, which the resolver then uses for the later method’s receiver inference.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
| if not re.fullmatch(r"\\?[A-Za-z_]\w*(?:\\[A-Za-z_]\w*)*", arm) or arm.lower() in _PHP_DOC_SCALARS: | ||
| return None | ||
| return arm.rsplit("\\", 1)[-1] |
There was a problem hiding this comment.
Qualified PHP docblock types can bind to the wrong class
A receiver documented as \Vendor\Actions can be resolved to an unrelated Actions class in the scanned project.
Preserve qualified names from _php_doc_type during class resolution.
Evidence · checked against the code
- Independent source review: Checked the unique-match resolver and the receiver inference path. Both names are reduced to their final component, and a sole matching project class is accepted. The inspected tests cover unqualified docblock types but do not demonstrate a guard for a qualified type colliding with an unrelated class.
- The report concerns docblock types; namespace resolution for other PHP type sources may have separate existing behavior.
Prompt to fix with AI
In graphify/extractors/engine.py, _php_doc_type (lines 1190-1192):
Problem: Qualified PHP docblock types can bind to the wrong class
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
What does this PR do?
_resolve_php_member_callstypes a->m()receiver from what produced it, step by step; every new edge is INFERRED (0.85)@return; a function with no declared type is typed from itsreturns (eachreturnalone, so a factoryswitchkeeps every arm)if/elsearms that assign related classes keep each class, and the call binds on each one that has the method@param,@varv8finds is lostnewsites (constructors) and method calls (other methods) pass; several callers meet at their nearest common baseprivatemethod reaches only that method, never a same-named subclass methodphp_scopes,php_origin,_php_ret,_php_privateandphp_this_calls;_AST_CACHE_SCHEMA60 -> 61newlocals and$this->prop->m(); happy to rebase onto it or split if that lands firstType of change
Verification & Invariants
Invariant: a receiver binds only when its type evidence names one class, or related classes the code shows (
ifarms, callers); anything else binds nothingInvariant: no
callsedge onv8is dropped; an unknown chain step falls back to the file tableInvariant: unrelated classes that share a method name are never reached (each new test file has same-named decoy classes in other files)
Limitation: interfaces are not in the class lineage, so callers passing an interface and a class that implements it meet at no common base, and that parameter stays untyped
Limitation: override fan-out is bounded by "some non-test function returns the class", not by data flow, so it can list overrides that never run from a given call site
Limitation:
insteadof/astrait rules,__call, and variable method names stay unresolved, as beforeRead the CONTRIBUTING.md guide.
Reproduced the issue and identified the invariant.
Made the smallest fix necessary.
Added a regression test.
Kept the PR description synchronized with the final implementation.
Documented any limitations / unsupported cases explicitly.
How was this tested?
test_a_test_caller_types_no_parameter,test_a_test_caller_does_not_widen_what_production_passes,test_a_private_base_method_reaches_no_overridetest_php_return_type_receivers.py,test_php_property_types.py,test_php_override_dispatch.py,test_php_parameter_types.py,test_php_branch_alternatives.pypyteston the 5 new filesorigin/v8(d249de3)32 failed, 17 passed49 passedRepro from the issue (4 files),
callsedges fromValidator::check():.get_actions_instance()-> Factory.php.is_game_over()-> Actions.php.is_game_over()-> OtherActions.php (decoy)Large legacy PHP codebase (
.incas PHP), whole-graphcallsedges from PHP callers:if/elserule alone, edges vs the previous commit: 41 added, 182 removed; the removed are base-class fan-outs replaced by the classes the callers actually pass (one call site: 66 targets -> 6, the six factories that call it)ci.ymlruns it (-n auto -m "not serial", then-m serial) on Python 3.11 and 3.13: 1 and 2 failures, all intest_the_pool_hands_its_workers_the_overrides/test_extract_honours_max_workers_argument; both pass when run alone on each versionGraphify-specific checklist