[PhpStorm] Adding class 'aliases'

I'm working with a framework where functionality can be overriden by extending a class which does not exist. These 'virtual' classes map to
real, concrete classes in the project's include path and the real class reference is included in a docblock. The non-existent classes all have a known, predictable prefix so I'm happy detecting them from their name. I'm also happy getting the real class from the doc comment given a PhpClass object I can call getDocComment on.

Here's an example:

<?php
namespace SomeNamespace;

/**
* @see \Vendor\Product\RealClass
*/
class MyClass extends ABCD_XYZ {

}


In the example above, the class extends ABCD_XYZ which does not exist (in my example all virtual classes start with ABCD_). As a result, PhpStorm ttreats it as undefined and there is no autocompletion for protected functions, fields etc etc. The real class that "MyClass" is extending is included in the docblock above the class declaration.

You can hack around the problem by writing something like this but I'd prefer to solve this in a plugin. It's important to note that the if (false) is required because the class needs to not exist at runtime.

<?php
namespace SomeNamespace;

if (false) use \Vendor\Product\RealClass as ABCD_XYZ;

/**
* @see \Vendor\Product\RealClass
*/
class MyClass extends ABCD_XYZ {

}


I'm hoping to create a plugin which allows PhpStorm to understand where these class aliases acually point to in order that autocompletion can work. I've tried implementing PhpTypeProvider2 to provide type information however my code is not called for all PsiElement sub-classes, seemingly only function calls. I looked at implementing PsiReferenceContributor but I was concerned it'd break the autocomplete and I wasn't sure how to reference an existing class.

Where should I start with this?

2 comments
Comment actions Permalink

To make the IDE work just as it does with "if (false) use .." hack you need to extend existing resolve code for com.jetbrains.php.lang.psi.elements.ClassReference. A the moment there is no suitable extension point to do this but we're thinking on adding it to php openapi. Can you please file this as a feature request to https://youtrack.jetbrains.com/newIssue?project=WI ?

As to how to find (and use) any existing entity - com.jetbrains.php.PhpIndex is the entry point. In particular "getClassesByFQN" is of interest here.

0
Comment actions Permalink

Thanks for the quick response. I've created a feature request for the openapi subsystem.

0

Please sign in to leave a comment.