Search code examples
phpnamespacesphpstorm

Is this namespacing issue my problem or is it an IDE problem?


I have a namespacing notification on the PhpStorm IDE and I can't see how to resolve it.

IDE

  • PhpStorm 2023.1

Structure

I have a class amongst various other classes by other vendors, my class sits in this folder:

/home/classes/vendor/namespaceOuter/namespaceInner/subnamespace

The autoloader.php file sits in /home/classes/

My class file sits inside the above folder, and is ClassFile.php. Within, this has the namespace set out as here:

<?php
Namespace namespaceOuter\namespaceInner\subnamespace\ClassFile;

use \exception;
use \PDOexception;

class ClassFile {
      ... 
}

Problem

The PhpStorm IDE tells me:

Namespace name doesn't match the PSR-0/PSR-4 project structure

NOT a problem

  • The autoloader works perfectly.
  • The class and its children and peers all work perfectly on the live website, the issue seems purely with the IDE informing me we're not following the PSR-0/PSR-4 .
  • These are our own classes and not set by or involving Composer (other classes do).

Attempted Solutions

  • I have tried to change the namespace to make it call from root (\namespaceOuter\namespaceInner\subnamespace\ClassFile;) within the Class file but this doesn't remove the notification.

  • I have tried to use the suggested fix by the IDE which in fact makes a bunch of new child folders in the current class' folder (/home/classes/vendor/namespaceOuter/namespaceInner/subnamespace/namespaceOuter/namespaceInner/subnamespace/ClassFile.php) which is clearly impractical and wrong.

  • Other established classes (such as PHPMailer) within the vendor/ folder do not have the same notice on their class files.

Reading

I have read the PHP Namespacing documentation as well as the PSR-4 documentation which includes this example:

Fully Qualified Class Name Namespace Prefix Base Directory Resulting File Path
\Symfony\Core\Request Symfony\Core ./vendor/Symfony/Core/ ./vendor/Symfony/Core/Request.php

While this IS NOT a Symfony project, the inconsistency between qualified namespace and file path implies this is not an issue.

Fully Qualified Class Name Namespace Prefix Base Directory Resulting File Path
\namespaceOuter\namespaceInner\subnamespace\ClassFile.php namespaceOuter\namespaceInner\subnamespace classes/vendor/ ???

Question

Is this namespacing incorrect somehow or is there an issue with the PHPStorm IDE suggestion (I believe so) -- if so, how can I check/ update / confirm / fix this?

Thanks


Solution

  • After spending a long time writing out this question I found the answer was:

    In PhpStorm --> Settings --> Directories the /home/classes/vendor/namespaceOuter/namespaceInner/subnamespace directory was marked as a "Resource Root". By replacing this with /home/classes/ as the Resource Root (Documentation) the notification magically disappeared.

    So this issue appeared to be an IDE issue rather than a namespacing issue.

    EDIT:

    The project root is /home/ .
    The $_SERVER['DOCUMENT_ROOT'] is /home/public_html .
    The classes/ folder is now a "Resources Root" and the /home/ folder is the "Content Root" for the PhpStorm project.

    This works because (I think) The autoloader sits in the classes/ folder, so PhpStorm "Resources Root" should be the same folder the PHP Autoloader sits in so that PhpStorm can find all the autoloaded classes