Group Use Statements in PhpStorm 2016.1

Posted on by Gary Hockin

For the recent 2016.1 release, a lot of work was done to improve the group use functionality that landed in PHP 7.

For those who don’t know, PHP 7 gave us the ability to group common namespaces together in our use statements. Consider the following code taken from the widely used Symfony HttpKernel package:

namespace Symfony\Component\HttpKernel;

use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

As we can see, there are a number of reused parts to the namespaces we import. Every imported class starts with Symfony\Component, and 10 out of the 14 classes imported start with Symfony\Component\HttpKernel. That’s a lot of reused code. PHP 7 gives us the group use functionality, which means that the above use statements can be simplified to:
namespace Symfony\Component\HttpKernel;

use Symfony\Component\HttpKernel\{
    Controller\ControllerResolverInterface, Exception\NotFoundHttpException, 
    Exception\HttpExceptionInterface, Event\FilterControllerEvent, 
    Event\FilterResponseEvent, Event\FinishRequestEvent, Event\GetResponseEvent,
    Event\GetResponseForControllerResultEvent, Event\GetResponseForExceptionEvent, 
    Event\PostResponseEvent
};
use Symfony\Component\HttpFoundation\{
    Request, RequestStack, Response
};
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

While earlier versions of PhpStorm have partially supported these grouped use statements, PhpStorm 2016.1 brings a number of improvements and even a new intention to make working with grouped use statements much easier.

Firstly, a number of inconsistencies (read bugs) around group use statements have been fixed. Previously, PhpStorm would get itself confused if you had group use statements and tried to use some of the built in inspections to refactor your code, or tweak your imports. In earlier releases, for example, using the _auto-import namespace_ feature didn’t respect the existing groups, but now it does. We also added improvements around the optimizing of imports, and the refactoring tools (like moving namespaces and classes, and moving methods up and down).

Secondly, and more importantly, a couple of brand new intentions were created. Who wants to have to manually mess around with optimizing a long list of use statements to convert them into groups? Not me, and thankfully we can now use the Group use statements by selected prefix intention.

group-use-intention

Make sure you have a PHP 7 language level set up for your project (you should be able to tell as you’ll have no inspection errors on scalar or return type hints), simply place the caret on the part of the namespace you wish to group by, and use CMD/CTRL + ENTER to pull up the intentions menu. Now you can select Group use statements by selected prefix and PhpStorm will do the rest. If you wish to do the reverse, and “ungroup” your use statements, the Split selected group use statements intention will do exactly that.

There are currently no modifications to the PSR coding standards to standardize the use of group use statements, but the Framework Interoperability Group are in discussion about how to create standards in this area. Until then, the code formatter will leave your group use statements alone, but once PSR-12 has been accepted, we’ll update our coding standard options to include the PHP 7 specific guidelines.

– Gary and the PhpStorm Team

The Drive to Develop!
JetBrains

Comments below can no longer be edited.

4 Responses to Group Use Statements in PhpStorm 2016.1

  1. Johnny Mast says:

    April 8, 2016

    What is the theme used the gif?

  2. chocochaos says:

    April 8, 2016

    Could you please include a fix for https://youtrack.jetbrains.com/issue/WI-30953 ? It’s bad enough that 2016.1 was released without this bug fixed.

  3. Akairis says:

    April 19, 2016

    ctrl + Enter not work for me. I have default settings in PHPStorm

Subscribe

Subscribe for updates