Using Composer 2.0 with PSR4

Vitaliy Dotsenko
Sep 10 · 3 min read
Image for post
Image for post
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.

Legacybeta

Building better software

Sign up for LegacyBeta Newsletter

By Legacybeta

From our desk to yours Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Vitaliy Dotsenko

Written by

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

Legacybeta

Articles on living and working in an online world.

Vitaliy Dotsenko

Written by

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

Legacybeta

Articles on living and working in an online world.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store