{"id":19334,"date":"2014-04-22T08:21:48","date_gmt":"2014-04-22T08:21:48","guid":{"rendered":"https:\/\/blog.jetbrains.com\/phpstorm\/?p=7348"},"modified":"2024-01-09T15:00:03","modified_gmt":"2024-01-09T14:00:03","slug":"psr-0-psr-4-and-sourcetest-root-support-in-phpstorm-8-eap","status":"publish","type":"phpstorm","link":"https:\/\/blog.jetbrains.com\/phpstorm\/2014\/04\/psr-0-psr-4-and-sourcetest-root-support-in-phpstorm-8-eap\/","title":{"rendered":"PSR-0, PSR-4 and Source\/Test Root support in PhpStorm 8 EAP"},"content":{"rendered":"<p>The latest\u00a0<a href=\"http:\/\/confluence.jetbrains.com\/display\/PhpStorm\/PhpStorm+Early+Access+Program\" target=\"_blank\" rel=\"noopener\">PhpStorm 8 EAP<\/a>\u00a0brings support for <a href=\"http:\/\/www.php-fig.org\/psr\/psr-0\/\" target=\"_blank\" rel=\"noopener\">PSR-0<\/a>\u00a0and <a href=\"http:\/\/www.php-fig.org\/psr\/psr-4\/\" target=\"_blank\" rel=\"noopener\">PSR-4<\/a>\u00a0namespace 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.<\/p>\n<p>PSR-0, also\u00a0known as the\u00a0Autoloading Standard, prescribes that classes and namespaces in PHP should match the directory and file structure, and vice-versa. PSR-4\u00a0allows\u00a0specifying a namespace prefix for a given directory explicitly. Having this project structure makes autoloaders in different PHP frameworks interoperable.<\/p>\n<p><!--more--><\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2014\/04\/phpstorm-autodetection-of-psr-0-root.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-7363\" alt=\"Autodetection of PSR-0 root\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2014\/04\/phpstorm-autodetection-of-psr-0-root.png\" width=\"556\" height=\"66\" \/><\/a><\/p>\n<p>Detection of the source root can also be triggered from the\u00a0<strong><em>Code | Detect PSR-0 Namespace<\/em> <em>Roots<\/em><\/strong> menu. We can also configure the source root manually through <em><strong>Project Settings |<\/strong><strong> Directories<\/strong><\/em>.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2014\/03\/phpstorm-detect-psr-0-root.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-7352\" alt=\"Detection of PSR-0 root\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2014\/03\/phpstorm-detect-psr-0-root.png\" width=\"569\" height=\"333\" \/><\/a><\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2014\/03\/phpstorm-content-roots.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-7358\" alt=\"Multiple PSR-0 roots\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2014\/03\/phpstorm-content-roots.png\" width=\"473\" height=\"237\" \/><\/a><\/p>\n<p>Note that next to PSR-0, we can also configure the package prefix according to <a href=\"http:\/\/www.php-fig.org\/psr\/psr-4\/\" target=\"_blank\" rel=\"noopener\">PSR-4<\/a>. This can be done by clicking the <em><strong>p<\/strong><\/em> icon next to a source folder. PhpStorm will use the prefix specified here as the namespace root for the source folder.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2014\/04\/phpstorm-psr-411.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-7515\" alt=\"PSR-4 namespace prefix support\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2014\/04\/phpstorm-psr-411.png\" width=\"507\" height=\"277\" \/><\/a><\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2014\/03\/phpstorm-create-class.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-7353\" alt=\"Create class respecting PSR-0\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2014\/03\/phpstorm-create-class.png\" width=\"630\" height=\"424\" \/><\/a><\/p>\n<p>There is also an <a href=\"http:\/\/youtrack.jetbrains.com\/issue\/WI-20051\" target=\"_blank\" rel=\"noopener\">inspection to verify that all namespaces comply<\/a> to the PSR-0 and PSR-4\u00a0settings. If the class path does not match the namespace, PhpStorm will tell us.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2014\/04\/phpstorm-inspection-psr.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-7440\" alt=\"PSR-0, PSR-4 inspection namespace does not match project structure\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2014\/04\/phpstorm-inspection-psr.png\" width=\"474\" height=\"96\" \/><\/a><\/p>\n<p><a href=\"http:\/\/confluence.jetbrains.com\/display\/PhpStorm\/PhpStorm+Early+Access+Program\" target=\"_blank\" rel=\"noopener\">Download PhpStorm 8 EAP<\/a>\u00a0and 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\u00a0the\u00a0<a href=\"http:\/\/youtrack.jetbrains.com\/issues\/WI\" target=\"_blank\" rel=\"noopener\">issue tracker<\/a>, or by posting your comments below or in our\u00a0<a href=\"http:\/\/devnet.jetbrains.com\/community\/wi?view=discussions\" target=\"_blank\" rel=\"noopener\">forums<\/a>!<\/p>\n<p><em>Develop with pleasure!<\/em><br \/>\n<em>\u2013 JetBrains PhpStorm Team<\/em><\/p>\n","protected":false},"author":118,"featured_media":0,"comment_status":"open","ping_status":"open","template":"","categories":[826,808,907],"tags":[600,190,2264,2265,2266,195,1105],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/phpstorm\/19334"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/phpstorm"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/types\/phpstorm"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/users\/118"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/comments?post=19334"}],"version-history":[{"count":1,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/phpstorm\/19334\/revisions"}],"predecessor-version":[{"id":432145,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/phpstorm\/19334\/revisions\/432145"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/media?parent=19334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/categories?post=19334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/tags?post=19334"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/cross-post-tag?post=19334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}