[BUG]: use statement before namespace declaration #1121

Open
opened 2024-05-03 10:30:47 +00:00 by Polmock · 3 comments

What Happened?

In com_/src/Controller/DisplayController.php, the first lines of the file (after the initial comment) are the following:

use JCB\Joomla\Utilities\StringHelper;
use JCB\Joomla\Utilities\ArrayHelper;

namespace JCB\Component\<component_name>\Site\Controller;

use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Router\Route;
use Joomla\Utilities\ArrayHelper;
use Joomla\CMS\Language\Text;

as you can see, we have 2 issues:

  1. the namspace is not the first statament
  2. ArrayHelper is included twice

Changing the statement order and removing the second use of ArrayHelper, fixes the issues.

Similar issue in com_/src/Helper/RouteHelper.php:

use JCB\Joomla\Utilities\ArrayHelper;

namespace JCB\Component\<component_name>\Site\Helper;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Categories\CategoryNode;
use Joomla\CMS\Categories\Categories;

Putting as first instruction the namespace declaration fixes the issue.

Steps to reproduce the Bug

Compile the component

Which Joomla version are you compiling in?

3.10.12

Which PHP version are you compiling in?

8.2.2

Which Joomla versions are you targeting?

4.4.4

Which PHP version are you targeting?

8.2.4

Which Web server is JCB running on?

Apache 2.4

Which Relational Database is JCB running on?

MySql 8.0

Which OS is JCB running on?

Windows 11

Which JCB version are you using?

3.2.1-rc5

Where in JCB did this issue occur?

Compilation of a component

On which browsers did you encounter the issue?

Firefox, Chrome

Additional Comments

Maybe related to #1117? Same context

### What Happened? In com_<component>/src/Controller/DisplayController.php, the first lines of the file (after the initial comment) are the following: ``` use JCB\Joomla\Utilities\StringHelper; use JCB\Joomla\Utilities\ArrayHelper; namespace JCB\Component\<component_name>\Site\Controller; use Joomla\CMS\Factory; use Joomla\CMS\MVC\Controller\BaseController; use Joomla\CMS\Router\Route; use Joomla\Utilities\ArrayHelper; use Joomla\CMS\Language\Text; ``` as you can see, we have 2 issues: 1) the namspace is not the first statament 2) ArrayHelper is included twice Changing the statement order and removing the second use of ArrayHelper, fixes the issues. Similar issue in com_<component>/src/Helper/RouteHelper.php: ``` use JCB\Joomla\Utilities\ArrayHelper; namespace JCB\Component\<component_name>\Site\Helper; use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; use Joomla\CMS\Language\Multilanguage; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Categories\CategoryNode; use Joomla\CMS\Categories\Categories; ``` Putting as first instruction the namespace declaration fixes the issue. ### Steps to reproduce the Bug Compile the component ### Which Joomla version are you compiling in? 3.10.12 ### Which PHP version are you compiling in? 8.2.2 ### Which Joomla versions are you targeting? 4.4.4 ### Which PHP version are you targeting? 8.2.4 ### Which Web server is JCB running on? Apache 2.4 ### Which Relational Database is JCB running on? MySql 8.0 ### Which OS is JCB running on? Windows 11 ### Which JCB version are you using? 3.2.1-rc5 ### Where in JCB did this issue occur? Compilation of a component ### On which browsers did you encounter the issue? Firefox, Chrome ### Additional Comments Maybe related to #1117? Same context
Polmock added the
Bug
label 2024-05-03 10:30:47 +00:00
Owner

Can you please run Joomla 3 on PHP 8.0 and only use Firefox to run compilation of your components.

There are known issues with Joomla 3 in higher versions of PHP, specially related to the File and Folder classes across multiple OS.

We have a class that search for existing use statements and then try to add the new statements to them. So if it can't find any it looks for the defined('_JEXEC') or die('Restricted access'); and then adds the statements there. This is what I see is going wrong here... why I am not sure.

Here is the Injector->addUseStatements method that is doing this. You will see the fail-save is on line 563 which is causing this issue... we can improve this, but my real question is why is it not able to detect the other use statements correctly in the first place. Since this works on all our systems (we only run on Linux) I am wondering if this is OS related. I would be very grateful if you can help us debug this some more...

The class extracting the use statements is our own little parser. We opt for our own small parser for this task since all the other parsers are so large and really over kill. It could be that since I am looking for a class declaration we are missing it.

Let me know what you find.

Can you please run Joomla 3 on PHP 8.0 and only use Firefox to run compilation of your components. There are known issues with Joomla 3 in higher versions of PHP, specially related to the File and Folder classes across multiple OS. We have a class that search for existing `use` statements and then try to add the new statements to them. So if it can't find any it looks for the `defined('_JEXEC') or die('Restricted access');` and then adds the statements there. This is what I see is going wrong here... why I am not sure. Here is the Injector->[addUseStatements](https://git.vdm.dev/joomla/Component-Builder/src/commit/6e0e8ae860893f2db053c4be8e259f0820ed0617/libraries/vendor_jcb/VDM.Joomla/src/Componentbuilder/Compiler/Power/Injector.php#L500) method that is doing this. You will see the fail-save is on [line 563](https://git.vdm.dev/joomla/Component-Builder/src/commit/6e0e8ae860893f2db053c4be8e259f0820ed0617/libraries/vendor_jcb/VDM.Joomla/src/Componentbuilder/Compiler/Power/Injector.php#L563) which is causing this issue... we can improve this, but my real question is why is it not able to [detect the other use statements](https://git.vdm.dev/joomla/Component-Builder/src/commit/6e0e8ae860893f2db053c4be8e259f0820ed0617/libraries/vendor_jcb/VDM.Joomla/src/Componentbuilder/Compiler/Power/Injector.php#L148) correctly in the first place. Since this works on all our systems (we only run on Linux) I am wondering if this is OS related. I would be very grateful if you can help us debug this some more... The [class](https://git.vdm.dev/joomla/Component-Builder/src/commit/6e0e8ae860893f2db053c4be8e259f0820ed0617/libraries/vendor_jcb/VDM.Joomla/src/Componentbuilder/Compiler/Power/Parser.php#L22) extracting the [use statements](https://git.vdm.dev/joomla/Component-Builder/src/commit/6e0e8ae860893f2db053c4be8e259f0820ed0617/libraries/vendor_jcb/VDM.Joomla/src/Componentbuilder/Compiler/Power/Parser.php#L128) is our own little parser. We opt for our own small parser for this task since all the other parsers are so large and really over kill. It could be that since I am looking for a [class declaration](https://git.vdm.dev/joomla/Component-Builder/src/commit/6e0e8ae860893f2db053c4be8e259f0820ed0617/libraries/vendor_jcb/VDM.Joomla/src/Componentbuilder/Compiler/Power/Parser.php#L131) we are missing it. Let me know what you find.
Author

I've updated to 3.2.1-rc6 version.
And manually replaced the compiler folder just to be sured that it's up to date.
I've downgraded php to 8.0.27 version (of course, I always use Firefox to compile).

Here is the generated file

image

I've updated to 3.2.1-rc6 version. And manually replaced the compiler folder just to be sured that it's up to date. I've downgraded php to 8.0.27 version (of course, I always use Firefox to compile). Here is the generated file ![image](/attachments/e72c377f-392e-4257-b1b9-3bd551ad8f4b)
103 KiB
Author

And the RouterHelper.php file

image

And the RouterHelper.php file ![image](/attachments/0b3a43c5-32f9-407d-86ec-cfd55d1e0be5)
105 KiB
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: joomla/Component-Builder#1121
No description provided.