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.
After configuring the interpreter, the next step is to set up the PHPStorm preferences for how to execute PHPUnit. Click the Use Composer autoloader option. Select the vendor/autoload.php file in your Magento 2 installation. Optionally select the dev/tests/unit/phpunit.xml.dist file as the Default configuration file. I wonder why in the official devbox guide is advised to set.
Maybe you've already read a lot on the topic, but we've gathered our real experience and aggregated it into this post for you. Below are some key tips and tricks for configuring PhpStorm to help make your developments in Magento 2 easier and more enjoyable.
- Magento Cloud Docker provides a separate container to handle Xdebug requests in the Docker environment. Use this container to enable Xdebug and debug PHP code in your Docker environment without affecting your Magento Commerce Cloud project configuration.
- I have loaded Magento 2 files and XML validation into PHPStorm which is very convenient. Is it also possible to do this (automatically) with factory classes and maybee other generated files? Since they are autogenerated in /var/generation PHPStorm does not recognize it.
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 it. You can expect the following results:
- high validation speed
- code validation on-the-fly.
Learn more about Magento 2 standards at: Magento 2Developer Documentation
Configure your Magento 2 Code Sniffer correctly
Code Sniffer validates the code against standards of formatting, 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 a Code Sniffer project, follow these simple 6-step PhpStorm Magento 2 setup instructions:
- Go PhpStorm -> Preferences
- In the left menu, choose Inspections in the sub-menu Editor
- Find PHP -> PHP Code Sniffer validation in the list
- Turn Validation On
- Choose the Coding Standard -> Custom and click “ … “
- Now choose the directory dev/tests/static/testsuite/Magento/Test/Php/_files/phpcs/
Congratulations! You did it!
But how can you check to see if it's working fine? If you have configured all settings properly, you will see the following result:
P.S.: If you have not configured PhpStorm previously, you need to specify the direct destination of the phpcs. You can do this in the menu item PhpStorm -> Preferences -> Languages & Frameworks -> PHP -> Code Sniffer. Here you should specify the destination to the file in the Development Environment section.
Also, you can 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. However, it is more aimed at detecting possible errors and too complicated for interpretation code fragments (classes, methods).
Magento Phpstorm Xdebug
According to our experience, if your code passes all tests and validations (particularly regarding compliance with the rules CyclomaticComplexity, NPathComplexity, CouplingBetweenObjects), then it is easy to read and test, and it contains fewer potential errors.
The Configuration process of the 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:
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:
It’s a good habit and practice 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 development process, PhpStorm regularly indexes all project files and their changes. This action is important for building relations between classes and for autocompletion.
Still, 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 in order to configure your dev environment effectively.
We recommend that you exclude the following directories:
Phpstorm Magento 2 Plugin
- /bin/
- /dev/
- /pub/
- /setup/
- /var/cache/
- /var/log/
- /var/page_cache
- /var/view_processed
Follow these instructions to exclude suggested directories:
That's 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.
Magento 2 Phpstorm Urn
Usage of PhpStorm in Magento development can greatly improve your productivity. Share your thoughts in the comments below and have a nice coding!