ordered_imports.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ========================
  2. Rule ``ordered_imports``
  3. ========================
  4. Ordering ``use`` statements.
  5. Configuration
  6. -------------
  7. ``case_sensitive``
  8. ~~~~~~~~~~~~~~~~~~
  9. Whether the sorting should be case sensitive.
  10. Allowed types: ``bool``
  11. Default value: ``false``
  12. ``imports_order``
  13. ~~~~~~~~~~~~~~~~~
  14. Defines the order of import types.
  15. Allowed types: ``list<string>`` and ``null``
  16. Default value: ``null``
  17. ``sort_algorithm``
  18. ~~~~~~~~~~~~~~~~~~
  19. Whether the statements should be sorted alphabetically or by length, or not
  20. sorted.
  21. Allowed values: ``'alpha'``, ``'length'`` and ``'none'``
  22. Default value: ``'alpha'``
  23. Examples
  24. --------
  25. Example #1
  26. ~~~~~~~~~~
  27. *Default* configuration.
  28. .. code-block:: diff
  29. --- Original
  30. +++ New
  31. <?php
  32. +use AAA;
  33. +use const AAB;
  34. use function AAC;
  35. -use const AAB;
  36. -use AAA;
  37. Example #2
  38. ~~~~~~~~~~
  39. With configuration: ``['case_sensitive' => true]``.
  40. .. code-block:: diff
  41. --- Original
  42. +++ New
  43. <?php
  44. +use const AA;
  45. use function Aaa;
  46. -use const AA;
  47. Example #3
  48. ~~~~~~~~~~
  49. With configuration: ``['sort_algorithm' => 'length']``.
  50. .. code-block:: diff
  51. --- Original
  52. +++ New
  53. <?php
  54. +use Bar;
  55. +use Acme;
  56. +use Bar1;
  57. use Acme\Bar;
  58. -use Bar1;
  59. -use Acme;
  60. -use Bar;
  61. Example #4
  62. ~~~~~~~~~~
  63. With configuration: ``['sort_algorithm' => 'length', 'imports_order' => ['const', 'class', 'function']]``.
  64. .. code-block:: diff
  65. --- Original
  66. +++ New
  67. <?php
  68. +use const BBB;
  69. use const AAAA;
  70. -use const BBB;
  71. +use AAC;
  72. use Bar;
  73. -use AAC;
  74. use Acme;
  75. +use function DDD;
  76. use function CCC\AA;
  77. -use function DDD;
  78. Example #5
  79. ~~~~~~~~~~
  80. With configuration: ``['sort_algorithm' => 'alpha', 'imports_order' => ['const', 'class', 'function']]``.
  81. .. code-block:: diff
  82. --- Original
  83. +++ New
  84. <?php
  85. +use const AAAA;
  86. use const BBB;
  87. -use const AAAA;
  88. +use AAC;
  89. use Acme;
  90. -use AAC;
  91. use Bar;
  92. +use function CCC\AA;
  93. use function DDD;
  94. -use function CCC\AA;
  95. Example #6
  96. ~~~~~~~~~~
  97. With configuration: ``['sort_algorithm' => 'none', 'imports_order' => ['const', 'class', 'function']]``.
  98. .. code-block:: diff
  99. --- Original
  100. +++ New
  101. <?php
  102. use const BBB;
  103. use const AAAA;
  104. -use function DDD;
  105. -use function CCC\AA;
  106. -
  107. use Acme;
  108. use AAC;
  109. +
  110. use Bar;
  111. +use function DDD;
  112. +use function CCC\AA;
  113. Rule sets
  114. ---------
  115. The rule is part of the following rule sets:
  116. - `@PER <./../../ruleSets/PER.rst>`_ with config:
  117. ``['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'none']``
  118. - `@PER-CS <./../../ruleSets/PER-CS.rst>`_ with config:
  119. ``['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'none']``
  120. - `@PER-CS1.0 <./../../ruleSets/PER-CS1.0.rst>`_ with config:
  121. ``['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'none']``
  122. - `@PER-CS2.0 <./../../ruleSets/PER-CS2.0.rst>`_ with config:
  123. ``['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'none']``
  124. - `@PSR12 <./../../ruleSets/PSR12.rst>`_ with config:
  125. ``['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'none']``
  126. - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ with config:
  127. ``['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha']``
  128. - `@Symfony <./../../ruleSets/Symfony.rst>`_ with config:
  129. ``['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha']``
  130. References
  131. ----------
  132. - Fixer class: `PhpCsFixer\\Fixer\\Import\\OrderedImportsFixer <./../../../src/Fixer/Import/OrderedImportsFixer.php>`_
  133. - Test class: `PhpCsFixer\\Tests\\Fixer\\Import\\OrderedImportsFixerTest <./../../../tests/Fixer/Import/OrderedImportsFixerTest.php>`_
  134. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.