braces.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ===============
  2. Rule ``braces``
  3. ===============
  4. The body of each structure MUST be enclosed by braces. Braces should be properly
  5. placed. Body of braces should be properly indented.
  6. Warning
  7. -------
  8. This rule is deprecated and will be removed in the next major version
  9. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. You should use ``single_space_around_construct``, ``control_structure_braces``,
  11. ``control_structure_continuation_position``, ``declare_parentheses``,
  12. ``no_multiple_statements_per_line``, ``braces_position``,
  13. ``statement_indentation`` and ``no_extra_blank_lines`` instead.
  14. Configuration
  15. -------------
  16. ``allow_single_line_anonymous_class_with_empty_body``
  17. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  18. Whether single line anonymous class with empty body notation should be allowed.
  19. Allowed types: ``bool``
  20. Default value: ``false``
  21. ``allow_single_line_closure``
  22. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. Whether single line lambda notation should be allowed.
  24. Allowed types: ``bool``
  25. Default value: ``false``
  26. ``position_after_anonymous_constructs``
  27. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. Whether the opening brace should be placed on "next" or "same" line after
  29. anonymous constructs (anonymous classes and lambda functions).
  30. Allowed values: ``'next'`` and ``'same'``
  31. Default value: ``'same'``
  32. ``position_after_control_structures``
  33. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  34. Whether the opening brace should be placed on "next" or "same" line after
  35. control structures.
  36. Allowed values: ``'next'`` and ``'same'``
  37. Default value: ``'same'``
  38. ``position_after_functions_and_oop_constructs``
  39. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  40. Whether the opening brace should be placed on "next" or "same" line after classy
  41. constructs (non-anonymous classes, interfaces, traits, methods and non-lambda
  42. functions).
  43. Allowed values: ``'next'`` and ``'same'``
  44. Default value: ``'next'``
  45. Examples
  46. --------
  47. Example #1
  48. ~~~~~~~~~~
  49. *Default* configuration.
  50. .. code-block:: diff
  51. --- Original
  52. +++ New
  53. <?php
  54. -class Foo {
  55. - public function bar($baz) {
  56. - if ($baz = 900) echo "Hello!";
  57. +class Foo
  58. +{
  59. + public function bar($baz)
  60. + {
  61. + if ($baz = 900) {
  62. + echo "Hello!";
  63. + }
  64. - if ($baz = 9000)
  65. + if ($baz = 9000) {
  66. echo "Wait!";
  67. + }
  68. - if ($baz == true)
  69. - {
  70. + if ($baz == true) {
  71. echo "Why?";
  72. - }
  73. - else
  74. - {
  75. + } else {
  76. echo "Ha?";
  77. }
  78. - if (is_array($baz))
  79. - foreach ($baz as $b)
  80. - {
  81. + if (is_array($baz)) {
  82. + foreach ($baz as $b) {
  83. echo $b;
  84. }
  85. + }
  86. }
  87. }
  88. Example #2
  89. ~~~~~~~~~~
  90. With configuration: ``['allow_single_line_closure' => true]``.
  91. .. code-block:: diff
  92. --- Original
  93. +++ New
  94. <?php
  95. $positive = function ($item) { return $item >= 0; };
  96. $negative = function ($item) {
  97. - return $item < 0; };
  98. + return $item < 0;
  99. +};
  100. Example #3
  101. ~~~~~~~~~~
  102. With configuration: ``['position_after_functions_and_oop_constructs' => 'same']``.
  103. .. code-block:: diff
  104. --- Original
  105. +++ New
  106. <?php
  107. -class Foo
  108. -{
  109. - public function bar($baz)
  110. - {
  111. - if ($baz = 900) echo "Hello!";
  112. +class Foo {
  113. + public function bar($baz) {
  114. + if ($baz = 900) {
  115. + echo "Hello!";
  116. + }
  117. - if ($baz = 9000)
  118. + if ($baz = 9000) {
  119. echo "Wait!";
  120. + }
  121. - if ($baz == true)
  122. - {
  123. + if ($baz == true) {
  124. echo "Why?";
  125. - }
  126. - else
  127. - {
  128. + } else {
  129. echo "Ha?";
  130. }
  131. - if (is_array($baz))
  132. - foreach ($baz as $b)
  133. - {
  134. + if (is_array($baz)) {
  135. + foreach ($baz as $b) {
  136. echo $b;
  137. }
  138. + }
  139. }
  140. }
  141. References
  142. ----------
  143. - Fixer class: `PhpCsFixer\\Fixer\\Basic\\BracesFixer <./../../../src/Fixer/Basic/BracesFixer.php>`_
  144. - Test class: `PhpCsFixer\\Tests\\Fixer\\Basic\\BracesFixerTest <./../../../tests/Fixer/Basic/BracesFixerTest.php>`_
  145. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.