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 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 standards without even running. Results you get:
- high validation speed
- code validation on-the-fly.
Configure your 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
To include the rules set into Code Sniffer in Magento 2 project follow this simple 6-step instruction:
- Go PhpStorm -> Preferences
- In the left menu choose Inspections in the sub-menu Editor
- Find in the list PHP -> PHP Code Sniffer validation
- Turn Validation On
- Choose Coding Standard -> Custom and click “ … “
- 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:
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.
Use PHP 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).
We can make a bold claim according to our experience, that 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 PHPMD 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 would get the result from the screenshot below:
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 occured while XML files editing process, PhpStorm will show it:
It’s a precise habit to perform checking for errors in XML files.
To use this feature, simply 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 autocomplete.
But every project includes lots of files, which are not necessary to index. Furthermore, the exclusion of those files allows to enhance PhpStorm performance in everyday activities.
We recommend you exclude the following directories:
Follow this instruction to exclude suggested 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 to pass it easy and enjoy your developments.
Share your thoughts in the comments below and have a nice coding!