ordered_class_elements.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. ===============================
  2. Rule ``ordered_class_elements``
  3. ===============================
  4. Orders the elements of classes/interfaces/traits/enums.
  5. Description
  6. -----------
  7. Accepts a subset of pre-defined element types, special element groups, and
  8. custom patterns.
  9. Element types: ``['use_trait', 'public', 'protected', 'private', 'case',
  10. 'constant', 'constant_public', 'constant_protected', 'constant_private',
  11. 'property', 'property_static', 'property_public', 'property_protected',
  12. 'property_private', 'property_public_readonly', 'property_protected_readonly',
  13. 'property_private_readonly', 'property_public_static',
  14. 'property_protected_static', 'property_private_static', 'method',
  15. 'method_abstract', 'method_static', 'method_public', 'method_protected',
  16. 'method_private', 'method_public_abstract', 'method_protected_abstract',
  17. 'method_private_abstract', 'method_public_abstract_static',
  18. 'method_protected_abstract_static', 'method_private_abstract_static',
  19. 'method_public_static', 'method_protected_static', 'method_private_static']``
  20. Special element types: ``['construct', 'destruct', 'magic', 'phpunit']``
  21. Custom values:
  22. - ``method:*``: specify a single method name (e.g. ``method:__invoke``) to set
  23. the order of that specific method.
  24. Configuration
  25. -------------
  26. ``order``
  27. ~~~~~~~~~
  28. List of strings defining order of elements.
  29. Allowed types: ``array``
  30. Default value: ``['use_trait', 'case', 'constant_public', 'constant_protected', 'constant_private', 'property_public', 'property_protected', 'property_private', 'construct', 'destruct', 'magic', 'phpunit', 'method_public', 'method_protected', 'method_private']``
  31. ``sort_algorithm``
  32. ~~~~~~~~~~~~~~~~~~
  33. How multiple occurrences of same type statements should be sorted.
  34. Allowed values: ``'alpha'`` and ``'none'``
  35. Default value: ``'none'``
  36. ``case_sensitive``
  37. ~~~~~~~~~~~~~~~~~~
  38. Whether the sorting should be case sensitive.
  39. Allowed types: ``bool``
  40. Default value: ``false``
  41. Examples
  42. --------
  43. Example #1
  44. ~~~~~~~~~~
  45. *Default* configuration.
  46. .. code-block:: diff
  47. --- Original
  48. +++ New
  49. <?php
  50. final class Example
  51. {
  52. use BarTrait;
  53. use BazTrait;
  54. const C1 = 1;
  55. const C2 = 2;
  56. - protected static $protStatProp;
  57. public static $pubStatProp1;
  58. public $pubProp1;
  59. + var $pubProp2;
  60. + public static $pubStatProp2;
  61. + public $pubProp3;
  62. + protected static $protStatProp;
  63. protected $protProp;
  64. - var $pubProp2;
  65. private static $privStatProp;
  66. private $privProp;
  67. - public static $pubStatProp2;
  68. - public $pubProp3;
  69. protected function __construct() {}
  70. - private static function privStatFunc() {}
  71. + public function __destruct() {}
  72. + public function __toString() {}
  73. public function pubFunc1() {}
  74. - public function __toString() {}
  75. - protected function protFunc() {}
  76. function pubFunc2() {}
  77. public static function pubStatFunc1() {}
  78. public function pubFunc3() {}
  79. static function pubStatFunc2() {}
  80. - private function privFunc() {}
  81. public static function pubStatFunc3() {}
  82. + protected function protFunc() {}
  83. protected static function protStatFunc() {}
  84. - public function __destruct() {}
  85. + private static function privStatFunc() {}
  86. + private function privFunc() {}
  87. }
  88. Example #2
  89. ~~~~~~~~~~
  90. With configuration: ``['order' => ['method_private', 'method_public']]``.
  91. .. code-block:: diff
  92. --- Original
  93. +++ New
  94. <?php
  95. class Example
  96. {
  97. + private function B(){}
  98. public function A(){}
  99. - private function B(){}
  100. }
  101. Example #3
  102. ~~~~~~~~~~
  103. With configuration: ``['order' => ['method_public'], 'sort_algorithm' => 'alpha']``.
  104. .. code-block:: diff
  105. --- Original
  106. +++ New
  107. <?php
  108. class Example
  109. {
  110. - public function D(){}
  111. + public function A(){}
  112. public function B(){}
  113. - public function A(){}
  114. public function C(){}
  115. + public function D(){}
  116. }
  117. Example #4
  118. ~~~~~~~~~~
  119. With configuration: ``['order' => ['method_public'], 'sort_algorithm' => 'alpha', 'case_sensitive' => true]``.
  120. .. code-block:: diff
  121. --- Original
  122. +++ New
  123. <?php
  124. class Example
  125. {
  126. + public function AA(){}
  127. + public function AWs(){}
  128. public function Aa(){}
  129. - public function AA(){}
  130. public function AwS(){}
  131. - public function AWs(){}
  132. }
  133. Rule sets
  134. ---------
  135. The rule is part of the following rule sets:
  136. - `@PER <./../../ruleSets/PER.rst>`_ with config:
  137. ``['order' => ['use_trait']]``
  138. - `@PER-CS1.0 <./../../ruleSets/PER-CS1.0.rst>`_ with config:
  139. ``['order' => ['use_trait']]``
  140. - `@PER-CS2.0 <./../../ruleSets/PER-CS2.0.rst>`_ with config:
  141. ``['order' => ['use_trait']]``
  142. - `@PSR12 <./../../ruleSets/PSR12.rst>`_ with config:
  143. ``['order' => ['use_trait']]``
  144. - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_
  145. - `@Symfony <./../../ruleSets/Symfony.rst>`_ with config:
  146. ``['order' => ['use_trait']]``