如何使带有 PDT 的 Eclipse 与已注册的 Joomla 名称空间别名一起工作?

How to make Eclipse with PDT work with registered Joomla namespace aliases?

我想做的是开始在 Joomla 下工作!安装了 PHP 开发工具的 Eclipse PHP IDE 中的项目。我使用 Eclipse Oxygen 进行 PHP 开发并使用 Joomla 3.8.2 项目。

创建项目并导入代码后,我收到有关无法将某些 class 解析为类型的验证错误。例如:

$par = JComponentHelper::getParams('com_somecomponent');

这给了我一个验证错误:

JComponentHelper cannot be resolved to a type.

我认为这是因为 JComponentHelper 是已注册的 Joomla!别名,真实姓名是 \Joomla\CMS\Component\ComponentHelper。我如何向 Eclipse 提供此信息才能正确解析名称空间?

Joomla 项目一直在逐步将框架的 classes 迁移出全局命名空间,但提供别名以简化旧项目和扩展的转换。正如我们所知,Eclipse 无法推断有关这些别名的信息,因为 Joomla 使用 PHP 的 class_alias() function 动态生成它们。

从版本 3.8.0 开始,Joomla 提供了一个 stub generator 来分析框架中的 classes 以创建一个文件,IDE 可以轻松地从中加载缺失的信息:

As Joomla transitions its core classes from residing in the global PHP namespace to using namespaced PHP classes, it will be a common occurrence for developers to work in an environment where their code is still using the old class names which may not exist in newer Joomla releases except for in PHP's autoloader as a class alias. This script therefore allows developers to generate a mapping file they can use in their local environment which will create "real" classes for the aliased class names and allow things like IDE auto completion to work normally.

我们可以通过位于 build/ 目录中的 运行 stubGenerator.php 实用程序生成此文件命令提示符或终端:

php build/stubGenerator.php

...在项目的根目录中创建一个 stubs.php 文件。然后,Eclipse 应该显示别名的 Content Assist 信息。此文件也适用于其他 IDE,如 NetBeans 和 PHPStorm。一个小警告:

Note that this file will raise some IDE errors as it will generate stub classes extending a final class (something not allowed in PHP). Therefore it is suggested that inspections on this file are disabled.

不幸的是,我们不能从 PDT 的 PHP 验证中排除单个文件,但我们可以从 "Problems" window 中删除错误,如果它们出现应该抑制它们直到我们重新生成存根文件。


虽然这解决了 Eclipse 中的问题,但我们需要考虑即将发布的 Joomla 版本 deprecate many of these aliases for removal, so we want to avoid referencing these when possible as Joomla moves towards Composer and the PSR-4 namespacing convention

而不是直接使用别名:

$par = JComponentHelper::getParams('com_somecomponent');

...考虑导入 class:

use Joomla\CMS\Component\ComponentHelper; 
...
$par = ComponentHelper::getParams('com_somecomponent');