braces_position.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ========================
  2. Rule ``braces_position``
  3. ========================
  4. Braces must be placed as configured.
  5. Configuration
  6. -------------
  7. ``allow_single_line_anonymous_functions``
  8. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. Allow anonymous functions to have opening and closing braces on the same line.
  10. Allowed types: ``bool``
  11. Default value: ``true``
  12. ``allow_single_line_empty_anonymous_classes``
  13. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. Allow anonymous classes to have opening and closing braces on the same line.
  15. Allowed types: ``bool``
  16. Default value: ``true``
  17. ``anonymous_classes_opening_brace``
  18. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. The position of the opening brace of anonymous classes‘ body.
  20. Allowed values: ``'next_line_unless_newline_at_signature_end'`` and ``'same_line'``
  21. Default value: ``'same_line'``
  22. ``anonymous_functions_opening_brace``
  23. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. The position of the opening brace of anonymous functions‘ body.
  25. Allowed values: ``'next_line_unless_newline_at_signature_end'`` and ``'same_line'``
  26. Default value: ``'same_line'``
  27. ``classes_opening_brace``
  28. ~~~~~~~~~~~~~~~~~~~~~~~~~
  29. The position of the opening brace of classes‘ body.
  30. Allowed values: ``'next_line_unless_newline_at_signature_end'`` and ``'same_line'``
  31. Default value: ``'next_line_unless_newline_at_signature_end'``
  32. ``control_structures_opening_brace``
  33. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  34. The position of the opening brace of control structures‘ body.
  35. Allowed values: ``'next_line_unless_newline_at_signature_end'`` and ``'same_line'``
  36. Default value: ``'same_line'``
  37. ``functions_opening_brace``
  38. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  39. The position of the opening brace of functions‘ body.
  40. Allowed values: ``'next_line_unless_newline_at_signature_end'`` and ``'same_line'``
  41. Default value: ``'next_line_unless_newline_at_signature_end'``
  42. Examples
  43. --------
  44. Example #1
  45. ~~~~~~~~~~
  46. *Default* configuration.
  47. .. code-block:: diff
  48. --- Original
  49. +++ New
  50. <?php
  51. -class Foo {
  52. +class Foo
  53. +{
  54. }
  55. -function foo() {
  56. +function foo()
  57. +{
  58. }
  59. -$foo = function()
  60. -{
  61. +$foo = function() {
  62. };
  63. -if (foo())
  64. -{
  65. +if (foo()) {
  66. bar();
  67. }
  68. -$foo = new class
  69. -{
  70. +$foo = new class {
  71. };
  72. Example #2
  73. ~~~~~~~~~~
  74. With configuration: ``['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end']``.
  75. .. code-block:: diff
  76. --- Original
  77. +++ New
  78. <?php
  79. -if (foo()) {
  80. +if (foo())
  81. +{
  82. bar();
  83. }
  84. Example #3
  85. ~~~~~~~~~~
  86. With configuration: ``['functions_opening_brace' => 'same_line']``.
  87. .. code-block:: diff
  88. --- Original
  89. +++ New
  90. <?php
  91. -function foo()
  92. -{
  93. +function foo() {
  94. }
  95. Example #4
  96. ~~~~~~~~~~
  97. With configuration: ``['anonymous_functions_opening_brace' => 'next_line_unless_newline_at_signature_end']``.
  98. .. code-block:: diff
  99. --- Original
  100. +++ New
  101. <?php
  102. -$foo = function () {
  103. +$foo = function ()
  104. +{
  105. };
  106. Example #5
  107. ~~~~~~~~~~
  108. With configuration: ``['classes_opening_brace' => 'same_line']``.
  109. .. code-block:: diff
  110. --- Original
  111. +++ New
  112. <?php
  113. -class Foo
  114. -{
  115. +class Foo {
  116. }
  117. Example #6
  118. ~~~~~~~~~~
  119. With configuration: ``['anonymous_classes_opening_brace' => 'next_line_unless_newline_at_signature_end']``.
  120. .. code-block:: diff
  121. --- Original
  122. +++ New
  123. <?php
  124. -$foo = new class {
  125. +$foo = new class
  126. +{
  127. };
  128. Example #7
  129. ~~~~~~~~~~
  130. With configuration: ``['allow_single_line_empty_anonymous_classes' => true]``.
  131. .. code-block:: diff
  132. --- Original
  133. +++ New
  134. <?php
  135. $foo = new class { };
  136. -$bar = new class { private $baz; };
  137. +$bar = new class {
  138. +private $baz;
  139. +};
  140. Example #8
  141. ~~~~~~~~~~
  142. With configuration: ``['allow_single_line_anonymous_functions' => true]``.
  143. .. code-block:: diff
  144. --- Original
  145. +++ New
  146. <?php
  147. $foo = function () { return true; };
  148. -$bar = function () { $result = true;
  149. - return $result; };
  150. +$bar = function () {
  151. +$result = true;
  152. + return $result;
  153. +};
  154. Rule sets
  155. ---------
  156. The rule is part of the following rule sets:
  157. - `@PER <./../../ruleSets/PER.rst>`_ with config:
  158. ``['allow_single_line_empty_anonymous_classes' => true]``
  159. - `@PER-CS <./../../ruleSets/PER-CS.rst>`_ with config:
  160. ``['allow_single_line_empty_anonymous_classes' => true]``
  161. - `@PER-CS1.0 <./../../ruleSets/PER-CS1.0.rst>`_ with config:
  162. ``['allow_single_line_empty_anonymous_classes' => true]``
  163. - `@PER-CS2.0 <./../../ruleSets/PER-CS2.0.rst>`_ with config:
  164. ``['allow_single_line_empty_anonymous_classes' => true]``
  165. - `@PSR2 <./../../ruleSets/PSR2.rst>`_
  166. - `@PSR12 <./../../ruleSets/PSR12.rst>`_ with config:
  167. ``['allow_single_line_empty_anonymous_classes' => true]``
  168. - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ with config:
  169. ``['allow_single_line_anonymous_functions' => true, 'allow_single_line_empty_anonymous_classes' => true]``
  170. - `@Symfony <./../../ruleSets/Symfony.rst>`_ with config:
  171. ``['allow_single_line_anonymous_functions' => true, 'allow_single_line_empty_anonymous_classes' => true]``
  172. References
  173. ----------
  174. - Fixer class: `PhpCsFixer\\Fixer\\Basic\\BracesPositionFixer <./../../../src/Fixer/Basic/BracesPositionFixer.php>`_
  175. - Test class: `PhpCsFixer\\Tests\\Fixer\\Basic\\BracesPositionFixerTest <./../../../tests/Fixer/Basic/BracesPositionFixerTest.php>`_
  176. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.