Features Releases

Configuring With Composer in PhpStorm 2017.2

Configuring PhpStorm correctly is important to get the most out of your IDE, and PhpStorm 2017.2 has made it easier by allowing you to detect some of the settings from your `composer.json` file.

When you’re using PhpStorm to develop efficiently, there is a few configuration setting that should be right to make the IDE is as helpful as possible. Telling PhpStorm which version of PHP you’re targeting allows the inspections that power the IDE to let you know if you’re using things that are deprecated (or not available in your target version), and enables some features that are only available in later versions of PHP like the scalar parameter and return types.

You can configure the PHP version you are targeting using the dropdown you’ll find under Preferences | Languages and Frameworks | PHP:

composer-php-settings

Telling PhpStorm where your namespaced files live will help when creating classes and tests (and navigating between them) and refactoring things. You can mark directories as sources or tests from the context menu in the project pane, or you can use the Directories section of the Preferences window.

composer-mark-directories

  • Tests – Mark the directory as a source of PHPUnit tests
  • Sources – Mark as a source of PHP autoloadable classes
  • Excluded – Don’t index this!
  • Resource Root – Contains resources that can be accessed relatively – like images, CSS files or javascript

You can learn more about marking directories in the web help article.

It’s important when you’re marking folders as either Tests or Sources that you also tell PhpStorm which namespaces those directories contain, and you can do this by clicking on the Package Prefix icon under the directory listing in the right-hand pane:

composer-mark-dir_upd2
Here we can see that we’ve marked `lib/Doctrine/ORM` as a PSR-4 namespace root for the `Doctrine\ORM` and `tests/Doctrine/Tests` as a PSR-4 namespace root for `Doctrine\Tests`.

Adding this information is a little cumbersome and we’ve made attempts before to automate the process using the Detect PSR-0 Namespace Roots action (under the Code menu), but this has never been entirely successful and is getting less useful with the prevalence of the PSR-4 autoloading standard. Of course, the most common way of autoloading files in this day is to use the `autoload` and `autoload-dev` keys of your `composer.json` file to setup autoloading for you.

    "autoload": {
        "psr-4": { "Doctrine\\ORM\\": "lib/Doctrine/ORM" }
    },
    "autoload-dev": {
        "psr-4": { "Doctrine\\Tests\\": "tests/Doctrine/Tests" }
    },

Using these Composer directives allows you to use a single autoloader for locating and loading both libraries files pulled in as Composer dependencies and your own code and test. The `autoload` files are available to autoload always, whereas the `autoload-dev` files are only autoloadable in your dev environment (which is nice to avoid polluting the autoloader with tests in production).

Composer also allows you to tell it the minimum version of PHP you support and will fail to install if the target system doesn’t run the supported version:

    "require": {
        "php": "^7.0",
    },

We’ve learned that you should be defining your autoloading rules and minimum PHP version in your Composer settings and that PhpStorm also needs you to configure this information to work at its best – so why can’t PhpStorm read the Composer configuration to save you configuring it yourself?

As of 2017.2 PhpStorm can now read your Composer configuration to automatically configure directories and PHP version for you.

To make this happen, PhpStorm needs only to have Composer configured correctly including where to find the Composer executable, and which `composer.json` file to use.

composer-sunc_upd

When you use this feature, you get the added bonus of automatically configuring PHPUnit and Behat if you’re requiring them as a dependency (or development dependency) in your `composer.json`.

This release has some more improvements around auto-detection, we’ve also added auto-detection of enabled extensions under the PHP configuration as part of our continuing mission to make using PhpStorm easier (more information comes in future blog posts).

– Gary & The PhpStorm Team

 

image description