class_attributes_separation.rst 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ====================================
  2. Rule ``class_attributes_separation``
  3. ====================================
  4. Class, trait and interface elements must be separated with one or none blank
  5. line.
  6. Configuration
  7. -------------
  8. ``elements``
  9. ~~~~~~~~~~~~
  10. Dictionary of ``const|method|property|trait_import|case`` =>
  11. ``none|one|only_if_meta`` values.
  12. Allowed types: ``array<string, string>``
  13. Default value: ``['const' => 'one', 'method' => 'one', 'property' => 'one', 'trait_import' => 'none', 'case' => 'none']``
  14. Examples
  15. --------
  16. Example #1
  17. ~~~~~~~~~~
  18. *Default* configuration.
  19. .. code-block:: diff
  20. --- Original
  21. +++ New
  22. <?php
  23. final class Sample
  24. {
  25. protected function foo()
  26. {
  27. }
  28. +
  29. protected function bar()
  30. {
  31. }
  32. -
  33. -
  34. }
  35. Example #2
  36. ~~~~~~~~~~
  37. With configuration: ``['elements' => ['property' => 'one']]``.
  38. .. code-block:: diff
  39. --- Original
  40. +++ New
  41. <?php
  42. class Sample
  43. -{private $a; // foo
  44. +{
  45. +private $a; // foo
  46. +
  47. /** second in a hour */
  48. private $b;
  49. }
  50. Example #3
  51. ~~~~~~~~~~
  52. With configuration: ``['elements' => ['const' => 'one']]``.
  53. .. code-block:: diff
  54. --- Original
  55. +++ New
  56. <?php
  57. class Sample
  58. {
  59. const A = 1;
  60. +
  61. /** seconds in some hours */
  62. const B = 3600;
  63. }
  64. Example #4
  65. ~~~~~~~~~~
  66. With configuration: ``['elements' => ['const' => 'only_if_meta']]``.
  67. .. code-block:: diff
  68. --- Original
  69. +++ New
  70. <?php
  71. class Sample
  72. {
  73. /** @var int */
  74. const SECOND = 1;
  75. +
  76. /** @var int */
  77. const MINUTE = 60;
  78. -
  79. const HOUR = 3600;
  80. -
  81. const DAY = 86400;
  82. }
  83. Example #5
  84. ~~~~~~~~~~
  85. With configuration: ``['elements' => ['property' => 'only_if_meta']]``.
  86. .. code-block:: diff
  87. --- Original
  88. +++ New
  89. <?php
  90. class Sample
  91. {
  92. public $a;
  93. +
  94. #[SetUp]
  95. public $b;
  96. +
  97. /** @var string */
  98. public $c;
  99. +
  100. /** @internal */
  101. #[Assert\String()]
  102. public $d;
  103. -
  104. public $e;
  105. }
  106. Rule sets
  107. ---------
  108. The rule is part of the following rule sets:
  109. - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ with config:
  110. ``['elements' => ['method' => 'one']]``
  111. - `@Symfony <./../../ruleSets/Symfony.rst>`_ with config:
  112. ``['elements' => ['method' => 'one']]``
  113. References
  114. ----------
  115. - Fixer class: `PhpCsFixer\\Fixer\\ClassNotation\\ClassAttributesSeparationFixer <./../../../src/Fixer/ClassNotation/ClassAttributesSeparationFixer.php>`_
  116. - Test class: `PhpCsFixer\\Tests\\Fixer\\ClassNotation\\ClassAttributesSeparationFixerTest <./../../../tests/Fixer/ClassNotation/ClassAttributesSeparationFixerTest.php>`_
  117. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.