-
-
Notifications
You must be signed in to change notification settings - Fork 11.4k
feat(extract): support PHP closures (#3409) #3461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nikhilsaxena04
wants to merge
4
commits into
Graphify-Labs:v8
Choose a base branch
from
nikhilsaxena04:fix-php-closures
base: v8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+164
−3
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c8c3af1
feat(extract): support PHP closures (#3409)
nikhilsaxena04 5400b9c
fix(extract): stable closure naming for PHP (#3409)
nikhilsaxena04 38861aa
fix(extract): robust PHP nested routing closure extraction (#3409)
nikhilsaxena04 e845f15
refactor(extract): hoist PHP route helper to module level (#3409)
nikhilsaxena04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| from pathlib import Path | ||
| from graphify.extract import extract_php | ||
| import pytest | ||
|
|
||
|
|
||
| def test_php_route_closure_gets_semantic_name(tmp_path): | ||
| """A closure passed to a routing method gets a 'VERB /path' label.""" | ||
| src = b"""<?php | ||
| $app->get('/api/users', function() { | ||
| return []; | ||
| }); | ||
| $app->post('/api/users', function() { | ||
| return 'created'; | ||
| }); | ||
| """ | ||
| php_file = tmp_path / "routes.php" | ||
| php_file.write_bytes(src) | ||
|
|
||
| res = extract_php(php_file) | ||
| if res.get("error"): | ||
| pytest.skip(res["error"]) | ||
|
|
||
| labels = {n["label"] for n in res["nodes"]} | ||
|
|
||
| assert "GET /api/users()" in labels, "Expected route closure to have semantic label 'GET /api/users()'" | ||
| assert "POST /api/users()" in labels, "Expected route closure to have semantic label 'POST /api/users()'" | ||
|
|
||
|
|
||
| def test_php_generic_closure_gets_ordinal_name(tmp_path): | ||
| """Non-routing closures get stable ordinal names {closure#N}.""" | ||
| src = b"""<?php | ||
| $fn1 = fn($x) => $x + 1; | ||
| $fn2 = function() { return 'hello'; }; | ||
| """ | ||
| php_file = tmp_path / "closures.php" | ||
| php_file.write_bytes(src) | ||
|
|
||
| res = extract_php(php_file) | ||
| if res.get("error"): | ||
| pytest.skip(res["error"]) | ||
|
|
||
| labels = {n["label"] for n in res["nodes"]} | ||
|
|
||
| # Ordinals, not line numbers | ||
| assert "{closure#1}()" in labels, "Expected first generic closure to be '{closure#1}()'" | ||
| assert "{closure#2}()" in labels, "Expected second generic closure to be '{closure#2}()'" | ||
| # Ensure no old line-based names leak through | ||
| assert not any("closure@" in l for l in labels), "Line-based closure names should not appear" | ||
|
|
||
|
|
||
| def test_php_nested_route_closure_composes_prefix(tmp_path): | ||
| """A closure passed to a routing method inside a group() composes the path.""" | ||
| src = b"""<?php | ||
| $app->group('/api/v1', function ($group) { | ||
| $group->get('/users/{id}', function ($req, $res) { return 1; }); | ||
| }); | ||
| """ | ||
| php_file = tmp_path / "nested_routes.php" | ||
| php_file.write_bytes(src) | ||
|
|
||
| res = extract_php(php_file) | ||
| if res.get("error"): | ||
| pytest.skip(res["error"]) | ||
|
|
||
| labels = {n["label"] for n in res["nodes"]} | ||
| assert "GET /api/v1/users/{id}()" in labels, "Expected nested route closure to compose prefix" | ||
| assert "{closure#1}()" in labels, "Expected outer group closure to fallback to ordinal" | ||
|
|
||
|
|
||
| def test_php_cache_get_avoids_route_false_positive(tmp_path): | ||
| """A get() call without a '/' path is treated as a generic closure, not a route.""" | ||
| src = b"""<?php | ||
| $value = $cache->get('user:42', function () { return 2; }); | ||
| """ | ||
| php_file = tmp_path / "cache.php" | ||
| php_file.write_bytes(src) | ||
|
|
||
| res = extract_php(php_file) | ||
| if res.get("error"): | ||
| pytest.skip(res["error"]) | ||
|
|
||
| labels = {n["label"] for n in res["nodes"]} | ||
| assert "{closure#1}()" in labels, "Expected non-routing get() to fallback to ordinal" | ||
| assert not any(l.startswith("GET ") for l in labels), "Expected no route label for cache method" | ||
|
|
||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_extract_generic()fans out to 26 callees (efferent coupling); 18 callers depend on it (afferent coupling).
Grounded coupling-delta finding (deterministic), not an LLM guess.