Proposed solution

  1. use statements MUST be located after the initial @file phpDoc block (and after the namespace declaration, if any).
  2. use statements SHOULD be "ordered by":
    1. (FullyQualifiedClassName == framework/vendor namespace)
    2. FullyQualifiedClassName


 * @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;

At least one use statement block is mixing symfony and drupal use statement. Let's go through the kernel code and order these lines in a sane way.


aspilicious’s picture

Title: Order the use statement in a sane way » Order the use statements in a sane way
chrisdolby’s picture

Issue tags: +coding standards

It would be good to determine what comes first, whether purely alphabetically or Symfony first, Drupal second (or vice versa), or something else.

Crell’s picture

My knee-jerk suggestion is "external" classes first, alphabetically, and Drupal-namespaced classes last, alphabetically, with a whitespace break between them. Or possibly between each "set" of namespaces. (So if we pull in some Zend code, for instance, it would be all Symfony classes, break, all Zend classes, break, all Drupal classes.)

That's just a knee-jerk suggestion, though, so feel free to disagree. :-)

jhodgdon’s picture

There is always resistance to adding coding standards that everyone needs to follow... Is this one really necessary? How often do we even have more than just a couple of "use" statements (I'm assuming that if we have only 2-3 of them, the order isn't really important)?

Crell’s picture

My totally unscientific memory of classes in the kernel work to date suggests that 5-10 use statements per file is not at all unusual. At that point I think having some minimal level of organization is useful, although I totally agree that it probably does not justify a strict rule. Perhaps if we just had clustering, eg, list all Symfony classes first, linebreak, then Drupal classes? That is effectively what I've been doing anyway in any of the classes I've touched.

(There zero runtime impact here so it's a purely human usability / aesthetic question.)

aspilicious’s picture

Most newer patches don't use a space but order them.
So can we just say we try to order them with ot without a space in the future? :)

jhodgdon’s picture

Do we really need a standard for this? I don't think we have a standard for the order of properties in a render API array and stuff like that.. the assumption is that people should do it in a logical and readable manner.

aspilicious’s picture

I can live with no standard...

Crell’s picture

I am OK with postponing this thread until the issue does come up in another thread. Then we can reopen this one. :-)

ZenDoodles’s picture

Status: Active » Postponed
sun’s picture

Title: Order the use statements in a sane way » [policy] Coding standards for "use" statements
Component: other » documentation
Status: Postponed » Active
Issue tags: +PSR-0

Unpostponing since I filed #1791928: [policy] Coding standards for "use" statements yesterday...

I've copied the issue summary / concrete proposal from over there.

sun’s picture

Issue summary: View changes

Imported summary from

Lars Toomre’s picture

As I asked in the duplicate thread, what is the suggested sane ordering method? Alphabetical?

YesCT’s picture

I think we are seeing longer lists of use statements and people are starting to add new use's in alphabetical order.

jhodgdon’s picture

Do we need a standard though? The more standards we have, the more we either have (a) a lot of Core code that violates them or (b) a lot of patches being held up on standards. Neither of these outcomes is optimal, so we need to think hard about whether a standard is really necessary before we adopt one.

In this case, my personal view is that a standard saying that all use statements need to be alphabetical is not necessary, and that adding another standard that most of Core does not follow is not a good idea.

I guess I'm sounding like a broken record. I have asked this same question three times now about whether having a standard at all is necessary. So far, no one has really answered that question.

Crell’s picture

I don't think a strict standard is necessary. At best a "recommended but not enforced". We have enough nitpicks to block patches on without adding another one that doesn't do anything. :-)

YesCT’s picture

@jhodgdon I think no one is answering because we agree? :)

claudiu.cristea’s picture

The most used seems to be:

Drupal\*\*  <= modules

Each wildcard in alphabetical order.

jhodgdon’s picture

Status: Active » Closed (won't fix)

I am going to just close this as "won't fix". There doesn't seem to be any momentum towards making this a standard that we want to enforce. If individual files have completely random ordering, we can clean those up in the general interest of code readability (which is a guideline we have in Drupal and in fact in any code), without having a standard that says we must.

If you disagree strongly and can recruit more people who also do, please reopen this issue, but I just haven't seen much interest here.

jhodgdon’s picture

Issue summary: View changes

Updated issue summary.

catch’s picture

Issue summary: View changes
Status: Closed (won't fix) » Active
jhodgdon’s picture

I added a note to about this. That seems to be the only place in our coding standards docs pages that talks about "use" statements.

If that's OK, maybe change this issue back ot "won't fix", since we didn't make a standard?

mpdonadio’s picture

I think something more concrete would prevent back-and-forths w/ patch reviews, and precise language is better in specs (esp w/ non-English speakers). What may be sensible to one person may not be sensible to someone else. I vote to use language like "alphabetical unless there is a very compelling reason for a different grouping".

jhodgdon’s picture

Project: Drupal core » Coding Standards
Version: 8.0.x-dev »
Component: documentation » Coding Standards

Sure, in an ideal world, but please read the entire thread here. There wasn't general agreement that we needed a standard, especially as Core would be in violation of it immediately.

I guess what we should do is leave this open and move it to the coding standards issue queue. Meanwhile, I think the standards page is correct when it says we do not have a standard.

dawehner’s picture

These days I think given that so many people use phpstorm their order is kind of implicit deterministic, because it resorts them automatically, I think.