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:
- Removes unused imports
- Sort imports alphabetically (ascending order)
- 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:
Develop with pleasure!
– JetBrains Web IDE Team
Subscribe to Blog updates
PhpStorm 2023.3 Is Now Available
Explore the new features in PhpStorm 2023.3: support for PHP 8.3, the Pest test framework, improvements for Symfony projects and AI Assistant.
PhpStorm Now Supports PHP 8.3
PhpStorm now provides full support for PHP 8.3 feature: the new `#[Override]` attribute and `json_validate()` function, and more!