curly_braces_position.rst 4.6 KB

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