2

I develop a PHP project in Eclipse. Before updating to last version (2020-06), I used to had no warnings... Since I made this update, it is spamming me with two warnings.

First is about class files, like MyClass defined in MyClass.inc.php : Eclipse says me I must name my file "MyClass.php" or name my class MyClass.inc ... -__________- But I want to continue naming them MyClass.inc.php !!!

Second is about namespaces... I don't use them, and Eclipse says me that << The declared namespace "" does not match the expected namespace "path\folder" >> (path\folder is an exemple for this post).

I use PHP 7.4... I tried filters, but it don't work correctly (may be my bad), and I do not find how to disable this warnings specificaly.

Thanks for helping, I hope some update will fix it it if it's a bug T_T

steffen
  • 16,138
  • 4
  • 42
  • 81
  • Bugs should be reported to https://github.com/eclipse/pdt/issues . If a bug report isn't filed, it can't get fixed. – nitind Jun 21 '20 at 14:40
  • Also, where is it spamming you? – nitind Jun 21 '20 at 14:50
  • These warnings are shown in the « problems » box, the same place where are shown compile errors for others languages. And I don’t know if it’s a bug, or if it’s a change. – Damien Verlynde Jun 22 '20 at 18:35

2 Answers2

7

The new namespace validation rule, although valid, is cumbersome. I guess it's nothing unusual or wrong to have namespaces which don't exactly match the directory structure. I am working on a mezzio-based application, where this is the usual case, since the framework uses composer for autoloading.

After the upgrade there is no file in my project where Eclipse wouldn't warn that, eg.: The declared namespace "App\Middleware" does not match the expected namespace "project\src\App\src\Middleware".

This warning states the truth but by any means this case should be a reason for a warning in my opinion...

EDIT: There seems to be an option which allows to change the reported level or to mute the "problem" completely:

Preferences->PHP->Validation->Error/Warnings: Unexpected namespace name

eremem
  • 86
  • 2
  • Thanks ! It fixed the problems, by setting them on « ignore » instead of « warning ». I don’t know I didn’t see it before... – Damien Verlynde Jun 22 '20 at 18:42
  • 3
    Ignoring will hide all these messages, but I'ld prefer to declare the list of valid "root" directories. I haven't found that method yet. – le_top Dec 17 '20 at 09:18
  • 2
    It seems to me that Eclipse PDT really likes to assume that project root == namespace root. I would like this to be configurable, too, but for now we have to just ignore this sanity check because it really doesn't work as-is. – Mikko Rantalainen Aug 16 '21 at 09:43
3

To configure custom paths for namespaces in Eclipse:

  1. Install "PHP Development Tools (PDT) Composer Support"
  2. Right click on the project → Configure → Add Composer Support
  3. A Composer configuration dialog should open
  4. In the "Autoloading" tab, you can assign namespaces to paths (relative to project root)

That would create a composer.json in the root of the PHP project with the following contents:

{
    "name" : "my project",
    "autoload" : {
        "psr-4" : {
            "some\\namespace" : "src/some/namespace"
        }
    }
}

You can define multiple mappings from namespaces to directories. See composer documentation for more information.

For your other problem, I think I would give in and move all files from .inc.php to .php. You'll probably have less problems in the future if you do so.

steffen
  • 16,138
  • 4
  • 42
  • 81
  • This doesn't work well for WordPress plugins. Upvoted since it's probably true, and it's not your fault. Just wish Eclipse would make it directory based. (Ex. Look for a composer.json file in each directory at build time.) – Rick Mac Gillis Sep 22 '22 at 21:08