How to get the name of the class owning a method which is called?


to get the class owner of a method in a java custom rule, I do this :

private static class LoopBodyVisitor extends BaseTreeVisitor {
    public void visitMethodInvocation(MethodInvocationTree methodInvocationTree) {
        String className = methodInvocationTree.symbol().owner().name();

How can I get the class owning a method in PHP ? Is it even possible ?
Here is what I have so far :

private static class LoopBodyVisitor extends PHPVisitorCheck {
    public void visitFunctionCall(FunctionCallTree functionCalltree) {
        String className = functionCalltree. (???);

It looks like MethodSymbol exists with an “owner()” method, but I don’t know how to get this symbol inside visitors :

public interface MethodSymbol extends FunctionSymbol {

  Visibility visibility();  
  String name();    
  Trilean isOverriding();    
  Trilean isAbstract();    
  ClassSymbol owner();

source : sonar-php/ at master · SonarSource/sonar-php · GitHub

Hello @Tristan107,

yes, you are correct. MethodSymbol would be the class to use to access a method owner. Unfortunately, the symbols and associated symbol table you pointed out are not currently in the front-end API and therefore not accessible to custom rules. However, your example is a good use case that could lead us to make the symbol table available via API. I will discuss this with the team.


Ok, thanks.