Error: Class 'path' Is not compliant with PSR-4 configuration

Viewed 296

I am trying PHP coding standard package: https://github.com/inpsyde/php-coding-standards, but when I start running phpcs, I get:

nickan@nickan-VirtualBox:~/src/wordpress/wp-content/plugins/my-plugin$ vendor/bin/phpcs ./src/model/Users.php 
E 1 / 1 (100%)



FILE: /home/src/wordpress/wp-content/plugins/my-plugin/src/model/Users.php
-------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-------------------------------------------------------------------------------------------------------------------------------
 5 | ERROR | Class 'Plugin\Model\Users', located at
   |       | '/home/src/wordpress/wp-content/plugins/my-plugin/src/model/Users.php', is not
   |       | compliant with PSR-4 configuration. (Inpsyde.CodeQuality.Psr4.InvalidPSR4)

Here is the file in path my-plugin/src/model/Users.php

<?php declare(strict_types=1);

namespace Test\Model;

class Users
{

}

I don't get what's wrong, the code is working fine but the error keeps showing up. I tried to have different namespacing, changing the folder, etc, still to no avail, help would be greatly appreciated. Thanks.

2 Answers

PSR-4 demands (among other stuff) that the namespace directly corresponds to the directory structure. This is case sensitive.

Long story short: You need to name your directory src/Model/Users.php with a capital M.

Also, your namespace does not have a vendor prefix as demanded by PSR-4. The sniff would not be able to detect that, but you should still fix that.

The issue maybe because of the following configurations. Please make sure you have added the correct configurations

    <rule ref="Inpsyde.CodeQuality.Psr4">
    <properties>
        <property
            name="psr4"
            type="array"
            value="Inpsyde\MyProject=>wp-content/plugins/inpsyde-task-plugin/InpsydeTaskPlugin.php,InpsydeTests=>tests/InpsydeTests"/>
    </properties>
</rule>
Related