Optimize Imports for PHP

Those of you who have already tried PhpStorm 6 might have noticed a lot of great additions to PHP imports support. In this post we would like to tell you about a long awaited feature: Optimize Imports for PHP.

The Optimize Imports action is designed to help you maintain (sometimes very long) lists of imports. It performs the following optimizations for you:

  1. Removes unused imports
  2. Sort imports alphabetically (ascending order)
  3. Splits multiple use statement imports into single use statement imports.

Note that importing is performed in compile-time, so it is a safe transformation and doesn’t change the semantics of your application.

For example, before Optimize Imports, the list of imports is a bit cluttered:

After Optimize Imports, we can see (diff) that imports have been sorted by namespace:

The Optimize Imports action can be invoked directly on a file, or on a directory, by pressing Ctrl+Alt+O or Cmd+Opt+O on Mac. Optimize imports is also an option in the Commit and Reformat Code dialogs.

Before committing code to source control, we can select the Optimize imports checkbox to perform the optimize imports action before the actual commit:

The Reformat Code dialog also allows us to optimize imports in one go:

Please download the latest build, provide feedback for bugs and feature requests here, and leave questions in the comments below or in our forums!

Develop with pleasure!
– JetBrains Web IDE Team