Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpStorm tells me: "Namespace name doesn't match PSR 0/PSR 4 project structure"

Tags:

phpstorm

I'm using Laravel 5.0 and everything works fine.

However, I keep getting these warning messages. I have looked at the JetBrains docs, but I remain a bit foggy what to do about it.

Essentially, I am concerned that if I allow PhpStorm to automatically run "Detect PSR0 Namespace Roots" then it could break my code / project.

Not sure what is going on, what should I do?

like image 420
Vince Avatar asked May 07 '15 18:05

Vince


People also ask

What is PSR-0 and PSR 4?

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.

What PSR-0?

PSR-0 looks at the namespace of a class and discerns its location on the hard drive from that bit of information. For example, the class \Zend\Mail\Message would lead to /path/to/project/lib/vendor/Zend/Mail/Message.


2 Answers

If you use composer the more recent answer to synchronize with composer is a better option.


PSR-4 namespace roots can specify a prefix which can be configured.

Here is the process:

  1. Find the root of your namespace e.g src.
  2. right click Mark Directory as Sources Root.
  3. Menu -> File -> Settings -> Directories.
  4. Click the pencil icon or p^ next to your Sources Root.
  5. Enter the namespace prefix.

See PHPStorm configuring PHP namespaces

like image 72
Paul Avatar answered Oct 17 '22 04:10

Paul


Easier than the last answers:

PHPStorm & Composer settings

Go to the Settings windows: Languages & Frameworks -> PHP -> Composer and then, enable Sync IDE settings with composer.json in order to load the PSR-0 & PSR-4 set up.

PS: PhpStorm 2020.3.2 (Build #PS-203.7148.74, built on January 27, 2021)

I hope it's useful to everyone!

like image 37
Mexidense Avatar answered Oct 17 '22 05:10

Mexidense