LukeXF LukeXF - 11 months ago 94
PHP Question

PhpStorm IDE with composer not recognising package classes

I've taken a look at this question, but unfortuently there is no answer.

I'm using PhpStorm and I am building a project with a composer project loaded. I'm trying to make it so that PhpStorm recognises my classes/functions when I ctrl+click on them, specifically, for the projects installed via composer.

I have tried changing the

Source Folders
but I still cannot get this to work.

I am using
require __DIR__ . '/vendor/autoload.php';
to load my composer dependancies.

What do I need to do for PhpStorm to recognise my declarations from files loaded through composer?

To support my question, here is some pictures:

My test.php file: (notice how it can't recognise my class):

enter image description here

A project added via composer and installed using autoloader:

enter image description here

My composer.json file:

enter image description here

Answer Source

Well ... technically that package has no Braintree_Subscription class defined (I mean -- "proper" definition). Your 2nd screenshot shows definition of Subscription class from \Braintree namespace.

Here is that file:

Notice the very last line -- that "undefined" class is mentioned there when calling class_alias() function:

class_alias('Braintree\Subscription', 'Braintree_Subscription');

It looks like your PhpStorm does not understand such class definition.

Here is the ticket from PhpStorm's Issue Tracker: WI-11936 -- accordingly to the ticket it has been fixed on 3rd of November 2016 (2 days ago, basically). The fix (class alias support) will be available in next major version, which is 2016.3.

You may wait till it will be officially released (approximately end of the month) or try next EAP or RC build (watch their blog or twitter for announcements).

Right now (for current and past stable versions: 2016.2 and older) the solution is to use "real" class name (\Braintree\Subscription) instead of alias (Braintree_Subscription) in your code.

But in any case: using real class in your own fresh code name is the most correct way -- leave class aliases for situation where some legacy/compatibility support is required and other edge cases.

P.S. This has nothing to do with Composer -- nothing at all.

It's just the code examples -- for whatever reason (possibly legacy/compatibility reasons .. or easier for new to PHP users, do not know) they are using class aliases instead of real class names and PhpStorm does not understand such aliases.