One of the most important tools for quality developments in Magento is PhpStorm:

  • “Intelligent coding assistance.”
  • “PHP IDE that actually ‘gets’ your code.”

That’s what PhpStorm is for developers.

Maybe you have already read a lot on the topic. But we gathered our real experience and aggregated it into this post for you. Below are key tips and tricks for configuring PhpStorm for your easy and enjoyable developments in Magento 2.

Use Static Code Analysis

Magento 2 includes a ready-made set of rules and standards for Code Sniffer (phpcs) and PHP Mess Detector (phpmd).

Use these tools to validate code against Magento 2 coding standards without even running. Results you get:

  • high validation speed
  • code validation on-the-fly.

Learn more about Magento 2 standards at: Magento 2 Developer Documentation

Configure your Magento 2 Code Sniffer correctly

Code Sniffer validates the code against standards of formatting, for availability and accuracy of comments arrangement, etc.

You can find the file with the rules set in dev/tests/static/testsuite/Magento/Test/Php/_files/phpcs/ruleset.xml

phpCS Configuration

To include the rules set into Code Sniffer project follow this simple 6-step PhpStorm Magento 2 setup instructions:

  1. Go PhpStorm -> Preferences
  2. In the left menu choose Inspections in the sub-menu Editor
  3. Find in the list PHP -> PHP Code Sniffer validation
  4. Turn Validation On
  5. Choose Coding Standard -> Custom and click “ … “
  6. Now choose the directory dev/tests/static/testsuite/Magento/Test/Php/_files/phpcs/

Congratulations! You did it!

But how to check if it is working fine? If you have configured all settings properly, you would see the following result:

phpCS Highlight

P.S.: If you have not configured PhpStorm before, you had to specify the direct destination of phpcs. You can do it in the menu item PhpStorm -> Preferences -> Languages & Frameworks -> PHP -> Code Sniffer. Here you should specify the destination to the file in the section Development Environment.

PhpStorm

Also, try to use PhpStorm to format code automatically. It can greatly speed up the process if you have a lot of files that were formatted incorrectly.

Use PhpStorm Mess Detector to detect possible errors and complications

PHPMD also executes the static code analysis. But it is more aimed at detecting possible errors and too complicated for interpretation code fragments (classes, methods).

According to our experience, if your code passes all tests and validations (particularly, on compliance with the rules CyclomaticComplexity, NPathComplexity, CouplingBetweenObjects), then it is easy to read, to test and it contains less potential errors.

Configuration process of PHP mess detector in PhpStorm is similar to the Code Sniffer setting up procedure, except for the rules set file: dev/tests/static/testsuite/Magento/Test/Php/_files/phpmd/ruleset.xml

If you have done everything accurately, you will get the result from the screenshot below:

phpMD Hightlight

Use XML Schema Validation to avoid errors in XML files

Unlike Magento 1, every XML file in Magento 2 (layouts, etc/*) must comply with XSD schemas. If any error occurred while XML files editing process, PhpStorm will show it:

XSD

It’s a precise habit to perform checking for errors in XML files.

To use this feature, execute the following command in ssh terminal: bin/magento dev:urn-catalog:generate .idea/misc.xml

Use Excluded Directories to boost performance

In the process of development, PhpStorm regularly indexes all project files and their changes. This action is important for building relations between classes and for autocompletion.

But every project includes lots of files, which are not necessary to index. Furthermore, the exclusion of those files allows you to increase PhpStorm performance in everyday activities. You need to understand PhpStorm indexing settings to configure your dev environment effectively.

We recommend you exclude the following directories:

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

Follow this instruction to exclude suggested directories:

Excluded directories

So, this is it. Now your basic configuration of PhpStorm is ready. We hope our experience, tips, and tricks on this process helped you pass it easily and enjoy your developments.

Usage of PhpStorm in Magento development can greatly improve your productivity. Share your thoughts in the comments below and have a nice coding!

Related Posts
10 Rules of eCommerce Project Planning You Need to Follow. Part 1 Magento Robots.txt and Sitemap: Everything You Need to Know Making the right choice for your Magento Store. Magento extensions, Magento custom development, or both.

Enjoyed the Read?

Don’t miss our next article!