Using PhpStorm for CiviCRM Development¶
PHPStorm is a commercial IDE which is popular among CiviCRM developers.
General project setup¶
- Use the root directory of your CMS as your project root (otherwise in-app debugging won't work)
- You can speed up PhpStorm's indexing process by taking the following steps to ignore most files:
- Settings > Directories
- Mark all directories as Excluded
- Drill down to find the civicrm folder and mark it as Sources
Code style setup¶
Create the CiviCRM code styling preference:
- Open Settings > Editor > Code Style
- Select Scheme > Default or Project if you want these settings to apply only to CiviCRM
- In the Code style sub-menu, select PHP, then Set from ... at the far right, Drupal (this sets options across all tabs of the dialog), then click Apply
- In the Code style sub-menu, select Javascript, then Set from ... at the far right, PHP (this replicates all PHP settings to the Javascript section), then click Apply
That's it. You can now use this code style on all future CiviCRM-related projects.
XDebug integration¶
You need to configure XDebug on the webserver, phpstorm on the development machine and a debugger helper in the browser.
To configure XDebug and PHPStorm see: https://www.jetbrains.com/help/phpstorm/configuring-xdebug.html
For browser helpers see: https://confluence.jetbrains.com/display/PhpStorm/Browser+Debugging+Extensions
Running automated tests from within PHPStorm¶
PHPStorm provides convenient tools for running and debugging unit tests in-app.
Note
These instructions assume you already have a working instance of CiviCRM running locally with buildkit.
- On the CLI from a directory inside your build, run the command
civibuild phpunit-info
. This outputs configuration settings you will need to paste into PHPStorm. - From the
Run > Edit Configurations
menu edit the default PHPUnit configuration for your project. - Check "Use alternative configuration file" and choose the one output by the
civibuild
command. - Set the custom working directory as specified by the
civibuild
output. - Expand "Environment variables" and add the variables shown in the
civibuild
output.
For more detailed instructions (with screenshots!) see this StackExchange answer.
Adding external libraries¶
It can be frustrating when writing tests the PHPStorm complains about missing classes or undefined methods. This happens because PHPUnit is not included in the CiviCRM codebase.
To remedy this you can add an external content root. You'll need to clone the PHPUnit library locally. Then from that directory check out the latest supported version of PHPUnit (4.x right now).
After that you just add the directory to your project include paths by following the instructions on the Jetbrains site.
Alternatively: always use buildkit to generate you CiviCRM development environment; it ships with many tools - including PHPUnit 4