The step-by-step guide to set up PhpStorm for Magento 2

Magento is an open-source e-commerce platform written in PHP. Magento employs the MySQL/MariaDB relational database management system, the PHP programming language, and elements of the Zend Framework. It applies the conventions of object-oriented programming and model–view–controller architecture. Magento also uses the entity–attribute–value model to store data. On top of that, Magento 2 introduced the Model-View-ViewModel pattern to its front-end code using the JavaScript library Knockout.js.

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.

URN Catalog. The step-by-step guide to set up PhpStorm for Magento 2
URN Catalog. The step-by-step guide to set up PhpStorm for Magento 2


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

PHP CodeSniffer (PHPCS) is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations. PHP CodeSniffer is an essential development tool that ensures your code remains clean and consistent.

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:

dev/tests/static/testsuite/Magento/Test/Php/_files/phpcs/

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:

vendor/bin/phpcs
PHPCS . The step-by-step guide to set up PhpStorm for Magento 2
PHPCS . The step-by-step guide to set up PhpStorm for Magento 2

After this, PHPStorm will analyze each PHP file if it follows the standards:

PHPCS ok. The step-by-step guide to set up PhpStorm for Magento 2
PHPCS ok. The step-by-step guide to set up PhpStorm for Magento 2


OK

PHPCS wrong. The step-by-step guide to set up PhpStorm for Magento 2
PHPCS wrong. The step-by-step guide to set up PhpStorm for Magento 2


WRONG

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:

dev/tests/static/testsuite/Magento/Test/Php/_files/phpcs/
PHPMD Mess Detector. The step-by-step guide to set up PhpStorm for Magento 2
PHPMD Mess Detector. The step-by-step guide to set up PhpStorm for Magento 2

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:

vendor/bin/phpmd

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.

PHPMD Mess Detector 1. The step-by-step guide to set up PhpStorm for Magento 2
PHPMD Mess Detector 1. The step-by-step guide to set up PhpStorm for Magento 2
PHPMD Mess Detector 2. The step-by-step guide to set up PhpStorm for Magento 2
PHPMD Mess Detector 2. The step-by-step guide to set up PhpStorm for Magento 2

4- Exclude directories

In PHPStorm, you can customize the project structure, using the Directories page of the Settings/Preferences dialog.

Exclude directories. The step-by-step guide to set up PhpStorm for Magento 2
Exclude directories. The step-by-step guide to set up PhpStorm for Magento 2

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:

  • /bin/
  • /dev/
  • /pub/
  • /setup/
  • /var/cache/
  • /var/log/
  • /var/page_cache/
  • /var/view_preprocessed/

What are your thoughts on this? Do you configure anything else? Maybe the Unit tests? Let me know your comments, any feedback is welcomed! 🙂

0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Rafael Corrêa Gomes ♛

Thanks for sharing Joaquín!

Joaquín Ruiz
6 years ago

Thx Rafael! Hope it’s been useful! 🙂

2
0
Would love your thoughts, please comment.x
()
x