Using Composer 2.0 with PSR4

Vitaliy Dotsenko
Legacybeta
Published in
3 min readSep 10, 2020
Photo by Carlos Deleon on Unsplash

If you check the composer website you’ll see there is a new major version of composer coming out soon. Version Two (v2) has some features that you should be aware of, but the main one being deprecation of support for incorrect namespaces. Before it is released we should prepare our composer.json to prevent incompatibility errors. In the following article we will provide examples of the errors and show you how to fix them.

You might have noticed recently when using normal composer (v1.10.x) commands composer install or composer require ... a warning message:

Deprecation Notice: Class LOCATION does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:/usr/local/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201

The message means something is wrong with the paths in the autoload section of the composer.json. We hope to cover all the reasons that this might be occurring.

1. Upper case in the namespace but lower case in the path

The first reason if your web application has some library and it is located in lib/SomeLibrary/SomeLibrary.php and the namespace of the file SomeLibrary.php is Lib\SomeLibrary:

<?phpnamespace Lib\SomeLibrary;class SomeLibrary {
...

In this case the composer v1 will show the warning message:

Deprecation Notice: Class Lib\SomeLibrary\SomeLibrary located in ./lib/SomeLibrary/SomeLibrary.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:/usr/local/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201

To resolve this issue you should rename the lib folder to Lib, it should be exactly the same as what you have in the namespace. We do not recommend renaming all the namespaces to match the capitalization of the folder path because of readability/maintainability concerns. This just doesn't look as good. namespace lib\SomeLibrary;

2. The different class name than the file name

If your file is Library.php but the class name is different - SomeLibrary:

...
class SomeLibrary {
...

To resolve this one you should rename the file Library.php to SomeLibrary.php to be the same as the class name. The refactoring that must be done because of the file rename will be much easier than the refactoring if you changed the class name.

3. Namespace missing the full root Namespace in multi-level namespaces

If your package has a multi-level namespace, the namespace in all the files should include the root Namespace.

For example, if you have a package with the namespace like "Vendor\\Api\\": "src/", and inside src/ you have a folder called Api. In this folder you have a Request.php class.

EXAMPLE 1: WRONG

namespace Vendor\Api;

EXAMPLE 2: RIGHT

namespace Vendor\Api\Api;

Seems like an easy mistake that we have all probably come across in the past. A similar issue to this is if you have added an extra path to your namespace. The Example 1 would be correct if Request.php lives in the root of src but your namespace looks like Example 2 (you have added an extra API to the namespace). Just remember; the namespace in the file should exactly match the file path with the Namespace root prepended to it.

We hope that this has helped you in getting ready for the release of composer v2. We are excited about the features in the upcoming release. Providing more feedback for developers and enforcing coding standards will lead to better code that can withstand the tests of time.

--

--

Vitaliy Dotsenko
Legacybeta

I like coding, open-source software and hi-tech 🚀