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

This blog is permanently closed.

For up-to-date information please follow to corresponding WebStorm blog or PhpStorm blog.

 
This entry was posted in Cool Feature, PhpStorm and tagged , , , , , , . Bookmark the permalink.

5 Responses to Optimize Imports for PHP

  1. Priet says:

    Great feature!

    “Splits multiple use statement imports into single use statement imports.”

    What is wrong with using a single ‘use’ statement for multiple imports per namespace?

    • Maks says:

      @Priet nothing.
      Only is one option more.

      The people which follows PSR-1/2 coding style convenions can be found useful.

  2. What about imports for parent classes vs. child classes where the parent must be imported first. Will they get alphabetized even if the parent would alphabetically sort after the child?

    • Alexey Gopachenko says:

      Not sure we get the question. Care to provide an example where use class list depends on class inheritance?

    • Jannik says:

      use in PHP is just the import of the namespace, not the import of an actual file (which is done by import/require/autoloading).
      Namespace imports are just a lexicographical aliasing of the namespace inside of this one file.

Comments are closed.