Setting up PhpStorm
PhpStorm is a commercial IDE, it is arguably the best IDE for PHP developers with features such as code completion, code inspection, phpunit support, Behat support, database editor, debugger, etc.
Installation
OS X
Do not install Java manually, download a PhpStorm package with bundled java instead.
General settings
- Disable missing @throws tag warning in "Preferences / Project Settings / Inspections / PHP / PHPDoc"
- Strip trailing whitespace from "Modified Lines" in "Preferences / IDE Settings / Editor/ General"
- Show line numbers in "Preferences / IDE Settings / Editor / Appearance"
- Add Moodle database prefix by create file .phpstorm.meta.php in the project root directory, put the code below to enable PhpStorm recognized {table_name} as Database table:
<?php
namespace PHPSTORM_META {
override(
// Virtual function to indicate that all SQL
// injections will have the following replacement rules.
sql_injection_subst(),
map([
'{' => "mdl_", // all `{` in injected SQL strings will be replaced with a prefix
'}' => , // all `}` will be replaced with an empty string
]));
}
Bug tracker integration
- Add tracker linking in "Preferences / Version control / Issue Navigation"
- Set Issue ID to "MDL-\d+|CONTRIB-\d+|MOBILE-\d+|MDLSITE-d+|MDLQA-\d+|UX-\d+|MDLNET-\d+|WPQA-\d+" and Issue link to "https://tracker.moodle.org/browse/$0" or just click on 'add Jira pattern' and paste "https://tracker.moodle.org"
Code formatting
- Setup coding style to use all rules from Coding style in "Preferences / Project Settings / Code Style / PHP" (or simply import from https://github.com/enovation/moodle-utils/blob/master/phpstorm-config/Moodle.xml) - this will allow you to use automatic code formatting and it does nice code formatting on copy/paste.
- Set line separator to "Unix and OS X (\n)" in "Preferences / Project Settings / Code Style / General".
- Set right margin to 132 or 180 in "Preferences / Project Settings / Code Style / General".
Tips & Tricks
- Use
/** @var admin_root $ADMIN */
to autofill $ADMIN->... - Remove SQL syntax inspection errors for Moodle tables surrounded by curly brackets (like:
SELECT * FROM {user}
) by adding\{(\w+)\}
to Tools > Databases > user parameters
(more info: https://blog.jetbrains.com/phpstorm/2014/11/database-language-injection-configuration/ , and a "feature request" to improve it: https://youtrack.jetbrains.com/issue/WI-4123 )
- You can deactivate warnings for specific exceptions (in particular the coding_exception, which is unlikely to be catched in your code) by going to Settings > PHP and add them to 'Unchecked Exceptions' under the 'Analysis' tab
Moodle code checker
Follow the instructions in the README
PHPUnit integration
- Install PHPUnit via Composer
- Tell PHPStorm where is composer - go to "Preferences / PHP / Composer", fill in "Path to PHP executable", "Path to composer.phar", "Path to composer.json" and make sure the option "Add packages as libraries" is enabled.
- Go to "Run / Edit configurations"
- Add PHPUnit configuration by clicking on "+"
- Click "Use alternative configuration file" and select your phpunit.xml file
- Go to "Run / Run ..." and select your new PHPUnit configuration to run
Database editor
- Click on the "Database" tab to see the database window
- Click "+" in the top left and add "Database source" for your database
- Note: click on the link to download the necessary drivers directly from IDE
Javascript Development
You can work on Javascript development by add Grunt configuration:
- Install Watchman - https://facebook.github.io/watchman/docs/install.html
- From the main Moodle directory open terminal and run:
npm install -g grunt-cli
npm install
- Open "Edit configuration"
- Add new Grunt Task
- Verify that the node version is set with proper version for the current Moodle version
- In Tasks select watch
- Save the Grunt task
- Verify that in config.php the setting is not comment
$CFG->cachejs = false;
- Click on run icon
- happy Javascript development
Useful plugins
- Php Inspections (EA Extended) - https://plugins.jetbrains.com/plugin/7622-php-inspections-ea-extended-/
- SonarLint - https://plugins.jetbrains.com/plugin/7973-sonarlint/
- Diff / Patch File Support - https://plugins.jetbrains.com/plugin/11957-diff--patch-file-support/
- Handlebars/Mustache - https://plugins.jetbrains.com/plugin/6884-handlebars-mustache/
- Markdown Navigator - https://plugins.jetbrains.com/plugin/7896-markdown-navigator/
- PHP composer.json support - https://plugins.jetbrains.com/plugin/7631-php-composer-json-support/
- PHP Advanced AutoComplete - https://plugins.jetbrains.com/plugin/7276-php-advanced-autocomplete/