Magento is a complex platform/framework, therefore we need to use all of the tools we have to help us to develop Magento properly. In my opinion, PHPStorm is the better IDE to develop a Magento2 project. That is because I present you The step-by-step guide to set up PhpStorm for Magento 2 to help the developers to develop properly Magento 2 projects by following 4 easy steps.
The step-by-step guide to set up PhpStorm for Magento 2
1- URN catalog
Magento code references all XSD schemas as Uniform Resource Names (URNs). If you’re developing code and need to reference XSDs, your integrated developer environment (IDE) to recognize and highlight URNs. This makes development easier.
Better in green than in red right? 😉
To perform this, you just need to execute this command from the root of your Magento2 directory:
bin/magento dev:urn-catalog:generate .idea/misc.xml
- bin/magento is the CLI of Magento2
- .idea/misc.xml is the location where we tell PHPStorm the URNs structure.
If you go to Preferences > Languages & Frameworks > Schemas and DTDs, you will see the urn:magento:framework configurations.
2- Code Sniffer
In PhpStorm, we can configure code style through Preferences > Editor > Inspections > PHP > Code Sniffer and use one of the many available schemes. In this case, we’ll use the one that Magento provides us, and it is in the following directory:
And if you don’t have PHPCS installed on your environment, you can specify a ‘phpcs‘ binary through Preferences > Languages & Frameworks > PHP > Code Sinffer. Magento 2 provides the binary in the following directory:
After this, PHPStorm will analyze each PHP file if it follows the standards:
3- Mess Detector
PHPMD can be seen as an user friendly and easy to configure front-end for the raw metrics measured by PHP Depend. What PHPMD does is: It takes a given PHP source code base and look for several potential problems within that source. These problems can be things like Possible bugs, Sub-optimal code, Over-complicated expressions, Unused parameters, methods, properties etc.
In PhpStorm, we can configure code style through Preferences > Editor > Inspections > PHP > Mess Detector and use one of the many available schemes. In this case, we’ll use the one that Magento 2 provides us, and it is in the following directory:
And if you don’t have PHPCS installed on your environment, you can specify a ‘phpcs’ binary through Preferences > Languages & Frameworks > PHP > Mess Detector. Magento 2 provides a binary in the following directory:
After this, PHPStorm will analyze each PHP file. You can see the results in the RHS gutter, at the top-right. It appears in a yellow box.
4- Exclude directories
In PHPStorm, you can customize the project structure, using the Directories page of the Settings/Preferences dialog.
Or you can customize the folders on the left pane (project) by performing a left click over the folder.
We recommend to exclude the following folders:
What are your thoughts on this? Do you configure anything else? Maybe the Unit tests? Let me know your comments, any feedback is welcomed! 🙂