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

Posted by Joaquín Ruiz on 24th May 2017

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.


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 provides the binary in the following directory:

vendor/bin/phpcs

Now PHPStorm will analyze each PHP file if it follows the standards:


OK


WRONG

3- Mess Detector

PHPMD can be seen as an user friendly and easy to configure frontend 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, Suboptimal code, Overcomplicated 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 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 > Mess Detector. Magento provides the binary in the following directory:

vendor/bin/phpmd

Now 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:

  • /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! 🙂




About the author:

Joaquín Ruiz is a Computer Engineer, Senior PHP Developer and Magento Certified Developer. Joki has more than 7 years of experience working with multiple PHP frameworks. He knows Magento, WordPress, Laravel, Yii.. like the back of his hand ;)