Fork me on GitHub
a Sensio Labs Product

Space Sets (v3.64.0) edition

Rule ordered_imports

Ordering use statements.

Configuration

case_sensitive

Whether the sorting should be case sensitive.

Allowed types: bool

Default value: false

imports_order

Defines the order of import types.

Allowed types: list<string> and null

Default value: null

sort_algorithm

Whether the statements should be sorted alphabetically or by length, or not sorted.

Allowed values: 'alpha', 'length' and 'none'

Default value: 'alpha'

Examples

Example #1

Default configuration.

--- Original
+++ New
 <?php
+use AAA;
+use const AAB;
 use function AAC;
-use const AAB;
-use AAA;

Example #2

With configuration: ['case_sensitive' => true].

--- Original
+++ New
 <?php
+use const AA;
 use function Aaa;
-use const AA;

Example #3

With configuration: ['sort_algorithm' => 'length'].

--- Original
+++ New
 <?php
+use Bar;
+use Acme;
+use Bar1;
 use Acme\Bar;
-use Bar1;
-use Acme;
-use Bar;

Example #4

With configuration: ['sort_algorithm' => 'length', 'imports_order' => ['const', 'class', 'function']].

--- Original
+++ New
 <?php
+use const BBB;
 use const AAAA;
-use const BBB;

+use AAC;
 use Bar;
-use AAC;
 use Acme;

+use function DDD;
 use function CCC\AA;
-use function DDD;

Example #5

With configuration: ['sort_algorithm' => 'alpha', 'imports_order' => ['const', 'class', 'function']].

--- Original
+++ New
 <?php
+use const AAAA;
 use const BBB;
-use const AAAA;

+use AAC;
 use Acme;
-use AAC;
 use Bar;

+use function CCC\AA;
 use function DDD;
-use function CCC\AA;

Example #6

With configuration: ['sort_algorithm' => 'none', 'imports_order' => ['const', 'class', 'function']].

--- Original
+++ New
 <?php
 use const BBB;
 use const AAAA;

-use function DDD;
-use function CCC\AA;
-
 use Acme;
 use AAC;
+
 use Bar;
+use function DDD;
+use function CCC\AA;

Rule sets

The rule is part of the following rule sets:

  • @PER with config:

    ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'none']

  • @PER-CS with config:

    ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'none']

  • @PER-CS1.0 with config:

    ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'none']

  • @PER-CS2.0 with config:

    ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'none']

  • @PSR12 with config:

    ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'none']

  • @PhpCsFixer with config:

    ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha']

  • @Symfony with config:

    ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha']

References

The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.