PhpStorm does not recognise class aliases

て烟熏妆下的殇ゞ 提交于 2019-12-06 08:33:13

问题


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 in Settings/Directories 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): project/test.php

A project added via composer and installed using autoloader: project/vendor/braintree/braintree_php/lib/Braintree/Subscription.php

My composer.json file: project/composer.json


回答1:


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: https://github.com/braintree/braintree_php/blob/master/lib/Braintree/Subscription.php

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.



来源:https://stackoverflow.com/questions/40441937/phpstorm-does-not-recognise-class-aliases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!