Standard/methodology for inlining full classpath vs "use"

I’m trying to figure out if there is a standard or methodology being used for inlining full classpaths vs employing the “use” directive. There seem to be three different methods in place:

  • In the majority of cases, classes are imported with the “use” directive so they can be referenced without having to use the full classpath and namespace.
  • In some cases, fully namespaced classes are inlined. This appears to happen when the class is only used once, but that isn’t a hard rule. In fact, there are some places where classes that are only used once are imported with “use” while classes that are used multiple times are inlined.
  • Some files like index.php and cron.php inline everything. Perhaps this is because they don’t declare a namespace?

There’s also the case where OC is imported with “use,” but this is only done in 12 files across the whole codebase. Everywhere else, the code uses \OC.

What are the guidelines for NC? I personally lean toward importing with the “use” directive whenever possible because it makes it simple to see what classes are being used for a particular file - everything is right up at the top.

I would say, go with the use method. The other options sound more like special cases where you really know what you are doing. At least for normal app development that is perfectly fine and the default case.

I am not aware of any specific guideline for Nextcloud. But having use instead of fully qualified classnames is always the preferred solution since you then have all namespaces you need at the beginning of your file and not somewhere within the code.

index.php and cron.php do not inline everything - they include lib/base.php which then also makes a lot of use of the use statement.

I am actually talking about core development, not app development. I’m considering cleaning up some of the deprecated calls. For example:

is deprecated and replaced by:

which is technically:

Let’s look at some examples where getConfig() is used:

  • In the vast majority of NC files, classes are imported with use. This makes sense.
  • In cron.php and public.php, nothing is imported with the use directive. Instead, all classpaths are inlined.
  • In console.php, remote.php, register_command.php, and base.php, some classes are imported with use, but some are inlined.

For each of the above scenarios:

  1. First (and most importantly in my opinion), WHY are some classes inlined vs imported via use? What makes each scenario different? I can find no correlation between frequency of use of the class, the declaration of a namespace for the current class, or anything else to determine when to inline vs not.
  2. Second (and only after someone explains point 1), should I inline the full classpaths or import the AllConfig class with use?

Finally, can someone address the 12 instances of classes where use OC is declared while the rest of the NC codebase inlines \OC? What makes those classes different?

Basically, I’m just trying to figure out the methodology, here. If there isn’t one, perhaps it should be established.

In my personal opinion, I think that it makes sense to import anything that isn’t at the root classpath (or that shares the same base classpath of the current namespace) with use. So, inline things like:

  • \OC
  • \Exception
  • \DirectoryIterator

but import everything else with use. However, if the class is at the base of the current namespace, there is no need to import it with use. For example, it is unnecessary to have use OC\App\InfoParser at the top of OC\App\AppManager since they both share the same base classpath.