Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Proposed solution
use
statements MUST be located after the initial@file
phpDoc block.use
statements SHOULD be "ordered by":- (FullyQualifiedClassName == framework/vendor namespace)
- FullyQualifiedClassName
Example
/**
* @file
* Contains Drupal\node\Node.
*/
namespace Drupal\node;
use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Entity\EntityFieldQuery;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\file\File;
use Drupal\user\User;
Comments
Comment #1
Lars Toomre CreditAttribution: Lars Toomre commentedWonderful... What is the Drupal policy? Alphabetical order?
Comment #2
Crell CreditAttribution: Crell commentedThere is currently no Drupal policy. I couldn't find the previous issue where this was discussed and ended up with a general "meh".
Most of my code has been putting all Symfony classes together in one block, then all Drupal stuff together in a separate block, ordered roughly by component/subsystem. I don't know that a hard alphabetical order is necessary; it's not like it actually means anything to the compiler either way, and we're not going to be doing deployment based on the diffs (like for CMI objects).
Comment #3
Anonymous (not verified) CreditAttribution: Anonymous commentedThis was also discussed here: #1624564: Coding standards for "use" statements
Personally, I like the suggestion as the code looks cleaner (to me), but the issue that was raised there was more that it's another coding standards issue to enforce, and that some people may be resistant.
If there are starting to be large numbers of these, though, I would prefer a standard.
Comment #4
sunOh. Sorry, I was not aware that we had an issue already. Marking as duplicate. Moved everything over.