0

I just upgraded to Composer 2.0 which has apparently made some modifications to how it parses classes...

composer dump-autoload -o or the alias composer du -o basically dumps a lot of lines in my console with text like:

Class MyProject\SubCategory\CoolClass located in C:/udvikling/MyProjectRoot/src/MyProjectClasses\MyProject\SubCategory\CoolClass.php does not comply with psr-4 autoloading standard. Skipping

After reading Composer psr-4 autoload issue i ensured that all names that contribute to my namespaces and classpaths are capitalized. i.e. use myproject/helperclass is now use MyProject/HelperClass

My C:/udvikling/MyProjectRoot/src/composer.json includes the following

    "autoload": {
        "psr-4": {
            "MyProject\\": "MyProjectClasses/"
        }
    }

I made one go away by adding a leading backslash to the classname:

<?php

namespace \MyProject\Core;

class Timer {
    //...

but... according to php.net

Fully qualified names (i.e. names starting with a backslash) are not allowed in namespace declarations, because such constructs are interpreted as relative namespace expressions.

So this seems to not be the right choice... (my IDE also gave me an angry squiggly line under it...)

Besides going back to another version of composer, how do I fix this?

File structure:

C:\udvikling\MyProjectRoot -- the root of the project
C:\udvikling\MyProjectRoot\src --the php sources
C:\udvikling\MyProjectRoot\src\MyProjectClasses -- my PHP classes
C:\udvikling\MyProjectRoot\src\vendor -- my vendor stuff for composer
C:\udvikling\MyProjectRoot\src\composer.json - my composer config

JoSSte
  • 2,953
  • 6
  • 34
  • 54
  • can you show us your file structure? – Sindhara Dec 16 '20 at 22:44
  • I'm not sure what your file structure is. The formatting looks a bit bad. Is it `C:/udvikling/MyProjectRoot/src/MyProjectClasses` or are there actual underscores? Also where is your `composer.json` file in this structure? – apokryfos Dec 16 '20 at 22:50
  • @apokryfos i hope this makes it clearer – JoSSte Dec 16 '20 at 22:53

1 Answers1

1

I went and did some extra reading.

Apparently one of the goals of psr-4 is to reduce the folder depth.

The “src” and “tests” directories have to include vendor and package directory names. >This is an artifact of PSR-0 compliance.

Many find this structure to be deeper and more repetitive than necessary. This proposal suggests that an additional or superseding PSR would be useful [...]

It’s difficult to implement package-oriented autoloading via an extension or amendment to PSR-0, because PSR-0 does not allow for an intercessory path between any portions of the class name.

So basically, the issue was that inside my MyProjectClasses directory, for the class MyProject\Core\Timer I had it in a similar directory structure: C:\udvikling\MyProjectRoot\src\MyProjectClasses\MyProject\Core\Timer.php.
This had to be altered to that the base package was not included: C:\udvikling\MyProjectRoot\src\MyProjectClasses\Core\Timer.php

JoSSte
  • 2,953
  • 6
  • 34
  • 54