PSR-0, PSR-4 and Source/Test Root support in PhpStorm 8 EAP

The latest PhpStorm 8 EAP brings support for PSR-0 and PSR-4 namespace roots. PhpStorm comes with a configurable convention which specifies that the project Source Root is also a root for the package and namespace. In other words: every directory created under the Source Root will be considered a separate namespace.

PSR-0, also known as the Autoloading Standard, prescribes that classes and namespaces in PHP should match the directory and file structure, and vice-versa. PSR-4 allows specifying a namespace prefix for a given directory explicitly. Having this project structure makes autoloaders in different PHP frameworks interoperable.

When opening a PhpStorm project that contains at least one file with a namespace, the IDE will propose setting the namespace root. Also, when no configuration has been provided yet and a class is created, the IDE will propose to configure the namespace root.

Autodetection of PSR-0 root

Detection of the source root can also be triggered from the Code | Detect PSR-0 Namespace Roots menu. We can also configure the source root manually through Project Settings | Directories.

Detection of PSR-0 root

It is perfectly fine to configure multiple source roots: PhpStorm will treat each of those as a namespace root and provide namespace hints for files underneath them. For example every subfolder of a vendor folder can be marked as a namespace root. In the example below, we have two namespace roots: one for the actual application and one for a vendor folder.

Multiple PSR-0 roots

Note that next to PSR-0, we can also configure the package prefix according to PSR-4. This can be done by clicking the p icon next to a source folder. PhpStorm will use the prefix specified here as the namespace root for the source folder.

PSR-4 namespace prefix support

When configured (typically once per project), these settings are used throughout the IDE, for example when creating a new class or a new PHPUnit test class, and in structure refactorings (Move Class, Move Namespace, Extract Interface), to provide namespace hints.

Create class respecting PSR-0

There is also an inspection to verify that all namespaces comply to the PSR-0 and PSR-4 settings. If the class path does not match the namespace, PhpStorm will tell us.

PSR-0, PSR-4 inspection namespace does not match project structure

Download PhpStorm 8 EAP and give it a go! Note this is still a preview and we are working on expanding this feature. We very much appreciate your feedback through the issue tracker, or by posting your comments below or in our forums!

Develop with pleasure!
– JetBrains PhpStorm Team

This entry was posted in Cool Feature, Early Access Program and tagged , , , , , , . Bookmark the permalink.

9 Responses to PSR-0, PSR-4 and Source/Test Root support in PhpStorm 8 EAP

  1. Pingback: News You May Have Missed

  2. donquixote says:

    Hi,
    it is great to read this, but it seems a bit confusing to me, and it did not work with PSR-4 for me yet.

    In the example snapshot above showing the “Edit root properties” dialog, the prefix is “JetBrains/Wordpress/NetworkMarketing”. This is weird, I would expect this to be a namespace with backslashes – so “JetBrains\Wordpress\NetworkMarketing”. Also, to resemble the way Composer’s addPsr4() works, maybe add a trailing backslash, like so: “JetBrains\Wordpress\NetworkMarketing\” (or make this optional).

    Either way, none of these variations work for me so far. It still complains that “Namespace name doesn’t match PSR-0 / PSR-4 project structure”.

    I have a class Donquixote\CliTools\OptionInfo, and the file is src/OptionInfo.php. So I mark the src folder as Sources root, click “p” and specify “Donquixote/CliTools” and other variations with backslash, but I always get the above inspection warning.

    I can file a bug report, but let’s first clarify what exactly I am supposed to type in this dialog, ok?

    • Maarten Balliauw says:

      I’ve updated the screenshot, good catch. The namespace should of course have been the PHP namespace, in this case “JetBrains\Wordpress\NetworkMarketing”.

      Regarding your issue, I’ve just tried this and it seems that PhpStorm does not correctly pick this up at the root level. Would you mind filing an issue for this?

  3. Hi,
    I have just installed the last EAP version. I am working on our phing build file but i have a problem with the editor that do not accept a namespaced class name in the <taskdef> “classname” attribute :
    <taskdef name=”ComposerUpdate” classname=”Foo\Phing\Task\ComposerUpdate” />

    error : “Class Foo\Phing\Task\ComposerUpdate in ‘Foo\Phing\Task\ComposerUpdate.php’ does not exist. ”

    So there is no completion for our custom phing task/type and fake errors are reported.

    However phing run properly with this build file.

    I precise that i include the “vendor/autoload.php” at the top of the build file:
    <php expression=”include(‘vendor/autoload.php’)”/>
    Thanks.
    (Sorry for duplicate post…)

    Best regards,
    Antoine

    • leneshka says:

      Hello,

      please try point as separator instead of backslashes. Phing docs say that’s the right way, so it’s what PhpStorm demands for proper work.

  4. Pascal says:

    Hey,
    ist it possible to refactor a whole bunch of classes by simply drag & drop them in a new directory or do I have to go into every individual class an use class refactoring?

    Currently PhpStorm (v8.01) won’t update namespaces on drag&drop.

    Cheers
    Pascal

  5. Eddy says:

    Is it possible to switch the check off?
    The majority ofout code is legacy, and it keeps telling me it has PSR-0 namespace roots detected.
    Also can not find anything to ignore the message.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">