Skip to content

Add LSP Call Hierarchy support - #3067

Open
klobastov wants to merge 10 commits into
phpactor:masterfrom
klobastov:feat/lsp-call-hierarchy
Open

klobastov wants to merge 10 commits into
phpactor:masterfrom
klobastov:feat/lsp-call-hierarchy

Conversation

@klobastov

Copy link
Copy Markdown

Summary

Implements the LSP Call Hierarchy capability in phpactor's language server,
so clients (e.g. Qwen Code) can use textDocument/prepareCallHierarchy,
textDocument/callHierarchy/incomingCalls and
textDocument/callHierarchy/outgoingCalls.

Changes

  • New LanguageServerCallHierarchy extension registering a
    CallHierarchyHandler as an LSP method handler.
  • CallHierarchyHandler registers the callHierarchyProvider capability and
    serves the three methods using Reflector, ReferenceFinder,
    AstProvider and DefinitionLocator.
  • OutgoingCallsWalker collects method/function calls inside the enclosing
    function body.
  • Unit tests for the handler.
  • Extension registered in lib/Phpactor.php.

Dependency

This PR depends on phpactor/language-server-protocol#30 (adds
CallHierarchyParams and the three request interfaces). The
phpactor/language-server-protocol dependency must be bumped to the tagged
release containing those classes before this PR can pass composer integrate
(phpstan/phpunit).

Implements the three LSP call-hierarchy methods (prepareCallHierarchy,
incomingCalls, outgoingCalls) via a new LanguageServerCallHierarchy
extension:

- CallHierarchyHandler: registers callHierarchyProvider capability and
  serves the three methods using Reflector, ReferenceFinder,
  AstProvider and DefinitionLocator.
- OutgoingCallsWalker: collects method/function calls within the
  enclosing function body.
- Unit tests for the handler.

Depends on phpactor/language-server-protocol PR phpactor#30 (CallHierarchyParams
and the three request interfaces).
Switch the Call Hierarchy handler and tests from the hand-written PrepareCallHierarchyRequest / CallHierarchyParams to the generated CallHierarchyPrepareRequest / CallHierarchyIncomingCallsParams / CallHierarchyOutgoingCallsParams, matching the regenerated language-server-protocol.
The import became unused after switching to ChainReferenceFinder.
PHP-CS-Fixer flagged it; removing it clears the CI CS-Fixer failure.
Comment thread phpstan-baseline.neon Resolved
- Bump phpactor/language-server-protocol to ^3.17.6 (Call Hierarchy request interfaces)
- Introduce Call value object and use it in OutgoingCallsWalker/CallHierarchyHandler
- Use typed $container->expect in LanguageServerCallHierarchyExtension
- Add MethodCallReferenceFinder unit test for cross-document method call references
- Fix CompletionWorseExtensionTest assertion (assert 0 completions for minimal input)
- Remove stale phpstan baseline entries for Call Hierarchy extension
Address review comment asking to test via LanguageServerTester and
avoid mocks, following the GotoImplementationHandlerTest pattern.
Replace Prophecy mocks with a real Reflector (ReflectorBuilder),
real reference finders (TolerantVariableReferenceFinder +
MethodCallReferenceFinder) and a real WorseReflectionDefinitionLocator.

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
try {
$lspDoc = $this->workspace->get($uri);
} catch (UnknownDocument) {
$range = RangeConverter::toLspRange($location->range(), '');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason to catch this exception? converting a range with an empty string will resul in Range(0,0,0,0) unconditionally.

try {
$targetText = $this->workspace->get($targetUri)->text;
} catch (UnknownDocument) {
$range = RangeConverter::toLspRange(ByteOffsetRange::fromInts($offset, $offset), $text);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly this will result in the wrong range which wouldn't be useful. Unless there is a reason I would let the exception bubble up (alternatively returning null and skipping)

return new CallHierarchyItem($name, $kind === 'method' ? SymbolKind::METHOD : SymbolKind::FUNCTION, $targetUri, $range, $range);
} catch (CouldNotLocateDefinition) {
$range = RangeConverter::toLspRange(ByteOffsetRange::fromInts($offset, $offset), $text);
return new CallHierarchyItem($name, $kind === 'method' ? SymbolKind::METHOD : SymbolKind::FUNCTION, $uri, $range, $range);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


/**
* Finds references to a method or function by searching all open documents
* in the workspace for matching call expressions.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a specific reason to only search opened documents vs. the entire workspace? is that what other implementations do?

Phpactor reference finding can be very slow accross the entire workspace so it's not necessarily a bad thing to do this but not sure it's what would be expected.

return [];
}

$walker = new OutgoingCallsWalker($bodyRange);

@dantleech dantleech Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using the Reflector to walk the AST will perform static analysis which can be very slow. It seems this just needs to collect method calls without resolving the types they are being called on - in which case no analysis of the AST is required.

You can simply call:

foreach ($node->getDescendantNodes() as $descendantNode) {
    if (!$descendantNode instance of MemberAcessNode) {
        continue;
    }
    
    // ...
}

@dantleech

dantleech commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

looks generally good, some questions and suggestions.

we have a separate issue that the previosuly tagged version of the LSP protocol causes issues - so Phpactor was pinned to the one before that. I'll look at upgrading the protocol in master

@dantleech

Copy link
Copy Markdown
Collaborator

ok, this PR can now be rebased on develop (the protocol is already updated there).

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants