Search code examples
phpeclipsejoomlaeclipse-pdt

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


What I'm trying to do is to start working under a Joomla! project in the Eclipse PHP IDE with PHP Development Tools installed. I use Eclipse Oxygen for PHP Development and work with a Joomla 3.8.2 project.

After creating project and importing the code, I get validation errors about not being able to resolve some class to a type. For example:

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

This gives me a validation error:

JComponentHelper cannot be resolved to a type.

I assume this is due to the fact that JComponentHelper is a registered Joomla! alias and the real name is \Joomla\CMS\Component\ComponentHelper. How can I provide Eclipse with this information to be able to correctly resolve the namespace?


Solution

  • The Joomla project has been gradually migrating the framework's classes out of the global namespace but provides aliases to ease the transition for older projects and extensions. As we know, Eclipse cannot infer information about these aliases because Joomla generates them dynamically using PHP's class_alias() function.

    Since version 3.8.0, Joomla provides a stub generator that analyzes the classes in the framework to create a file that IDEs can easily load the missing information from:

    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.

    We can generate this file by running the stubGenerator.php utility located in the build/ directory from a command prompt or terminal:

    php build/stubGenerator.php
    

    ...which creates a stubs.php file in the project's root directory. Then, Eclipse should display the Content Assist information for the aliases. This file also works for other IDEs like NetBeans and PHPStorm. A minor caveat:

    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.

    Unfortunately, we can't exclude a single file from PDT's PHP validation, but we can delete the errors from the "Problems" window if they appear which should suppress them until we regenerate the stubs file.


    While this solves the problem in Eclipse, we need to consider that upcoming Joomla releases 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.

    Instead of using the alias directly:

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

    ...consider importing the class:

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