Reworked Imports & Code Cleanup
In 2019.1 release, we’ve reworked the inspections and intention actions related to importing namespaces and using FQN. We’ve also added some PHP-specific intentions to the Code Cleanup tool, which will help you automatically run safe transformations on the whole project or a part of it.
Reworked Imports
We’ve reworked the inspections and intention actions related to importing namespaces and using FQN. The main idea behind them is to avoid qualifiers as much as possible.
To get started, make sure that the inspections Fully Qualified name usage and Unnecessary fully qualified name are enabled under Preferences | Editor | Inspections | PHP | Code Style.
In the Options section for each of these inspections, you can additionally choose whether they should apply to files not bound to any specific namespace, and whether the entities from the global namespace should be subject to imports optimization or not. In most cases, however, you’re good to go with the above default values.
Now, PhpStorm will now let you simply remove a redundant qualifier if possible:
Otherwise, it will let you replace it with a use import statement.
Also, when you have a class imported as an alias, PhpStorm will prompt you to reuse this alias when you paste some code into a file.
Sort use statements
If you have multiple use statements in a file, you can now automatically sort them either alphabetically or by length. Go to the Code Conversion tab under Preferences | Editor | Code Style | PHP and choose the sorting type.
Having done that, use Code | Optimize Imports to sort your statements.
Code Cleanup
Code Cleanup is a batch action that lets you run a number of safe transformations on the whole project or a part of it. Earlier, this tool included only JavaScript inspections inherited from WebStorm. In PhpStorm 2019.1, we are bringing PHP-specific intentions to Code Cleanup.
You can run the cleanup tasks at any time by selecting Code | Code Cleanup… from the main menu.
Under Code Style, you’ll find two inspections fixing the redundant usages of fully qualified class names. PhpStorm will get rid of them by either adding a use statement or just removing the extra namespace part.
The Quality Tools inspections provide support for two popular fixers: PHP CS Fixer and PHP_CodeSniffer’s phpcbf, which you can run to fix the code style automatically.
What’s more, Code Cleanup can run automatically on the files selected for commit to VCS. To opt in, simply select the Cleanup checkbox in the Commit Changes dialog.
Your JetBrains PhpStorm Team
The Drive to Develop